Official Android technical documentation-ApplicationId and PackageName
This article is translated from the official androd technical document ApplicationId versus PackageName. Original address: http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename.
URL: http://blog.csdn.net/maosidiaoxian/article/details/42619357. Indicate the source for reprinting. If any translation error occurs, please correct it.
All Android applications of ApplicationId and PackageName have a package name. The package name is the unique identifier of the app on the device and the unique identifier on the Google Play Store. This means that once your published program uses this package name,
You can never change it.Otherwise, your application will be treated as a brand new application, and users of your previous application will not see it as the updated installation package.
In the previous Android Gradle build system, the package name of your application is determined by the package attribute in the root element of your manifest file:
AndroidManifest. xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my.app"
android:versionCode="1"
android:versionName="1.0" >
However, the package defined here has a second purpose: it is used to name your resource class package (and to parse the class name of any related Activity ). In the above example, the generated R class will be
com.example.my.app.R
Therefore, if the code in your other package needs to reference these resources, you need to import com. example. my. app. R.
Using the new Android Gradle build system, you can easily build multiple
VersionFor example, you can build an application of the "free" and "pro" versions (by using flavors ), in addition, these different versions of the program should have different packages on the Google Play store, so that they can be separately installed and purchased, or installed at the same time, and so on. Similarly, you can also create "debug", "alpha", and "beta" applications (using build types) at the same time ), programs of these versions can also use a unique package name.
At the same time, the R class you want to import in the Code must remain unchanged during this period of time. When you are building different versions of your application, your. the java source file should not be changed.
Therefore, we
DecouplingPackage name usage:
- The final solution is to identify your app package on your device and Google Play store in the manifest of your generated .apk, which is called "application id ".
- It is used to reference your R class in the source code, and is used to parse any related Activity/Service registered package. It is still called "package ".
You can specify the application id in your gradle file as follows:
App/build. gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1"
defaultConfig {
applicationId "com.example.my.app"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
...
As before, you need to specify the packages for code in the Manifest file, just like the AndroidManifest. xml example above.
The most important part is as follows:When you do the same as above, the two packages are independent of each other. You are free to refactor your code-change the internal package for Activity and Service, update the package in your Manifest, and refactor your Import Statement. This will not affect the final id of your program. This final id is always the value of applicationId specified in your Gradle file.
You can change the applicationId of your application for different flavors and build types by using the following Gradle DSL method:
App/build. gradle:
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "
com.example.my.pkg
.free"
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
....
(In Android Studio, you can also perform these configurations graphically in the project structure dialog box .)
Note: For compatibility reasons, if you
NoDefine applicationId in your build. gradle file. This applicationId will be the same value specified in AndroidManifest. xml by default. In this case, the two are obviously not decoupled, and if you try to refactor your code, it will also change your application id in other places! In Android Studio, these two values are always specified for the newly created Project.
Note 2: The package name must always be specified in the default AndroidManifest. xml file. If you have multiple manifest (for example, a specific manifest of flavor or a specific manifest of buildType), the package name is optional, but if it is specified, it must be exactly the same as the package specified in the main manifest.