An Android application is written using Java or Kotlin (which compiles to Java for Android apps)
The package system is a folder that groups together relevant files. It makes the structure of an Android project.
A newly generated Android application usually has one package under the src/main/java
folder. The package name follows the name you specify in the New Project wizard.
For example, the Android app below uses the package name com.example.newapp
:
But when you try to change the package name through the Refactor > Rename… menu, you’ll find that you can only change the newapp
part of the package com.example.newapp
.
See the example below:
To completely change the three parts package name in Android Studio, you need to separate the compressed package name first.
In your Android Studio, click the gear icon ⚙ on the right side of the project view, then uncheck the Compact Middle Packages option:
You’ll see the package names being separated into their own directories, like how it appears in the file explorer:
Now you should be able to rename the com
and example
parts of the package name one by one.
Right-click on the package name you want to change and use the Refactor > Rename… option again.
Select Rename all when prompted by Android Studio.
In the following example, the package name has been changed to org.metapx.myapp
And that’s how you change a package name in Android Studio. You can select the Compact Middle Packages option again to shorten the package in the sidebar.
If you are changing the main package name generated by Android Studio, then there are two optional changes that you can do for your application.
First, open the AndroidManifest.xml
file in the manifests/
folder.
Change the package name of the <manifest>
tag as shown below:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newapp">
Next, open the build.gradle
file in your app
module.
It’s the second one under the Gradle Scripts menu as shown below:
You can change the applicationId
property as shown below:
android {
compileSdk 31
defaultConfig {
applicationId "com.example.newapp"
minSdk 21
targetSdk 31
// ...
}
}
These two changes are optional because the application ID and your package name have been decoupled in the latest Android framework.
In the past, the applicationId
and the manifest’s package
attribute must be changed accordingly.
And that’s how you change the package name from Android Studio. You can use the method above to change any package name you have in your project.