9

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?

Anurag Singh
  • 6,140
  • 2
  • 31
  • 47
  • I think using your production signature for debug builds might cause error such as releasing debug version to Google Play by accident. Why do you want to do it? – Amir Uval Jul 27 '17 at 21:20
  • @auval You are very correct. I want to separate the signature in order to debug the production build if any issue arises. Secondly, there is a limit on map api request. I want to preserve the limit for actual users on production. In develop mode testers or developers can keep on testing with develop signature. FYI https://stackoverflow.com/q/44584273/2870088 – Anurag Singh Jul 28 '17 at 07:58
  • You can set a different package name to debug builds in Gradle, and with a debug key you will not use up your production quota for maps – Amir Uval Jul 28 '17 at 08:06
  • @auval I believe that by just setting the different package name won't work. applicationId should should match with certificate signature in firebase console. Even for debug builds if I go with default buildTypes.debug is not true. I need to do something as per Mani answer below. If you think there is other better or alternative way please do post your answer. – Anurag Singh Jul 28 '17 at 08:20
  • In Firebase console you will need to setup a new app for it. – Amir Uval Jul 28 '17 at 08:33
  • My answer does not directly answer your question, rather than avoiding the need.. – Amir Uval Jul 28 '17 at 08:35
  • @auval Yes I understand that this doesn't directly answer. Thank-you. I will try to go over your suggestions. I do agree but now there is a limit of free three apps only :-). – Anurag Singh Jul 28 '17 at 08:35

2 Answers2

5

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.

Mani
  • 2,599
  • 4
  • 30
  • 49
  • A problem occurred evaluating project ':app'. > ProductFlavor names cannot collide with BuildType names. This won't work – Anurag Singh Jul 28 '17 at 08:24
  • Can you post your updated gradle file with changes you made to mine? – Mani Jul 28 '17 at 09:55
  • Updated the answer rectifying the error project build and additional property of debuggable true to enable debugging – Mani Jul 28 '17 at 10:24
0

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
    }
}
Anurag Singh
  • 6,140
  • 2
  • 31
  • 47
  • 1
    How does this answer your question? Question is to have same "signingConfig" based for buildTypes and not on productflavours!! – Mani Jul 28 '17 at 09:51
  • 1
    @Mani Same signingConfig is used for different buildTypes by keeping the same certificate under productFlavors. Also, there are two gaps with your solution as I have mentioned in my answer. First regarding the A problem occurred evaluating project ':app'. > ProductFlavor names cannot collide with BuildType names. Secondly, set debuggable true for debugging the build. It doesn't matter if you put signingConfig under productFlavors or buildTypes. – Anurag Singh Jul 28 '17 at 10:00
  • "Same signingConfig is used for different buildTypes by keeping the same certificate under productFlavors." - This would lead unnecessary confusion though this trick will work!! – Mani Jul 28 '17 at 10:15