-->

Is there a way to change gradle variable value bas

2019-02-25 08:32发布

问题:

Below is my gradle example: I want to change STORE_FILE_PATH value dynamically based on selected productFlavors. Currently STORE_FILE_PATH is always overwriting it's value with last defined productFlavor. (In my case it always becomes "/pro.jks")

Help me find solution. Thanks

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:

You should define multiple signingConfigs and use it in different 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
    }

}