在build.gradle

时间:2016-12-01 12:49:01

标签: android android-gradle

我的AndroidManifest.xml文件中有一些明显的占位符

build.gradle中,我想根据我的风格和构建类型动态填充这些值。

我该怎么做?

我写了一个执行以下操作的函数

def getKey() {
    def KeyToReturn = ""
    android.applicationVariants.all { variant ->
        printout("getKey: ${variant.name}")
        def flavor = "default";
        if (variant.productFlavors.size() > 0)
            flavor = variant.productFlavors.get(0);


        def buildType = variant.buildType.name

        if (buildType == "debug" || buildType == "staging") {
            if (flavor.name == "one") {
                KeyToReturn = test_key_1
            }
            if (flavor.name == "two") {
                KeyToReturn = test_key_2
            }

        }
        if (buildType == "release") {
            if (flavor.name == "one") {
                KeyToReturn = live_key_1
            }
            if (flavor.name == "two") {
                KeyToReturn = live_key_2
            }
        }
    }
    printout("KeyToReturn:" + KeyToReturn)
    return KeyToReturn
}

我在android.defaultConfig中有这个

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 23
    versionCode getVersionCode1()
    versionName getVersionName1() + ""
    manifestPlaceholders = [key: getKey()]
}

这是我的AndroidManifest.xml相关部分包含的内容

<meta-data
            android:name="key"
            android:value="${key}"/>

问题是当我查看构建的AndroidManifest.xml文件时 $ {key}值是一个空字符串。

如何正确填充此值?

1 个答案:

答案 0 :(得分:5)

您可以根据产品风格(和AFAIK构建类型)定义清单占位符,方法是将它们添加到正确的闭包中。

this sample app中,我使用产品风格为Android 7.0的network security configuration使用不同的规则:

productFlavors {
    thawte {
        resValue "string", "app_name", "CA Validation Demo"
        applicationId "com.commonsware.android.downloader.ca.thawte"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_thawte']
        buildConfigField "String", "URL", WARES
    }
    verisign {
        resValue "string", "app_name", "Invalid CA Validation Demo"
        applicationId "com.commonsware.android.downloader.ca.verisign"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_verisign']
        buildConfigField "String", "URL", WARES
    }
    system {
        resValue "string", "app_name", "System CA Validation Demo"
        applicationId "com.commonsware.android.downloader.ca.system"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_verisign_system']
        buildConfigField "String", "URL", WARES
    }
    pin {
        resValue "string", "app_name", "Cert Pin Demo"
        applicationId "com.commonsware.android.downloader.ca.pin"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_pin']
        buildConfigField "String", "URL", WARES
    }
    invalidPin {
        resValue "string", "app_name", "Cert Pin Demo"
        applicationId "com.commonsware.android.downloader.ca.invalidpin"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_invalid_pin']
        buildConfigField "String", "URL", WARES
    }
    selfSigned {
        resValue "string", "app_name", "Self-Signed Demo"
        applicationId "com.commonsware.android.downloader.ca.ss"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_selfsigned']
        buildConfigField "String", "URL", SELFSIGNED
    }
    override {
        resValue "string", "app_name", "Debug Override Demo"
        applicationId "com.commonsware.android.downloader.ca.debug"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_override']
        buildConfigField "String", "URL", SELFSIGNED
    }
}

然后我可以正常引用清单中的占位符:

<application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:networkSecurityConfig="@xml/${networkSecurityConfig}">