Summary of the difference between Android applicationId and package name

  • 2021-11-10 10:42:37
  • OfStack

Applying ID to Difference of Package Names

Every Android application has a 1-only application ID. In Android devices and markets, this ID is the 1-only identity of your application. To update the application in the market, the new application ID must match the original apk application ID1. Therefore, once the application is released, the application ID cannot be changed.

There is no concept of applicationId in Eclipse, and applicationId is equivalent to the package name in Eclipse. But in Android Studio, these two concepts are distinguished. The package name is defined in the manifest file:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.example.myapplicationtest" >

The application ID is defined in build. gradle of the moudle layer, and the applicationId value is the application ID, as follows:


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

However, when you create a new project in Android Studio, applicationId defaults to the package name of the project. Therefore, developers often confuse the two and think 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 of ID, and vice versa.

Usually, the application of Android ID is bound to the package name, so in Android API, some methods and parameters seem to return the package name from the name, but in fact they return the application ID value. For example, Context.getPackageName() Method returns the application ID, not the package name. If you don't believe me, let's look at it:

In the figure, the package name is: com.example.myapplicationtest Applying ID: com.example.myapplicationtest.id

Call the following code in MainActivity:


String applicationId = this.getPackageName();Log.i("TESTKIM","applicationId:" + applicationId);

Print the log file as follows:

The naming of ID is not arbitrary, and it needs to follow the following restrictions at least: The application of ID consists of at least two parts (that is to say, there is at least one point, such as com. example); Each part must begin with a letter; All characters must be alphanumeric or underscore [a-zA-Z0-9_]

Note:

If you use webview, please use the package name as the prefix of applying ID, otherwise, an error may be reported.

Use of ID

The application of ID is only identified by 1, so what use can it be in the development process?

Imagine 1, when we develop an application, we have 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, as long as the development version of the application ID and the release version are not the same. Therefore, you only need to modify the development version of ID in buildTypes. As follows:


android {
...
buildTypes {
  debug {
    applicationIdSuffix ".debug"  // Equivalent to " com.example.myapplicationtest.debug " 
  }
}
}

In the above code, ". debug" is added to the application ID of debug version after the original application ID. applicationIdSuffix represents the default application ID, which is the value of applicationId in defaultConfig. Therefore, the application of ID version of debug is "com. example. myapplicationtest. debug".

In addition, sometimes we want different versions of the app to be released to the market, such as free version and paid version. This requires us to build different application variants. Then we can make corresponding configuration in productFlavors to generate different applications. Such as:


android {
defaultConfig {
  applicationId "com.example.myapplicationtest"
}
productFlavors {
  free {
    applicationIdSuffix ".free"
  }
  paid {
    applicationIdSuffix ".paid"
  }
}
}

In the above code, we use "free" for the free version and "paid" for the paid version. In productFlavors, different applications are generated by configuring different applications ID. Eventually, both applications apk can exist in the market at the same time.

Modify package name

By default, the package name is the same as the application ID. Of course, the developer can also modify the package name. If the developer wants to modify the package name, note that the project directory structure must match the package name 1 of the package attribute in AndroidManifest. xml.


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

The package value serves two purposes:

It provides a namespace for the R. java file, for example, the package name for R class is com.example.myappcationtest.R

Determines the relative name of class declared in manifest. For example, the real path declared in manifest is: com.example.myapplicationtest.ManiActivity

If the developer wants to modify the package name, he must ensure that the package value in manifest is also modified synchronously.

Summarize


Related articles: