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

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

下面是我的 gradle 示例: 我想根据所选的 productFlavors 动态更改 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'
        }
    }
}

最佳答案

您应该定义多个 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
    }

}

关于android - 有没有办法根据选定的 productFlavor 更改 gradle 变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956182/

相关文章:

android - 如何取消从 AsyncTask 调用的 HTTP GET 请求?

Android:了解APK安装过程

Android Studio gradle 从服务器收到状态代码 400 : Bad Request

ubuntu - 如何在 ubuntu 服务器上安装 Flutter SDK

gradle - 无法获取类型对象的未知属性 'cppFlags'

android - readParcelable 返回 null

android - 如何在 UML 类图中包含 Android Activity 和屏幕?

java - Android Update 3后Android资源链接失败

android - 如何修复 android studio 同步 Gradle 时出现的 'Cannot invoke method buildTypes()' 错误?

android - 如何仅为特定的 buildType 覆盖 gradle 任务