在Android上使用不同的密钥进行Gradle签名

我有我的Android应用程序的许多口味, 我想除了一个都使用相同的密钥 。 有一个需要使用不同的密钥。

我如何重写signingConfig适用于应用程序的味道(但在相同的构buildtypes,如“释放”)的signingConfig

  • 我希望所有版本默认使用主版本configuration。
  • 我只想覆盖1味
  • 我希望能够用一个gradlew assembleRelease命令来运行所有的发行版本

最后一点非常重要,因为我现在有超过120种不同的口味,而且越来越多。 为了定制每一个单独的风味是很多额外的工作。


相关的post我试过了:

使用单一构buildtypes的不同键生成多个构build

  • 这需要对每种风味进行configuration
  • 它似乎并不使用我的自定义signingConfig无论如何

用gradle签署产品口味

  • 接受的解决scheme不起作用(对我来说)
  • 根据评论,这是可能的通过将buildTypes内的productFlavors但我不明白如何做到这一点。

在Gradle Product Flavors上debugging签名configuration

  • 正如在博客文章中解释的那样: 使用Gradle构buildAndroid应用程序的多个版本
  • 实际上不起作用,完美的作品
  • 但对于119种口味来说,这并不好

总的来说,每个解决scheme似乎仍然使用默认的发布configuration,而不是我的自定义configuration。


我的build.gradle重要部分是这样的:

 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插件用户指南说,你可以:

让每个发行包通过分别设置每个android.productFlavors.*.signingConfig对象来使用自己的SigningConfig

这在这个答案( 在Gradle Product Flavors上debugging签名configuration )和这个博客文章( 使用Gradle构buildAndroid应用程序的多个版本)中得到了certificate。

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


诀窍来自这个答案( 如何得到当前select的构build变种在gradle? ),它展示了如何循环构build变种(和扩展,口味)。

我的解决scheme使用循环来设置每个flavor的signingConfig ,而不是单独的一行。 这个比例非常好。 “覆盖”是通过一个单行来完成的,该行在循环之后指定了自定义configuration。

buildTypes.release块中放置下面的代码:

 // 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 

如果在product flavor中未指定signingConfig,下面给出的代码将使用release1作为默认的signingConfig。

应用程序/的build.gradle

 signingConfigs { debug { storeFile file("/home/.../debugkeystore.jks") storePassword "..." keyAlias "..." keyPassword "..." } release1 { storeFile file("/home/.../testkeystore1.jks") storePassword "..." keyAlias "..." keyPassword "..." } release2 { storeFile file("/home/.../testkeystore2.jks") storePassword "..." keyAlias "..." keyPassword "..." } release3 { storeFile file("/home/.../testkeystore3.jks") storePassword "..." keyAlias "..." keyPassword "..." } } defaultConfig { applicationId "com.example.signingproductflavors" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" signingConfig signingConfigs.release1 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { signingConfig signingConfigs.debug } } productFlavors { blocks { applicationId "com.example.blocks" resValue 'string', 'APP_NAME', "Blocks" } cloud { applicationId "com.example.cloud" resValue 'string', 'APP_NAME', "Cloud" signingConfig signingConfigs.release2 } deck { applicationId "com.example.deck" resValue 'string', 'APP_NAME', "Deck" signingConfig signingConfigs.release3 } } 

我不是100%确定这将工作,但我不认为你想创build一个新的构buildtypes。 这将创造一个新的构build变种每个味道。 当你真的只想要一种风味来覆盖“默认configuration”:)

这个代码没有经过testing,但你应该能够做到这一点:

 signingConfigs { normal { storeFile file('key') storePassword "pass" keyAlias "alias" keyPassword "pass" } custom { storeFile file('custom_key') storePassword "pass" keyAlias "alias" keyPassword "pass" } } /** * defaultConfig is of type 'ProductFlavor'. * * If we need to use a different signing key than the default, * override it in the specific product flavor. */ defaultConfig { versionCode 123 versionName '1.2.3' minSdkVersion 15 def standardSigningConfig = signingConfigs.normal buildTypes{ release { signingConfig standardSigningConfig zipAlign true // ... } debug { //not sure you need this node } } } productFlavors { def customConfig = signingConfigs.custom def standardSigningConfig = signingConfigs.normal apple { applicationId "demo.apple" } banana { applicationId "demo.banana" } custom { applicationId "custom.signed.app" signingConfig customConfig } } 

参考:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-Flavor-Configuration

你将不得不在你的buildType中定义签名configuration。 将自定义签名configuration添加到您的debugging生成types或创build一个自定义生成types

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

Gradle将为每个构buildtypes创build风格,并根据buildType风味将使用各自的signinconfig。 用上面的构buildtypes的configuration,让我们考虑一下“苹果”的味道。 Gradle将为苹果创build以下构build变体

  • appledebug – >自定义签名configuration
  • applecustom – >自定义签名configuration
  • applerelease – >发布签名configuration

    您可以select相应的构build版本并运行您的应用程序

将签名configuration添加到flavor

 productFlavors { def customSigningConfig = signingConfigs.custom custom { ... signingConfig customSigningConfig ... } 

在声明你的风味之前,你需要声明你的签名。

https://code.google.com/p/android/issues/detail?id=64701

一个想法可能是使用项目属性,以确定是否应该使用您的自定义signinconfig。

 if (project.hasProperty('custom')) { android.signingConfigs.release = customSigningConfig } else { //should use the default } 

然后build立你自己运行的自定义风格:

 gradle assembleCustomRelease -Pcustom=true 

tl; dr通过“gradle.startParameter.taskNames”来查找味道并修改variables。

我为Vine应用程序的testing版本做了这个工作,它的工作非常好。 你也可以使用它来编译不同的依赖关系,而不需要添加更多的风格维度。

在你的情况下,它会看起来像这样。

  //root of buil.gradle OR probably inside buildTypes.release def signType = signingConfigs.normal; //You can put this inside builTypes.release or any task that executes becore def taskNames = gradle.startParameter.taskNames; taskNames.each { String name -> if (name.contains("customFlavor")) { signType = signingConfigs.custom } } buildTypes{ release { signingConfig signType } }