Android Package Name vs Application ID
As an Android Developer, you should know that the Package Name that you choose for your app is very important. We are referring to the Package Name of the application which we declare in the Manifest.xml rather than the Java package name (although they will be identical). But there is a difference between the Package Name and the Application ID. In this article, we’ll look at the difference between the two.
Every Android app has a unique application ID that looks like a Java package name, such as com.example.myapplication. This ID uniquely identifies your app in Google Play Store. If you want to upload a new version of your app, the application ID (and the certificate you sign it with) must be the same as the original APK—if you change the application ID, Google Play Store treats the APK as a completely different app. Your application ID is defined with the applicationId property in your module’s build.gradle file, as shown below:
The applicationId exactly matches the Java-style package name you chose during project setup in android studio. However, the application ID and package name are independent of each other beyond this point. You can change your code’s package name (your code namespace) and it will not affect the application ID, and vice versa (though, again, you should not change your application ID once you publish your app). However, changing the package name has other consequences you should be aware of, so see the section about how to change the package name.
Note: Context.getPackageName() method returns your application ID.
Use of Application Id
For a large project, the cost of modifying the package name is too high and almost undesirable. And often there will be different versions of a project, such as paid version and free version. In this way, the package name of the final apk can be changed by simply modifying the applicationId in app/build.gradle, so that it is a different app for Android phones. At the same time, it does not affect the code consistency during development and compilation.
The package name is defined in AndroidManifest.xml at the beginning of the creation of an Android application project. The package name is just to organize your code. Although your project’s package name matches the application ID by default, you can change it. However, if you want to change your package name, be aware that the package name (as defined by your project directory structure) should always match the package attribute in the AndroidManifest.xml file as shown below.
The Android build tools use the package attribute for two things:
- It uses this name as the namespace for your app’s generated R.java class.
- It uses it to resolve any relative class names that are declared in the manifest file.
Example: With the above manifest, an activity declared as <activity android:name=”.MainActivity”> it will actually be parsed as com.example.myapplication.MainActivity.
Note: package attribute should always match your project’s base package name where you keep your activities and other app code.