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.
If your package name is more than two dot separated, say
com.hello.world
and moreover, you did not put anything incom/
andcom/hello/
. All of your classes are putting intocom/hello/world/
, you might DO the following steps to refactoring your package name(s) in Android Studio or IntelliJ:com/
,com/hello/
). You can achieve this by first add two files to package com.hello.world, saythen refactor them by moving them to com and com.hello respectively. You will see com and com.hello sitting there at the Project(
Alt+1
orCommand+1
for shortcut) and rename directories refactoring is waiting there as you expected.Refactor to rename one or more of these directories to reach your aim. The only thing you should notice here is you must choose the directories rather than Packages when a dialog ask you.
If you've got lots of classes in your project, it will take you a while to wait for its auto-scan-and-rename.
Besides, you need to rename the package name inside the AndroidManifest.xml manually, I guess, such that other names in this file can benefit the prefix.
[ALSO], it might need you to replace all
com.hello.world.R
to the newXXX.XXX.XXX.R
(Command+Shift+R
for short)Rebuild and run your project to see whether it work. And use "Find in Path" to find other non-touch names you'd like to rename.
The first part consists of creating a new package under
java
folder and selecting then dragging all your source files from theold package
to thisnew package
. After that you need toremane
the package name in androidmanifest
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 modulebuild.gradle
in your android studio in addition to changing the package name in themanifest
. So in summary, click onbuild.gradle
which is below the "AndroidManifest.xml" and modify the value ofapplicationId
to your new package name.Then, at the very top, under
build
.clean
your project, thenrebuild
. It should be fine from here.ctrl + Shift + R has always worked for me. Simply
replace all
and chooseAll Files
.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 :)
shift+F6
you will get a popup window as shown below select Rename package.That's it done.
Packages serve two purposes. One is to uniquely identify your app in the Google Play Store. The other is to name the package for the
R.java class
which is generated when you build your project. You can think of the first purpose as the external package and the second as the internal package. Assuming you want to change the external package so you can identify in the Play store, there's a convenient way to do this.In Android Studio,
Now, when you build your project, your APK and manifest will use this new package name.