Most developers learn android development through the use of tutorials in form of videos on YouTube or articles in google. This makes the developer either follow word by word from the tutorial and may end up causing problems to the code in the later stages
The code that the developers learn from android contains a package name that the readers of the tutorial may also use in the project he is creating meaning that the package name is not unique
While publishing applications in the google play store, the package name must be unique and must not be the example domain that is commonly used, therefore you must ensure that the package name is not similar to any other.
If in any way, the package name is found in the play store already used by another app, you must change or rename the package name so that it can be accepted by google play store.
What is a package name in android
A package name in android is that name that uniquely identifies your application on your device and in the google play store.
To use a familiar term, for example, in web development, a package name can be related to a domain name
Every app must have a unique package name despite being from the same publisher or developer
An example of a package name is com.example.myapplication
In the android studio, every activity, either java or kotlin, starts with the package name at the top
For example, view the image below
In this article, we shall discuss how to change or rename package name in android by using the various ways that enable you to do this
Rename Package name while you are creating the project
Firstly, the package name can be set when you are creating your project, that is while assigning the name of your project, you should also change the name of the package as the default is com.example and it is not accepted by google
To create a new project and change the package name, view the two screenshots below
In the next screen, the default name of the project is MyApplication, change it to the desired name of your application
The package name by default is com.example.(projectname)
When you change the name of the project, automatically the suffix name of the package name will change
What you need to do is replace com.example with your website domain name if the app has a registered domain
For this case we shall replace com.example with com.solutionspace
The project name is PackageNameProject, which means the package name is com.solutionspace.packagenameproject
You are now assured that the package name is unique
Rename package name in an already existing project
The next way of renaming or changing the package name is when already the project exists in android studio such that, you do not want to create another project but you wish to rename the package name
- Open the build Gradle for module
In the default config, change the applicationId to your desired name
The full code for the build.gradle will be as follows
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 31
defaultConfig {
applicationId "com.solutionspace.changepackagename" //the updated package name
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Please note that if you are changing the package name after being denied access by play store, you will need also the update the version code and version name to the next available values
- Click on sync at the alert text that will appear on the top of the screen and let the gradle update your project
- Next, open the android manifest file and update the package name to be the same in the gradle file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solutionspace.changepackagename">
You will notice the activities in the manifest file will turn out to have errors
- Finally, you will need to open every activity .kt file and update the package name also
Once it’s done, the package name will be updated to the name you have just used and that’s how you can update the package name if already a project has been created