Rename package in Android Studio
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.
In Android Studio, you can do this:
For example, if you want to change
In your Project pane, click on the little gear icon ( )
Uncheck / De-select the
Compact Empty Middle Packagesoption
Your package directory will now be broken up in individual directories
Individually select each directory you want to rename, and:
In the Pop-up dialog, click on
Rename Packageinstead 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
comin Android Studio, it might give a warning. In such case, select Rename All
Now open your Gradle Build File (
mobile). Update the
defaultConfigto 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! Anyway, Android Studio needs to make this process a little simpler.
Read more… Read less…
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.
You have to manually change AndroidManifest.xml and build.gradle file to the new package if you use this method.
Changing the application ID (which is now independent of the package name) can be done very easily in one step. You don't have to touch AndroidManifest. Instead do the following:
- right click on the root folder of your project.
- Click "Open Module Setting".
- Go to the Flavours tab.
- Change the applicationID to whatever package name you want. Press OK.
Note this will not change the package name. The decoupling of Package Name and Application ID is explained here: http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename
The approach used by me for renaming the package name is simple as follows:-
Step 1 : Select the Project option from left menu of Android Studio
Step 2 : Right click on java and add a new package and set the desired package name
Step 3 : Enter you new packagename
Step 4 :Copy all the files from your old package and paste in the new package
Step 5 :Rename the package name in manifest file
Step 6 :Rename the package name in build.gradle file
Step 7 :Then right click the old package and delete it with all its data, and delete that directory as well
Step 8 :Then Rebuild your project
Step 9 :Then you will find some errors of old import packagename in your project Select the old package name in any file and press CTRL + Shift + R , and enter you new package name in replace box, then press find
Step 10 :Then a popup appears like below and select All files option from it
Step 11 :Rebuild your project again, bingo your project packagename has been changed :)
- Goto your AndroidManifest.xml file.
Place your cursor in the package name like shown below. Don't select it, just place it.
Then press Shift + F6 you will get a popup window as shown below select Rename package.
Enter your new name and select Refactor. (Note since my cursor is on "something", only something is renamed.)
That's it done.
Right-click on the package at the Project Panel.
Choose Refactor -> Rename from the context menu.