Is there a way to change gradle variable value based on selected productFlavor?

喜你入骨 提交于 2019-12-02 00:37:30

问题


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
    }

}


来源:https://stackoverflow.com/questions/35956182/is-there-a-way-to-change-gradle-variable-value-based-on-selected-productflavor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!