Rename package in Android Studio

2018-12-31 01:09发布

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 refactoring, but I don't know how. I worked two years with Eclipse and in Eclipse it's a one-click operation.

30条回答
伤终究还是伤i
2楼-- · 2018-12-31 01:49

Be sure to go way back up to Sheharyar's Answer that starts off In Android Studio, you can do this: because it's GREAT. 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:

Deselect Compact Empty Middle Packages.

Refactor Rename each old directory node by choosing CHANGE PACKAGE (not directory) to match new package name. (Be sure to do a preview of changes.)

Edit build.gradle file and make APPLICATION_ID match new package name.

查看更多
高级女魔头
3楼-- · 2018-12-31 01:50

Another good method is: First create a new package with the desired name by right clicking on the Java folder → NewPackage.

Then, select and drag all your classes to the new package. Android Studio will refactor the package name everywhere.

Finally, delete the old package.

Done.

Very important:

You have to manually change AndroidManifest.xml and build.gradle file to the new package if you use this method.

查看更多
梦醉为红颜
4楼-- · 2018-12-31 01:50

The common mistake that one can make is one cannot rename the package structure i.e it is not possible to change com.name.android to com.Renamed.android when one tries to modify at com.name.android level.

In order to have the same desired change go one level up i.e com.name and here when you refactor change it to Renamed. This will work always

查看更多
妖精总统
5楼-- · 2018-12-31 01:50

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. 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 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".

Hope this helps :)

查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 01:51

In Android Studio, you can do this:

For example, if you want to change com.example.app to my.awesome.game, then:

  1. In your Project pane, click on the little gear icon ( Gears icon )

  2. Uncheck / De-select the Compact Empty Middle Packages option

Compact Empty Middle Packages

  1. Your package directory will now be broken up in individual directories

  2. Individually select each directory you want to rename, and:

    • Right-click it
    • Select Refactor
    • Click on Rename
    • In the Pop-up dialog, click on Rename Package instead of Rename Directory
    • Enter the new name and hit Refactor
    • Click Do Refactor in the bottom
    • Allow a minute to let Android Studio update all changes
    • Note: When renaming com in Android Studio, it might give a warning. In such case, select Rename All

enter image description here

  1. Now open your Gradle Build File (build.gradle - Usually app or mobile). Update the applicationId in the defaultConfig to your new Package Name and Sync Gradle, if it hasn't already been updated automatically:

Refactor Directories

  1. You may need to change the package= attribute in your manifest.

  2. Clean and Rebuild.

  3. Done! Anyways, Android Studio needs to make this process a little simpler.

查看更多
时光乱了年华
7楼-- · 2018-12-31 01:51

This I believe what you are looking for is Refactor > Move. You can also press F6.

You will get two popups. Make sure you select rename package on both. After that you will get a pop up to place the new package name.

Please note that if your package name is, for example, com.example.android.projectname then this will let you change com.example.android to something else.

One more thing, this will update the applicationId in your build.gradle as well. Just make sure that the checkboxes for "search in comments and strings" and "search for text occurrences" are both checked for it to work.

查看更多
登录 后发表回答