【问题标题】:Android configuration specific to each combination of 2 flavor dimensions特定于 2 个风味维度的每种组合的 Android 配置
【发布时间】:2021-02-09 14:46:24
【问题描述】:

我正在尝试向现有 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 占位符没有集成在我的项目清单中,而是在 openId Appauth 依赖项中,所以我不能只拥有几个不同风格的清单像我在其他地方读过的目录。

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

【问题讨论】:

  • 我不确定我是否完全理解您想要实现的目标。您是否希望 appAuthRedirectScheme 反映您的应用的最终 applicationId?在这种情况下,您只需在清单文件中使用 ${applicationId} 预定义占位符。如果您想要为每个维度组合提供非常具体的值,那么您应该定义一个包含 6 组值的单一维度,而不是 2 个维度。
  • @BladeCoder 我希望 appAuthRedirectScheme 特定于每个 env/variant 组合。按照惯例,它对应于 applicationId 但它可以是任何其他特定值。使用 ${applicationId} 占位符并不容易,因为需要自定义的清单元素来自库,所以我必须使用清单合并规则等,感觉很脆弱。我猜一维有 6 种口味的技术是最简单的,即使这意味着一点点复制。我只是希望 Gradle 允许我设置特定于风味的配置

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


【解决方案1】:

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

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") { ... }
}

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多