android - 处理产品口味时,main/AndroidManifest.xml 中的包名称应该是什么?

标签 android xml build.gradle android-productflavors package-name

我的 build.gradle 中有两种产品风格,如下所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.2"
    aaptOptions.setProperty("cruncherEnabled", false)

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 16
        applicationId "com.example.app"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    def final myApplicationId = 'com.example.app'
    def final appName = 'app'

    productFlavors {
        dev {

            resValue "string", "app_name", appName
            resValue "string", "APIURL", "http://prod.example.com/"
        }
        production {

            resValue "string", "app_name", appName
            resValue "string", "APIURL", "http://test.example.com/"
        }
    }
    buildTypes{
        dtest {
            applicationIdSuffix ".dev"
        }
        dprod{

        }
    }
}



dependencies {
    compile()
    few libraries
}

我的dev/AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app.dev"
    android:installLocation="auto"
    android:versionCode="3"
    android:versionName="3.1">

</manifest>

而我的production/AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:installLocation="auto"
    android:versionCode="3"
    android:versionName="3.1">

</manifest>

我的main/AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:installLocation="auto"
    android:versionCode="3"
    android:versionName="3.1">
    <application>
          <activity list.. </activity>
    </application>
</manifest>

我想在同一台设备上安装 dev 和 production flavor,但即使我设置了不同的 applicationId,我也无法更改包名称。我怀疑我在主 list 中提到的包名称以某种方式覆盖了 build.gradle 中的 applicationId。如果有人指出我正确的方向,我将不胜感激。谢谢。

最佳答案

与其在 productFlavors 中设置,不如在 buildTypes 中添加它

使用

buildTypes {
    debug {
        applicationIdSuffix ".dev"
        // Rest of the code block
    }

    release {
        // your code block
    }
}

更新

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.2"
    aaptOptions.setProperty("cruncherEnabled", false)

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 16
        applicationId "com.example.app"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    buildTypes {
        debug{
            applicationIdSuffix ".dev"
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    productFlavors {
        dev {

            resValue "string", "app_name", appName
            resValue "string", "APIURL", "http://prod.example.com/"
        }
        production {

            resValue "string", "app_name", appName
            resValue "string", "APIURL", "http://test.example.com/"
        }
    }
}



dependencies {
    compile()
    few libraries
}

关于android - 处理产品口味时,main/AndroidManifest.xml 中的包名称应该是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35481113/

相关文章:

在 ProcessResources 和过滤器中循环 Gradle

android - NotificationCompat 和 setFullScreenIntent()

android - 修复圆形 ViewPager 的动画

java - 设置网站以与Android手机同步数据

java - maven pom.xml 文件无法识别 Jersey 框架

java - 我无法将自定义微调器 XML 调用到我的 mainactivity.java 中

xml - 使用 dtd 时 clojure xml 解析缓慢

java - 改造 GSON 将日期从 json 字符串序列化为 long 或 java.lang.Long

android - 无法在 Android Studio 2.3 中使用注释处理器

Scala 不是基于 Java 10 构建的