Force to use same certificate to sign different “buildTypes” that are configured for a particular “productFlavor”?

半腔热情 提交于 2020-08-01 17:27:18

问题


Background:

I am generating builds using build variant. Below are the configurations:

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

Problem

As, of now for example if I talk about flavour production then productionRelease uses signingConfigs.production to sign the apk. But, productionDebug doesn't uses signingConfigs.production.

Expected output

When I generate the signed apk I want the gradle to do the following for me:

  1. developRelease and developDebug should be signed with only signingConfigs.develop

  2. productionRelease and productionDebug should be signed with only signingConfigs.production

Another question that is similar to this which led me to do the above: SHA-1 different for buildTypes (debug and release) for same productFlavors Firebase?


回答1:


Remove the signingConfig signingCongigs.develop elsewhere in the code block

And add new property within the buildTypes like

  • withProduction
  • withDevelop

Now add signingConfig to it

Thereby your updated gradle file look like as below

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

    }
}

In the terminal use the below gradle commmand: gradle assembleProduction to generate build with production certificates similarly gradle assembleDevelop or you can also use gradle assemble

You cannot force gradle to choose the certificates for debug property rather you could create own buildTypes

As per 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.

Update: As the other answer pointed out,

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




回答2:


Thanks to @Mani for throwing the light on buildTypes.debug.

As per documentation Application Signing

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.

The above is very correct. No need to remove signingConfig from productFlavors. Also, add debuggable true property under custom buildTypes against which you want to debug build and see the logs.

You can add below properties in buildTypes as per your need:

{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={}}

As, I have added debuggable true under withProductionDebug and 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
    }
}


来源:https://stackoverflow.com/questions/45166152/force-to-use-same-certificate-to-sign-different-buildtypes-that-are-configured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!