在 Android 上使用不同密钥的 Gradle 签名风格 [英] Gradle signing flavors with different keys on Android

查看:13
本文介绍了在 Android 上使用不同密钥的 Gradle 签名风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Android 应用程序有多种版本,我希望除一个之外的所有应用程序都使用相同的密钥.有一个需要使用不同的密钥.

I have many flavors of my Android app, and I want all but one to use the same key. There is one that needs to use a different key.

我如何只为 1 种风格的应用覆盖 signingConfig(但在相同的构建类型中,例如发布")?

How do I override the signingConfig for just 1 flavor of the app (but within the same build type e.g. "release")?

  • 我希望所有构建都默认使用主发布配置.
  • 我只想覆盖 1 种风味
  • 我希望能够使用单个 gradlew assembleRelease 命令运行所有发布版本
  • I would like all builds by default to use the main release configuration.
  • I only want to override 1 flavor
  • I want to be able to run all release builds with a single gradlew assembleRelease command

最后一点很重要,因为我目前有超过 120 种不同的口味,而且还在不断增长.为了单独定制每一种口味,需要做很多额外的工作.

This last point is important as I currently have over 120 different flavors and growing. In order to customise every single flavor individually is a lot of extra work.

我尝试过的相关帖子:

从单一构建类型生成使用不同密钥签名的多个构建

  • 这需要为每种风味进行配置
  • 它似乎并没有使用我的自定义 signingConfig 反正

使用 gradle 签署产品风格

  • 接受的解决方案不起作用(对我而言)
  • 根据评论,这可以通过放置buildTypes 里面的 productFlavors 但我不明白如何做到这一点.
  • accepted solution doesn't work (for me)
  • according to a comment this is possible by putting buildTypes inside the productFlavors but I do not understand how to do this.

在 Gradle 产品风味上调试签名配置

总的来说,每个解决方案似乎仍然使用默认的发布配置,而不是我的自定义配置.

Overall, each solution seems to still use the default release config, instead of my custom config.

我的 build.gradle 的重要部分如下所示:

Important parts of my build.gradle look like this:

signingConfigs {
    releaseConfig {
        storeFile file('key')
        storePassword "pass"
        keyAlias "alias"
        keyPassword "pass"
    }

    custom {
        storeFile file('custom_key')
        storePassword "pass"
        keyAlias "alias"
        keyPassword "pass"
    }
}

productFlavors {
    apple {
        applicationId "demo.apple"
    }
    banana {
        applicationId "demo.banana"
    }

    // def customConfig = signingConfigs.custom
    custom {
        applicationId "custom.signed.app"
        // signingConfig customConfig
    }
 }


buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
    release {
         signingConfig signingConfigs.releaseConfig
         // productFlavors.custom.signingConfig signingConfigs.custom
    }
}

推荐答案

Gradle Plugin User Guide 说你可以:

通过设置每个发布包使用自己的 SigningConfigandroid.productFlavors.*.signingConfig 对象分开.

have each release package use their own SigningConfig by setting each android.productFlavors.*.signingConfig objects separately.

此答案(Debug Signing Config on Gradle Product Flavors)和此博客文章 (使用 Gradle 构建多个版本的 Android 应用).

This is demonstrated in this answer (Debug Signing Config on Gradle Product Flavors) and this blog post (Building Multiple Editions of an Android App with Gradle).

但是,为每种风格指定单独的 signingConfig 行不能很好地扩展,并且超出了问题的范围.不幸的是,提供的答案都没有显示如何正确覆盖 signingConfig.

However, specifying a separate signingConfig line for each flavor does not scale well, and was out of scope of the question. Unfortunately none of the provided answers showed how to correctly override a signingConfig correctly.

诀窍来自这个答案(How to get the current selected build variant in gradle?),它展示了如何循环构建变体(以及扩展,风味).

The trick came from this answer (How to get the currently chose build variant in gradle?) which shows how to loop over build variants (and by extension, flavors).

我的解决方案使用循环来设置每种风味的 signingConfig,而不是为此设置单独的行.这可以很好地扩展.覆盖"是通过在循环之后指定自定义配置的单行来完成的.

My solution uses a loop to set the signingConfig on each flavor instead of having a separate line for that. This scales perfectly well. The "override" is done with a single line that specifies the custom config after the loop.

将以下代码放在 buildTypes.release 块中:

Place the following code inside the buildTypes.release block:

// loop over all flavors to set default signing config
productFlavors.all { flavor ->
    flavor.signingConfig signingConfigs.releaseConfig
}
// override default for single custom flavor
productFlavors.custom.signingConfig signingConfigs.custom

这篇关于在 Android 上使用不同密钥的 Gradle 签名风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆