Creating multiple APKs for different purposes.
PackageName vs ApplicationId
Nowadays, many times we come to the situation that we need the APK with another different package name. Most of us do it easily, but sometimes we got stuck because of applicationId and packageName.We must know the difference between packageName and applicationId. And the other thing is Java package.
The following are the three which keeps us confusing:
- applicationId: BuildConfig.APPLICATION_ID
- packageName: getApplicationContext().getPackageName()
- Java package: BuildConfig.class.getPackage().toString()
Let’s see with an example
The following is the snippet from the gradle of a sample Android application.
Here we will be having two different APKs.
- Release APK with com.mindorks.example.release
- Debug APK with com.mindorks.example.debug
The following is the snippet from manifest of the same sample Android application.
The following is the project package snippet of the same sample Android application.
So, let’s create a debug APK and see what are the values of all the three.
Read the values carefully.
The following shows the final AndroidManifest file after APK creation.
getPackageName gives the same applicationId which is created at the final moment from the gradle file and it overrides the AndroidManifest package. So the final AndroidManifest contains the same applicationId. So, the getPackageName is the same as the applicationId as the applicationId overrides the packageName in the AndroidManifest at the final moment.
But for the Java code, the package is same as the project structure. The package that is used in your source code to refer to your R class, and to resolve any relative activity, service registrations, continues to be called the package as defined in your manifest. So, the AndroidManifest should have the package same as Java package to resolve relative activity, service.
So, with the same java package, we can create any number of APKs with all unique applicationId.
But the final AndroidManifest contains the package as the unique applicationId only.
If you have to actually change the structure of your project, then you have to change your packageName in Manifest.xml.
If you rename the package name from manifest file, it will have NO impact on the applicationId even if they have the same name.
We can also create multiple APKs through productFlavors like above.
Remember, once an app has been published to Google Play store, the ApplicationId should never be changed.
So, the applicationId matters.
Happy Coding 🙂