Difference between application ID and package name
Each Android application has a unique application ID. On Android devices and markets, this ID is the unique identifier of your application. If you want to update the application in the market, the ID of the new application must be the same as the original Apk application ID. Therefore, once the application is published, the application ID cannot be changed.
There is no concept of applicationId in Eclipse. In Eclipse, applicationId is equivalent to the package name. But in Android Studio, these two concepts are distinguished. The package name is defined in the manifest file:
<manifest xmlns:andro package="" >
The application ID is defined in the moudle layer, and the applicationId value is the application ID, as shown below:
android { defaultConfig { applicationId "" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" } ... }
However, when creating a new project in Android Studio, the applicationId defaults to the package name of the project. So developers often confuse the two, thinking that they are a concept. In fact, the application ID and the package name are independent of each other. Changing the package name will not affect the application ID, and vice versa.
Usually Android's application ID is bound to the package name, so in the Android API, some methods and parameters seem to return package names from the name, but in fact they return the application ID value. For example,()
The method returns the application ID, not the package name. If you don't believe it, let's see:
In the picture, the package name is:
Application ID:
Call the following code in MainActivity:
String applicationId = ();("TESTKIM","applicationId:" + applicationId);
Print the log file as follows:
- The naming of the application ID is not arbitrary, it must at least follow the following restrictions:
- The application ID contains at least two parts (that is, there is at least one point, such as);
- Each part must begin with a letter;
- All characters must be alphanumeric or underscore [a-zA-Z0-9_]
Notice:
If you use webview, please use the package name as the prefix of the application ID, otherwise, an error may be reported.
Application ID usage
In addition to uniquely identifying the application, what are the uses of the application ID in the development process?
Just imagine, when we develop applications, we need to install the development version and the release version on a test machine at the same time. How can we do it? In fact, it is very simple. Just make the application ID of the development version inconsistent with the release version. Therefore, you only need to modify the application ID of the development version in buildTypes. as follows:
android { ... buildTypes { debug { applicationIdSuffix ".debug" //Equivalent to "" } } }
In the above code, add ".debug" to the debug version's application ID after the original application ID. applicationIdSuffix represents the default application ID, which is the value of applicationId in defaultConfig. Therefore, the application ID of the debug version is: "".
In addition, sometimes we want different versions of the applications we publish to the market, such as: free version and paid version. This requires us to build different application variants. Then we can make corresponding configurations in productFlavors to generate different applications. like:
android { defaultConfig { applicationId "" } productFlavors { free { applicationIdSuffix ".free" } paid { applicationIdSuffix ".paid" } } }
In the above code, we use “free” to represent the free version and “paid” to represent the paid version. In productFlavors, different applications are finally generated by configuring different application IDs. Finally, these two application apks can exist in the market at the same time.
Modify the package name
By default, the package name and the application ID are the same. Of course, developers can also modify the package name. If developers want to modify the package name, please note that the project directory structure must be consistent with the package name of the package attribute in the package.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0" >
The package value has two functions:
It provides a namespace for files, for example, the package name of R class is
Determines the relative name of the class declared in manifest. For example: The real path declared in manifest is:
If the developer wants to modify the package name, he must ensure that the package value in the manifest is also modified synchronously.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.