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

时间:2016-02-18 12:09:42

标签: 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,我也无法更改包名。我怀疑我在主清单中提到的包名是以某种方式覆盖build.gradle中的applicationId。如果有人指出我正确的方向,我会很感激。感谢。

1 个答案:

答案 0 :(得分:1)

不是在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
}