How-To : Change the Package Name in Android Studio
While developing any Android application, generally it is the tendency of Android app developer to keep the PackageName as it is and not to change it. But at times, while uploading an application on Play store or due to some other constraints there could be a need to change the PackageName of Android application or some packages in the application.
Changing PackageName is a crucial part of any Android application as many of the core libraries and R.java file depends on the package name. Also, changing the package name was bit easy task for the Eclipse users, but this might not be the same in Android Studio as the package level names are not displayed in Eclipse style. i.e. In Eclipse, the packages are displayed with full parent hierarchy, but in Android Studio, it is displayed in a folder level hierarchy, so sometimes it looks bit difficult to change the PackageName.
So in this article, I have tried to share some important guidelines to make this process easier. This is already documented in the Android Studio guide which is so huge that most of the time it is difficult to read everything of it. The purpose of sharing this feature separately is to highlight the steps, so it is easy for all Android developers to quickly change the PackageName when needed.
Below are the steps for changing the PackageName in Android studio.
- Lets say, I want to change package name com.mycompanyname1.data to com.mycompanyname.data.
- Click setting icon from Project explorer and check the Flatten package option.
- Now again click on setting option and uncheck Hide Empty Middle Packages.
- So now package name gets divided in different sub-package like com, com.mycompanyname1, com.mycompanyname1.data under java directory.
- Right click on com.mycompanyname1 package name and click option Refactor->Rename option (Alt+Shift+R) then rename packagename dialog box opens up, just change the package name as you want. For current example, we changed mycompanyname1 to mycompanyname.
- Click on Refactor button in dialog box.
- Click on Do Refactor at bottom.
- Android studio automatically changes package name in whole application except build.gradle.
- Open build.gradle file under application, rename package name manually.
- Now, again click on setting option and uncheck Flatten packages option.
- Sync the application again and the code will be refactored according to new package.
Hope this makes your life more easy now, to change the package name during Android app development.