AndroidManifest中的包名与Android中的flavors冲突

5

我将尝试制作我的应用的两个版本。

Gradle:

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

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

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

fistapp文件夹中的包名是:be.myname.fistapp,secondapp文件夹中的包名是: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"
        }

我有点困惑。这个问题应该怎样正确处理呢? :)
1个回答

4

你不需要为每个flavor在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.


那么在这里你应该使用什么:<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="${packageId}"> - Robin Manoli
似乎所有的 AndroidManifest.xml 文件中应该有相同的值。 - Robin Manoli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接