2

我的 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>

我的生产/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>

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

4

1 回答 1

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
}
于 2016-02-18T12:14:57.877 回答