Rename package in Android Studio

后端 未结 30 1973
深忆病人
深忆病人 2020-11-21 05:21

How do you rename packages in the new IDE Android Studio, based on IntelliJ IDEA?

Is there an automatic refactoring included?

I want to make bulk refactorin

相关标签:
30条回答
  • 2020-11-21 06:01

    I found another way that works or an extra step to some of the answers here especially if you want to change the domain as well. It works in Android Studio 1.4. This is what I did:

    1. Open Manifest.xml and change the package name to what you want.
    2. Open your app build.gradle file and change the Application Id in defaultConfig to the same name as in manifest and rebuild the project.
    3. If still an issue, open a file under the package name, go to the package breadcrumbs (i.e. package declaration at head of file) and set your cursor to the domain you want to change and hit "Shift + F6", it would come out with a dialog with multiple use warnings, click on "Rename packages" and then click on "Do Refactor" it should rename everything including the R.Java files.

    So for example if you want to rename "com.example.app" to "com.YourDomain.app", open a file under the package to be renamed, in the package breadcrumbs, set your cursor to "example" part of the domain and hit Shift + F6 and rename package to "YourDomain".

    0 讨论(0)
  • 2020-11-21 06:01

    I tried the two top-voted solutions but found some issues even though both work to some extent.

    • List item: The new package-drag-drop method leaves some unchanged and creates some undesired effects
    • List item: The rename package only changes the last part of package name

    After some experiments I found the following method works well for me.

    If you just need to change the last part of package name, use the method outlined by GreyBeardedGeek, namely

    Right-click on the package in the Project pane. Choose Refactor -> Rename from the context menu

    If you need to change the whole package name, do the following.

    Right-click on the package in the Project pane. Choose Refactor -> Move from the context menu

    This will create a new package folder (when necessary) but will keep the last part of your package name as before. If you need to change the last part, do the rename accordingly.

    Note also that you may need to modify package names in e.g. build.gradle, manifest, and/or any xml resource files, or even in your code if hardcoded. After all that, do Sync/Clean/Rebuild project as necessary.

    0 讨论(0)
  • 2020-11-21 06:02

    Please try the following steps:

    1. Click on setting gear icon and deselect Compact Empty Middle Package
    2. Now we can see each package folder is broken into parts
    3. Now right click on the first package folder >>> refactor >>> rename
    4. Now a warning will be displayed but you go ahead and click Rename Package
    5. After that enter your domain name for the package name
    6. Click on ‘Do Refactor’
    7. Now it has change the package domain name of the App. Now Change the domain extension and App folder name according to your requirement
    8. Now open build.gradle (Module: app) in Gradle Scripts. Here change the application id and click Sync Now.

    *******Finally it’s done ******

    0 讨论(0)
  • 2020-11-21 06:04

    I recommend to use the Sublime Text (or Notepad++). Replace com.one.lastname -> com.two.newname and com/one/lastname -> com/two/newname in ...\Projects[MyProject]. and don't forget to rename ...\Projects\MyProject\app\src\main\java\com\one\lastname, ...\Projects\MyProject\app\src\test\java\com\one\lastname and ...\Projects\MyProject\app\src\androidTest\java\com\one\lastname!

    That's all:)

    0 讨论(0)
  • 2020-11-21 06:05

    Be sure to go to Sheharyar's great Answer. There are so many answers and comments that follow it that it would be easy to get confused and give up, but don't. That answer works.

    In short, you do three things:

    (1) Deselect Compact Empty Middle Packages.

    (2) Refactor then Rename each old directory node by choosing Change Package (not Change Directory) to match the new package name. (Be sure to do a preview of changes.)

    (3) Edit the build.gradle file and make APPLICATION_ID match the new package name.

    0 讨论(0)
  • 2020-11-21 06:07
    • The first part consists of creating a new package under java folder and selecting then dragging all your source files from the old package to this new package. After that you need to remane the package name in android manifest to the name of the new package.

    • In step 2, here is what you need to do.You need to change the old package name in applicationId under the module build.gradle in your android studio in addition to changing the package name in the manifest. So in summary, click on build.gradle which is below the "AndroidManifest.xml" and modify the value of applicationId to your new package name.

    • Then, at the very top, under build. clean your project, then rebuild. It should be fine from here.

    0 讨论(0)
提交回复
热议问题