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.
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 makeAPPLICATION_ID
match new package name.Another good method is: First create a new package with the desired name by right clicking on the Java folder → New → Package.
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.
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
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:
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 :)
In Android Studio, you can do this:
For example, if you want to change
com.example.app
tomy.awesome.game
, then:In your Project pane, click on the little gear icon ( )
Uncheck / De-select the
Compact Empty Middle Packages
optionYour package directory will now be broken up in individual directories
Individually select each directory you want to rename, and:
Refactor
Rename
Rename Package
instead of Rename Directorycom
in Android Studio, it might give a warning. In such case, select Rename Allbuild.gradle
- Usuallyapp
ormobile
). Update theapplicationId
in thedefaultConfig
to your new Package Name and Sync Gradle, if it hasn't already been updated automatically:You may need to change the
package=
attribute in your manifest.Clean and Rebuild.
Done! Anyways, Android Studio needs to make this process a little simpler.
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.