Android清单包名称与 Android 中的风格冲突

AndroidManifest package name conflict with flavors in Android

我正在尝试为我的应用制作 2 种风格。

Gradle:

defaultConfig {
        applicationId "be.myname.firstapp"
        minSdkVersion 15
        targetSdkVersion 23 }

productFlavors {
        raterate {
            applicationId = "be.myname.firstapp"
        }
        scarsforlife {
            applicationId = "be.myname.secondapp"
        }
    }

我在主文件夹、fistapp 文件夹和 secondapp 文件夹中有一个 AndroidManifest。

第一个文件夹中的包名:be.myname.fistapp,第二个文件夹:be.myname.secondapp。

在主文件夹中,我首先拥有:be.myname.firstapp,但与 be.myname.secondapp 冲突。现在我尝试了:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="${packageId}">

</manifest>

得到这个:

Attribute manifest@package at AndroidManifest.xml:3:5-27 requires a placeholder substitution but no value for <packageId> is provided.
AndroidManifest.xml Error:
    Overlay manifest:package attribute declared at AndroidManifest.xml:3:5-45 value=(be.myname.secondapp)
    has a different value=(be.myname.secondapp) declared in main manifest at AndroidManifest.xml:3:5-27
    Suggestion: remove the overlay declaration at AndroidManifest.xml   and place it in the build.gradle:
        flavorName {
            applicationId = "be.myname.secondapp"
        }

我现在很困惑。这样做的正确方法是什么? :)

您不需要为每个口味修改 AndroidManifest.xml 中的包。

引用自tools.android.com

Therefore, we have decoupled the two usages of package name:

  • The final package that is used in your built .apk's manifest, and is the package your app is known as on your device and in the Google Play store, is the "application id".
  • The package that is used in your source code to refer to your R class, and to resolve any relative activity/service registrations, continues to be called the "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 used for your code in the Manifest file, just as shown in the above AndroidManifest.xml sample.

Here comes the critical part: When you've done the above, the two packages are independent. You are completely free to refactor your code - changing the internal package used for your activities and services, updating your Manifest package, and refactoring your import statements. This will have no bearing on the final id of your application, which is now always going to be the applicationId specified in the Gradle file.