特定于 2 个 flavor 维度的每种组合的 Android 配置

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

我正在尝试向现有的 Android 项目添加第二个维度。我已经有了针对不同环境(DEV、BETA、PROD)的维度,每个环境都有自己的后端 API 和自己的应用程序 ID(能够在同一设备上安装连接到多个环境的应用程序)。现在,我想为我的应用程序的 2 个变体添加另一个维度,一个具有所有功能的通用变体,一个具有部分功能子集的特定变体。除此之外,我仍然想要保留默认的调试和发布构建类型。

这是我在 build.gradle 中的配置到目前为止的样子:

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.example"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 56
        versionName "1.1.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [
                appAuthRedirectScheme: 'com.example'
        ]
    }
    signingConfigs {...}
    buildTypes {
        debug {...}
        release {...}
    }
    flavorDimensions "env", "variant"
    productFlavors {
        dev {
            dimension "env"
            applicationIdSuffix ".dev"
            resValue 'string', 'backend_url', 'https://dev.example.com/api/v1/'
            manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.dev'
            ]
        }
        beta {
            dimension "env"
            applicationIdSuffix ".beta"
            resValue 'string', 'backend_url', 'https://beta.example.com/api/v1/'
            manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.beta'
            ]
        }
        prod {
            dimension "env"
            resValue 'string', 'backend_url', 'https://example.com/api/v1/'
            manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example'
            ]
        }
        general {
            dimension "variant"
            applicationId "com.example"
        }
        specific {
            dimension "variant"
            applicationId "com.example.specific"
        }
    }
    ...
}

如您所见,通过组合 applicationId 和 applicationIdSuffix,我将能够获得特定于每个环境/变体风格的应用程序变体。

但是我还需要在manifestPlaceholders中拥有相应的映射,因为我知道appAuthRedirectScheme占位符没有集成在我的项目 list 中,而是集成在 openId Appauth 中依赖性,所以我不能像我在其他地方读到的那样,在各种 flavor 目录中只拥有多个 list 。

有没有办法在 build.gradle 中定义特定于每个 flavor 维度组合的build设置?换句话说,我希望 devGeneral、devSpecific、betaGeneral、betaSpecific、prodGeneral 和 prodSpecific 具有不同的 manifestPlaceholders 值。

最佳答案

我并不特别建议这种解决方案,但您可以尝试以下方法:

applicationVariants.all { variant ->
    if(variant.productFlavors.get(0).name == "dev") {
        if (variant.buildType.name == "debug" ) {
            variant.getMergedFlavor().manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.dev'
            ]
        }
        if (variant.buildType.name == "release" ) {
            variant.getMergedFlavor().manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.dev.foo'
            ]
        }
    }
    if(variant.productFlavors.get(0).name == "beta") { ... }
    if(variant.productFlavors.get(0).name == "prod") { ... }
}

关于特定于 2 个 flavor 维度的每种组合的 Android 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66121259/

相关文章:

android - 从 android 移动 sdk 在 aws 中创建帐户

java - 单击按钮时切换 TextView 背景

java - 即使设置可取消为 true 后,自定义对话框也不会取消

java - 从build.gradle从Java类访问变量

android - org.xml.sax.SAXParseException android studio gradle错误

android - 如何避免 Android 中的 GPS 漂移?

java - 如何从 View 类访问数组?

Android Studio 构建失败 - 任务 ':app:dexDebug' 执行失败

linux - AndroidStudio - 错误 - llij.ide.plugins.PluginManager - java.lang.NullPointerException

android - 在旧项目中更新谷歌播放服务