有没有办法根据选定的productFlavor更改gradle变量值?

时间:2016-03-12 09:58:53

标签: android gradle android-gradle build.gradle android-productflavors

以下是我的gradle示例: 我想根据选定的productFlavor动态更改STORE_FILE_PATH值。目前STORE_FILE_PATH总是用最后定义的productFlavor覆盖它的值。 (在我的情况下,它总是变成" /pro.jks")

帮我找到解决方案。感谢

def STORE_FILE_PATH = "";

android {
    productFlavors {
        free {

            versionCode 1
            versionName "1.0.0"

            applicationId "com.example.free"

            buildConfigField "boolean", "IS_AD_ENABLED", "true"
            STORE_FILE_PATH = "/free.jks"

        }


        pro {

            versionCode 1
            versionName "1.0.0 pro"

            applicationId "com.example.pro"

            buildConfigField "boolean", "IS_AD_ENABLED", "false"

            STORE_FILE_PATH = "/pro.jks"
        }

    }

    signingConfigs {
        signingConfig {
            keyAlias 'aa'
            keyPassword '123'
            storeFile file (STORE_FILE_PATH)
            storePassword '123'
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该定义多个signingConfig,并在不同的productFlavors中使用

signingConfigs{
   freeKey{}
   proKey{}
}

productFlavors {
    free {

        versionCode 1
        versionName "1.0.0"

        applicationId "com.example.free"

        buildConfigField "boolean", "IS_AD_ENABLED", "true"
        signingConfig signingConfigs.freeKey
    }


    pro {

        versionCode 1
        versionName "1.0.0 pro"

        applicationId "com.example.pro"

        buildConfigField "boolean", "IS_AD_ENABLED", "false"

        signingConfig signingConfigs.proKey
    }

}