The relationship and difference between Android package attribute, package name and Application ID

Glossary

  1. package attribute: in the AndroidManifest.xml file.
  2. package name: The package name of the module structure.
  3. Application ID: The applicationId property under the module defaultConfig block.

Set Application ID

Every Android application has a unique Application ID similar to the Java package name, such as com.example.myapp. On Android devices and the Google Play Store, the Application ID is your app's unique identifier. If you want to upload a new version of the application, the Application ID must be the same as the original one. If you change the Application ID of a new version of your app, the Google app store will see it as a completely different app. So, from the first time you upload your application, never change the Application ID .

The Application ID is defined in the applicationId property of the module build.gradle as follows:

android {
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    ...
}

When you create a new project in Android Studio, you can set the applicationId property so that the Application ID and package name match exactly. Other than that, the two are completely independent. Of course, you can change the package name, but this will not affect the Application ID. And vice versa (again: after you upload your app, don't modify the Application ID, don't modify the Application ID, don't modify the Application ID). However, you should be aware that modifying the package name has another consequence. See this part for details on modifying the package name (described below).

Although Application IDs look similar to traditional Java package names, the naming conventions for Application IDs are more restrictive:

  • At least two paragraphs (at least one. separated)
  • Each paragraph must start with a letter
  • All characters can only be letters, numbers and underscores

Note: Previously, the Application ID was directly bound to the package name. So, some Android APIs use "package name" in the method name or parameter name, but it actually refers to the Application ID. For example, the Context.getPackageName()method returns the Application ID. So there is no need to share the real package name outside of your application code.

Warning: If you use WebView, consider prefixing your Application ID with your package name, otherwise, you may hit issue 211768 .

Modify the Application ID of the build

When building an APK for your application, the build tools use the Application ID defined in the defaultConfig block in the build.gradle file to identify the APK (shown below). However, if you want to create different versions of the app and display different information in the Google Play Store, such as "Free" and "Pro". You need to build different versions with different Application IDs.

In this case, each build should define a different product flavor, and each flavor is inside the productFlavors{} block. For each flavor, you can redefine the applicationId property, or prefix and suffix the default applicationId as follows:

android {
    defaultConfig {
        applicationId "com.example.myapp"
    }
    productFlavors {
        free {
            applicationIdSuffix ".free"
        }
        pro {
            applicationIdSuffix ".pro"
        }
    }
}

After this definition, the Application ID of "free" is "com.example.myapp.free".

On build types, you can also use suffixes like this:

android {
    ...
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
}

Since Gradle first applies the product flavor before applying the build type, the "free debug" version now has an Application ID of "com.example.myapp.free.debug". This is useful if you want to have both debug and release on the same phone, since no two apps can have the same Application ID.

Keep in mind that if the same application has different Application IDs, the Google Play Store will consider it to be two applications. So, if you want to distribute multiple applications with the same information in order to adapt to different device configurations (eg different API levels). Then for each version you have to use the same Application ID and a different versionCode.

Warning: For compatibility with previous SDK tools, if you do not define the applicationId property in build.gradle, the build tools will use the package property in AndroidManifest.xml as the Application ID. In this case, renaming the package name means also renaming the Application ID.

Tip: If you need to reference the Application ID in the manifest file, you can use the ${applicationId} placeholder in the manifest properties. Gradle will automatically replace this ID with the real Application ID when building. See Inject Build Variables into the Manifest for more details .

Test-specific Application ID

By default, the build tools apply the Application ID to your instrumentation test APK using the Application ID of the specified build (with .test appended). For example, the real Application ID of a test application with build "com.example.myapp.free" is "com.example.myapp.free.test".

Usually this is not necessary, you can modify the Application ID by defining the testApplicationId property in the defaultConfig or productFlavor block.

Note: To avoid naming conflicts with the test application, the build tools use a namespace for the test application to generate R.class based on the test Application ID.

Modify package name

Although the package name matches the Application ID exactly by default, you can still modify it. If you want to modify the package name, please note that the package name (project directory structure) corresponds exactly to the package attribute in AndroidManifest.xml. As follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

For the Android build tools, the package attribute is used for two things:

  • The namespace to apply to your application's generated R.Java classes.

    For example: in the manifest file above. The R class is "com.example.myapp.R"

  • Relative path used to resolve any classes in the AndroidManifest.xml file.

    For example: in the manifest file above. An activity defined as \ will be resolved to com.example.myapp.MainActivity.

Therefore, the value of the package attribute should always be the same as the project's package name. Of course, you can set the subpackage name for the project. These files must import R classes from the namespace of the package attribute, and any components defined in the manifest must add the subpackage name (or write the full path).

If you need to completely rename the package name, make sure you have updated the package property. These remain in sync automatically until you rename the package with Android studio's tools. (If it is not kept in sync, your code will not resolve the R class correctly because it is no longer under the same package, and the manifest will not correctly identify your activities and other components).

You must set the package attribute at the top of the AndroidManifest.xml file, if you add another manifest file, please note that the package name provided by the highest priority manifest file is always used to merge the final manifest. More: [Merge Multiple Manifest Files.

Hope to know: Although your project can set the package attribute not equal to applicationId, it does not. The build tool will copy the Application ID and set it to the value of your application's unique package attribute during the final build. So if you check the AndroidManifest.xml file after a successful build, don't be surprised that the package attribute changes. On the Android platform and the Google App Store, the package attribute is the only identity for your application. So, once built with the original value (using the namespaced R class to resolve the component in the manifest), the build tool will discard the value of the package attribute and replace it with the Application ID.

Original link: https://developer.android.com/studio/build/application-id.html#change_the_package_name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325543718&siteId=291194637