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

时间:2017-07-18 11:58:34

标签: android gradle android-gradle 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'
    }
}

问题

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

预期输出

当我生成签名的apk时,我希望gradle为我做以下事情:

  1. developRelease developDebug 只能使用signingConfigs.develop

  2. 进行签名
  3. productionRelease productionDebug 只能使用signingConfigs.production

  4. 进行签名

    与此类似的另一个问题让我做了以上事情:SHA-1 different for buildTypes (debug and release) for same productFlavors Firebase?

2 个答案:

答案 0 :(得分:3)

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

在buildTypes中添加新属性,如

  • withProduction
  • withDevelop

现在添加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下添加debuggable true属性   想要调试构建并查看日志。

答案 1 :(得分:0)

感谢@Mani在buildTypes.debug上发光。

根据documentation Application Signing

  

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

以上是非常正确的。 无需从signingConfig移除productFlavors。另外,在自定义debuggable true下添加buildTypes属性,您要根据该属性调试构建并查看日志。

您可以根据需要在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={}}

因为,我在debuggable truewithProductionDebug

下添加了withDevelopDebug
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
    }
}