Android official technical document translation--applicationid and PackageName

Source: Internet
Author: User

This article is translated from the official technical document Androd ApplicationID versus PackageName ", the original address: Http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename.

This article address: http://blog.csdn.net/maosidiaoxian/article/details/41719357. Reprint please indicate the source. Please correct me if there is any inappropriateness in the translation.


ApplicationID and PackageNameAll Android apps have a package name. The package name is the unique identifier of the app on the device and is the only logo on the Google Play store. This means that once you have published a program that uses this package name,you'll never be able to change it .Otherwise, your application will be treated as a brand new application, and the user of your previous application won't see the installation package as an update.
In the previous Android Gradle build system, the package name of your application is determined by the Packages property 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 also has a second purpose: it is used to name the package of your resource class (and to parse any related activity's class name). In the example above, the generated R class will becom.example.my.app.R, so if the code inside your other package needs to reference these resources, you need to importCOM.EXAMPLE.MY.APP.R.
With the new Android Gradle build system, you can easily build multiple differentversionApplication, for example, you can build a "free" version and a "pro" version of the application (by using flavors), and these different versions of the program should have different packages on the Google Play store so that they can be installed and purchased separately, or install two simultaneously, Wait a minute. Similarly, you can create "debug", "Alpha", and "beta" versions of applications at the same time (using build types), and these versions of programs can also use a unique package name.
At the same time, the R class you want to import in your code must remain the same for that period, and your. Java source files should not be changed when you are building different versions of your application.
Therefore, wedecouplingTwo uses of the package name:
    • The final solution is in the manifest of your generated. APK and is used to identify your app's package on your device and the Google Play store, called the "Application ID".
    • Used to refer to your R class in the source code, and to resolve any related Activity/service registered packages, continues to be 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 package for the code in the Manifest file, as in the above Android?? Oidmanifest.xml example.
Here's the most critical part:when you do it like above, the two packages are independent of each other. You are completely free to refactor your code--change the internal packages used for activity and service, update the packages in your manifest, and refactor your import statements. This will not affect the final ID of your program, this final ID is always the value of the 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 graphically make these configurations in the Project Structure dialog box.) )
Note: For compatibility reasons, if youNoDefining ApplicationID in your Build.gradle file, this applicationid will default to the same value specified in Androidmanifest.xml. In this case, the two are obviously not decoupled, and if you try to refactor your code it will also accidentally change the ID of your application! In Android Studio, the new project will always specify both values.
NOTE 2: The package name must always be specified in the default Androidmanifest.xml file. If you have more than one manifest (such as a flavor specific manifest or a buildtype specific manifest), the package name is optional, but if it is specified, it must be exactly the same as the package specified in the main manifest.


Android official technical document translation--applicationid and PackageName

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.