【问题标题】:Force to use same certificate to sign different "buildTypes" that are configured for a particular "productFlavor"?强制使用相同的证书来签署为特定“productFlavor”配置的不同“buildTypes”?
【发布时间】:2023-03-17 16:09:01
【问题描述】:

背景:

我正在使用构建变体生成构建。以下是配置:

signingConfigs {
    production {
        storeFile file("some_path/buildsystem/keystore/some.release.keystore.jks")
        storePassword "somepassword"
        keyAlias "somekeyalias"
        keyPassword "some"
        v2SigningEnabled false
    }

    develop {
        storeFile file(".some_path./buildsystem/keystore/someother.debug.keystore.jks")
        storePassword "someother"
        keyAlias "someotherkeyalias"
        keyPassword "someother"
        v2SigningEnabled false
    }
}

productFlavors {
    production {
        signingConfig signingConfigs.production
      }

    develop {
        applicationIdSuffix ".develop"
        signingConfig signingConfigs.develop
     }
}

buildTypes {
    debug {
        minifyEnabled false
    }

    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

问题

现在,例如,如果我谈论风味 production 然后 productionRelease 使用 signingConfigs.production 签署 apk。但是,productionDebug 不使用signingConfigs.production

预期输出

当我生成签名的 apk 时,我希望 gradle 为我执行以下操作:

  1. developReleasedevelopDebug 应仅使用 signingConfigs.develop

  2. 签名
  3. productionReleaseproductionDebug 应仅使用 signingConfigs.production

  4. 签名

另一个与此类似的问题导致我执行上述操作:SHA-1 different for buildTypes (debug and release) for same productFlavors Firebase?

【问题讨论】:

  • 我认为使用您的生产签名进行调试构建可能会导致错误,例如意外将调试版本发布到 Google Play。你为什么要这样做?
  • @auval 你说的很对。如果出现任何问题,我想分离签名以便调试生产版本。其次,地图 api 请求有限制。我想保留生产中实际用户的限制。在开发模式下,测试人员或开发人员可以继续使用开发签名进行测试。仅供参考stackoverflow.com/q/44584273/2870088
  • 您可以设置不同的包名称来调试 Gradle 中的构建,并且使用调试密钥您不会用完地图的生产配额
  • @auval 我相信仅仅设置不同的包名是行不通的。 applicationId 应与 firebase 控制台中的证书签名匹配。即使对于调试版本,如果我使用默认的 buildTypes.debug 也不正确。我需要按照下面的 Mani 回答做一些事情。如果您认为还有其他更好或替代的方法,请发布您的答案。
  • 在 Firebase 控制台中,您需要为其设置一个新应用。

标签: android gradle android-gradle-plugin apk


【解决方案1】:

删除 signingConfig signingCongigs.develop 代码块中的其他地方

并在 buildTypes 中添加新属性,如

  • 与生产
  • 与开发

现在添加signingConfig

因此您更新的 gradle 文件如下所示

signingConfigs {
    production {
        storeFile file("some_path/buildsystem/keystore/some.release.keystore.jks")
        storePassword "somepassword"
        keyAlias "somekeyalias"
        keyPassword "some"
        v2SigningEnabled false
    }

    develop {
        storeFile file(".some_path./buildsystem/keystore/someother.debug.keystore.jks")
        storePassword "someother"
        keyAlias "someotherkeyalias"
        keyPassword "someother"
        v2SigningEnabled false
    }
}
productFlavors {
    production {
    }

    develop {
        applicationIdSuffix ".develop"
    }
}
buildTypes {
    /* NOTE: the debug block is not required because it is a default
 * buildType configuration; all of its settings are defined implicitly
 * by Gradle behind the scenes.
 */
    debug {
        minifyEnabled false
    }

    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        signingConfig signingConfigs.production
    }

    withProduction {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        signingConfig signingConfigs.production
    }

    withDevelop {
        minifyEnabled false
        signingConfig signingConfigs.develop
debuggable true

    }
}

在终端中使用以下 gradle 命令: gradle assembleProduction 生成带有生产证书的构建 类似gradle assembleDevelop 或者你也可以使用gradle assemble

您不能强制 gradle 为 debug 属性选择证书,而是可以创建自己的 buildTypes

根据documentation

自动签署您的应用程序。默认情况下,调试构建变体使用调试密钥签名,以便在开发设备上安装。声明要发布到 Google Play 商店的其他签名配置。

更新: 正如另一个答案指出的那样,

在您所针对的自定义 buildTypes 下添加可调试的 true 属性 想要调试构建并查看日志。

【讨论】:

  • 评估项目 ':app' 时出现问题。 > ProductFlavor 名称不能与 BuildType 名称冲突。这行不通
  • 您可以发布更新的 gradle 文件以及您对我所做的更改吗?
  • 更新了纠正错误项目构建的答案和 debuggable true 的附加属性以启用调试
【解决方案2】:

感谢@Mani 为buildTypes.debug 提供帮助。

根据documentation Application Signing

自动签署您的应用程序。默认情况下,调试构建变体使用调试密钥进行签名,以便在开发设备上安装。声明要发布到 Google Play 商店的其他签名配置。

上面说的很对。 无需从productFlavors 中删除signingConfig。此外,在自定义buildTypes 下添加debuggable true 属性,您要根据该属性调试构建并查看日志。

您可以根据需要在buildTypes 中添加以下属性:

{name=withDevelopDebug, debuggable=false, testCoverageEnabled=false, 
jniDebuggable=false, pseudoLocalesEnabled=false, 
renderscriptDebuggable=false, renderscriptOptimLevel=3, 
minifyEnabled=false, zipAlignEnabled=true, signingConfig=null, 
embedMicroApp=true, buildConfigFields={}, resValues={}, 
proguardFiles= [], consumerProguardFiles=[], manifestPlaceholders={}}

作为,我在withProductionDebugwithDevelopDebug 下添加了debuggable true

signingConfigs {
    production {
    storeFile file("some_path/buildsystem/keystore/some.release.keystore.jks")
    storePassword "somepassword"
    keyAlias "somekeyalias"
    keyPassword "some"        
    }

    develop {
    storeFile file(".some_path./buildsystem/keystore/someother.debug.keystore.jks")
    storePassword "someother"
    keyAlias "someotherkeyalias"
    keyPassword "someother"
    }
}

buildTypes {
    withProductionRelease{
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }

    withProductionDebug{
        minifyEnabled false
        debuggable true
    }

    withDevelopRelease{
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }

    withDevelopDebug{
        minifyEnabled false
        debuggable true
    }
}

productFlavors {
    production {
        signingConfig signingConfigs.production
    }

    develop {
        applicationIdSuffix ".develop"
        signingConfig signingConfigs.develop
    }
}

【讨论】:

  • 这如何回答您的问题?问题是基于 buildTypes 而不是 productflavors 具有相同的“signingConfig”!
  • @Mani 相同的signingConfig 用于不同的构建类型,方法是在productFlavors 下保持相同的证书。此外,正如我在回答中提到的那样,您的解决方案存在两个差距。首先关于评估项目':app'时出现的问题。 > ProductFlavor 名称不能与 BuildType 名称冲突。其次,将 debuggable true 设置为调试构建。将signingConfig放在productFlavors或buildTypes下都没关系。
  • "通过在 productFlavors 下保持相同的证书,相同的 signingConfig 用于不同的 buildTypes。" - 虽然这个技巧会起作用,但这会导致不必要的混乱!
猜你喜欢
  • 1970-01-01
  • 2021-05-06
  • 2015-07-30
  • 1970-01-01
  • 2022-10-24
  • 2021-12-11
  • 2020-12-02
  • 1970-01-01
  • 2016-09-16
相关资源
最近更新 更多