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
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:
build.gradle
file and change the Application Id in defaultConfig to the same name as in manifest and rebuild the project.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".
I tried the two top-voted solutions but found some issues even though both work to some extent.
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.
Please try the following steps:
*******Finally it’s done ******
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:)
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.
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.