android - 强制使用相同的证书来签署为特定 "buildTypes"配置的不同 "productFlavor"?

标签 android gradle android-gradle-plugin apk

背景:

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

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

问题

例如,现在如果我谈论 flavor production 那么 productionRelease 使用 signingConfigs.production 来签署apk。但是,productionDebug 不使用 signingConfigs.production

预期输出

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

  1. developReleasedevelopDebug 应该只用 signingConfigs.develop

  2. productionReleaseproductionDebug 应该只用 signingConfigs.production

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

最佳答案

删除 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

Automate signing your applications. Debug build variants are, by default, signed with a debug key for installation on development devices. Declare additional signing configurations for publication to the Google Play store.

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

Add debuggable true property under custom buildTypes against which you want to debug build and see the logs.

关于android - 强制使用相同的证书来签署为特定 "buildTypes"配置的不同 "productFlavor"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166152/

相关文章:

android - 如何将库项目添加到 android 项目?

android - Gradle: espresso-core & appcompat 无法解决; SDK安装没有额外功能

android - gradle 和 gradle-experimental 有什么区别?

android - 如何为gradle设置代理服务器?

sql - 如何正确对 SQL 结果集进行分组?

android - 将重要数据存储在内部存储器中

java - 启动 Intent 时上下文空指针

gradle - 在Gradle中为不同的库使用不同的存储库

java - Gradle如何添加VM选项以在javafx 11 idea上运行和部署jfoenix?

Android studio 构建失败,出现以下错误