【问题标题】:Gradle signing flavors with different keys on Android在 Android 上使用不同密钥的 Gradle 签名风格
【发布时间】:2015-09-03 02:28:44
【问题描述】:

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

如何仅针对 1 种风格的应用覆盖 signingConfig(但在相同的构建类型中,例如“发布”)?

  • 我希望所有构建默认使用主发布配置。
  • 我只想覆盖一种风味
  • 我希望能够使用单个 gradlew assembleRelease 命令运行所有发布版本

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


我尝试过的相关帖子:

Producing multiple builds signed with different keys from single build type

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

Signing product flavors with gradle

  • 已接受的解决方案不起作用(对我而言)
  • 根据a comment,这可以通过将buildTypes 放在productFlavors 中来实现,但我不明白该怎么做。

Debug Signing Config on Gradle Product Flavors

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


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 的文档特别好。
  • 感谢您的精彩问答!我看到你现在维护了大约 120 种口味,而且构建时间很长。我在创建一个必须多次重新命名的同时标签应用程序的同一条船上。我才刚开始。我必须使用不同的密钥来签署每种风格,但还有其他 UI 和配置更改。你对如何维护如此庞大的项目架构/配置有什么建议吗?如果你现在可以从头开始,你应该注意什么,或者你会做些什么不同的事情?如果您有时间分享,这将是一篇博文的好主题;)。提前致谢。

标签: android gradle android-gradle-plugin code-signing


【解决方案1】:

Gradle Plugin User Guide 表示您可以:

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

此答案 (Debug Signing Config on Gradle Product Flavors) 和此博客文章 (Building Multiple Editions of an Android App with Gradle) 对此进行了演示。

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


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

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

将以下代码放在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

【讨论】:

  • 不幸的是,您必须跳过这些障碍才能使其正常工作。必须迭代变体并通过字符串名称寻找风味似乎与他们的文档背道而驰。虽然...productFlavors.all 实际上是一个常用的脚本块,但我不确定你可以在那里切换signingConfig。特别是因为他们展示了一个没有那个的例子。但我很高兴你找到了解决方案!并感谢您将其发回。
  • 喜欢这个,这也简化了我现在拥有的东西。
  • 请注意,在 build.gradle 中顺序很重要—— productFlavors {} 和 signingConfigs {} 必须在 buildTypes {} 之前声明。否则没有这样的订单要求。
  • 这是不正确的,release{} buildType 会覆盖之前声明的 buildType 块,这样只会使用 release{} 签名密钥
  • 自 2015 年发布此内容以来,情况可能已发生变化 - 请随时发布更新的答案 @Jemshit。这个问题和答案来自一个 100% 的生产项目,每天构建调试和发布构建类型。至少,在过去,从我个人的使用经验来看,这 100% 有效。
【解决方案2】:

如果产品风格中没有指定signingConfig,下面给出的代码将使用release1作为默认signingConfig。

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

【讨论】:

  • 我喜欢简单的优雅,但它有用吗?在这种情况下,您将如何使用不同的密钥构建 debug 构建?我喜欢使用 defaultConfig 的想法,但这不会覆盖所有构建类型吗?
  • 是的,这段代码有效。产品风味的属性会覆盖默认配置的属性。默认配置不会覆盖构建类型。您可以在 buildTypes 中使用 debug { signingConfig signingConfigs.release3 } 为调试构建类型定义不同的签名配置。
  • 那么代码看起来如何:1)所有调试版本都使用相同的调试签名配置。 2) 除了 1 个版本之外,所有版本都使用相同的版本配置。 3) 单一发行版本使用不同的密钥?
  • 1) 所有调试版本都使用相同的调试签名配置。 2) 所有发布版本都可以使用产品风格中指定的不同发布签名配置。 3) 如果没有在其产品风格中指定任何内容,则发布版本可以使用默认的发布签名配置。
  • 您能否通过更新您的答案来展示该代码的外观?
【解决方案3】:

我不是 100% 确定这会起作用,但我认为您不想创建新的构建类型。这将为每种口味创建一个新的构建变体。当你真的只想要一种风格来覆盖“默认配置”时:)

此代码未经测试,但您应该能够按照以下方式做一些事情:

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

【讨论】:

  • 感谢文档链接。获得可行的解决方案是一段漫长的旅程。
【解决方案4】:

您必须在 buildTypes 中定义签名配置。将自定义签名配置添加到您的调试构建类型或创建自定义构建类型

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

Gradle 将为每种构建类型创建风味,并且根据构建类型,风味将使用各自的 signinconfig。通过上述构建类型的配置,让我们考虑一下“苹果”的味道。 Gradle 将为苹果创建以下构建变体

  • appledebug --> 自定义签名配置
  • applecustom --> 自定义签名配置
  • applerelease --> 发布签名配置

    您可以选择相应的构建变体并运行您的应用程序

为风味添加签名配置

productFlavors {
    def customSigningConfig = signingConfigs.custom

    custom {
        ...
        signingConfig customSigningConfig
        ...
    }

您需要在声明您的风味之前声明您的签名配置。

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

【讨论】:

  • 强调:我有 118 种口味使用相同的键。我使用不同的键有 1 种风味。我想知道 gradle 如何使用单个命令为所有 119 种风格构建发布 APK 文件。
  • 虽然 118 种口味对于口味来说似乎是一个很大的数字
  • 119 和计数。 gradle build 还将构建 119 个不需要的调试 APK 和 118 个不需要的自定义 APK(因为我只需要 1 个自定义)。当前的构建时间是 1 1/2 小时。 gradle build 将是 4 1/2 小时。不能很好地扩展。
  • 119 种口味的原因是什么,我只是在想是否有替代方法并避免在构建上花费 1 1/2。
  • 我已经更新了关于如何在 buildflavor 上设置 signingconfig 的帖子
【解决方案5】:

一个想法可能是使用项目属性来确定您是否应该使用您的自定义登录配置。

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

然后构建您运行的自定义风味:

gradle assembleCustomRelease -Pcustom=true

【讨论】:

  • 但是该调用只会构建您的特定变体,希望最多只需要几分钟。它也可以编写脚本和/或集成到您的 CI 中,因此您无需付出任何努力
  • 感谢@arnaud,谢谢。然而,这个问题试图找到一种方法来覆盖单一风味/变体的signingConfig,以避免使用额外的命令或脚本。
【解决方案6】:

tl;dr 通过“gradle.startParameter.taskNames”查找风味并修改变量。

我为 Vine 应用程序的测试变体执行此操作,效果非常好。您还可以使用它来编译不同的依赖项,而无需添加更多风味维度。

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

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

【讨论】:

  • 在运行gradlew assembleRelease 时,taskNames 只包含“assembleRelease”而不是所有的风味。所以这并没有真正做任何有用的事情。
  • ^是的,那只是一个占位符。
  • 嗯,让我看看你能不能找到当前的任务名称
猜你喜欢
  • 1970-01-01
  • 2022-08-09
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多