Many new developers will find it hard to rename a package. By default, package names are compacted in Android Studio, and that’s the reason why many can’t find a way to rename them.
- Open the project in Android Studio and navigate to the package you wish to rename. Right-click on the package and select “Refactor” from the context menu. From the “Refactor” menu, select “Rename”. You will be prompted to enter a new name for the package. Enter the new name and click “Refactor”.
- Update the manifest file. Open the manifest file and change the package name to the new name. This will ensure that the application is now using the new package name.
- You will need to update any references to the old package name in your code. Use the “Find in Path” feature to search for any references to the old package name and update them to the new package name. Once all references have been updated, you can save and close the project. Your package has now been renamed and is ready to be used in your project.
Step to refactor package renaming
1) Disable name compact mode
On the Project tab, click on the Gear icon to open a new menu then uncheck the “Compact Empty Middle Packages” menu item.
Now all packages will be listed in the hierarchy.
2) Refactor name
Right-click on the package you want to rename, select Refactor then Rename (You can use Shift + F6 shortcut to quickly access name refactor).
When a new popup menu appears, choose Rename Package then click on Refactor button.
3) Change package name in build.gradle
If Android Studio notifies an error in
buld.gradle, open it, and change the package name to a new one.