Search code examples
androidgradleandroid-build-flavorsandroid-build-type

How to have 2 flavors and 2 different key-configuration for debug&release for each?


Background

I think there is an issue on Google login that is related to the fact we use the same key configuration for 2 different flavors of an app we work on. The reason is that both seem to have the same SHA1 on debug and release.

The issue exists only on release version of the second flavor. On build&debug of the first flavor, and on debug of the second, it works fine.

The problem

I should probably generate a new key configuration while using the same release-keystore (generate using the existing one), but I'm not sure how to set it up on the gradle file.

Suppose the 2 package-names are "com.free" and "com.paid" (not real names, just for here to simplify the question).

This is what I have now, simplified and without the real values

defaultConfig {
    applicationId "com.free"
    ...
}

signingConfigs {
    debug {
        storeFile file('debug.keystore')
        storePassword "storePassword1"
        keyAlias "keyAlias1"
        keyPassword "keyPassword1"
    }

    release {
        storeFile file('release.keystore')
        storePassword "storePassword2"
        keyAlias "keyAlias2"
        keyPassword "keyPassword2"
    }
}

buildTypes {
    release {
        signingConfig signingConfigs.release
        ...
    } 

    debug {
        ...
    }
}

flavorDimensions.add("default")

productFlavors {
    free {
        dimension "default"
        applicationId "com.free"
        ...
    }

    paid {
        dimension "default"
        applicationId "com.paid"
        ...
    }
}

namespace 'com.free'

So this generates the 4 build-variants in the "Build Variants" window of Android Studio:

  1. freeDebug
  2. freeRelease
  3. paidDebug
  4. paidRelease

I want to stay with these, yet for "paid" ones have a different key-configuration as it's using the same one of "free" ones.

What I've found and tried

I've found the next questions and tutorials about this topic:

So, what I tried is to split the "release" in the "signingConfigs" (no need for the debug, as this one works fine for debug-free combination), remove the "signingConfig" from "buildTypes"->"release", and have 4 productFlavors instead of 2:

signingConfigs {
    //unchanged:
    debug {
        storeFile file('debug.keystore')
        storePassword "storePassword1"
        keyAlias "keyAlias1"
        keyPassword "keyPassword1"
    }

    //using new keystore file, split for 2 different flavors, and have new keyAlias and keyPassword for "paid" :
    releaseFree {
        storeFile file('new_release.keystore')
        storePassword "storePassword2"
        keyAlias "keyAlias2"
        keyPassword "keyPassword2"
    }
    releasePaid {
        storeFile file('new_release.keystore')
        storePassword "storePassword2"
        keyAlias "keyAlias3"
        keyPassword "keyPassword3"
    }
}

buildTypes {
    release {
        //commented this as it can't be used anymore (split and not shared)
        //signingConfig signingConfigs.release
        ...
    } 

    debug {
        ...
    }
}

flavorDimensions.add("default")

productFlavors {
    //split to 4 : free-debug, paid-debug, free-release, paid-release

    freeDebug {
        dimension "default"
        applicationId "com.free"
        //identical debug key configuration should work fine for both
        signingConfig signingConfigs.debug
        ...
    }

    paidDebug {
        dimension "default"
        applicationId "com.paid"
        //identical debug key configuration should work fine for both
        signingConfig signingConfigs.debug
        ...
    }

    freeRelease {
        dimension "default"
        applicationId "com.free"
        signingConfig signingConfigs.releaseFree 
        ...
    }

    paidDebug {
        dimension "default"
        applicationId "com.paid"
        signingConfig signingConfigs.releasePaid 
        ...
    }
}

The IDE accepts these changes, but instead of the planned 4 items in the "Build Variants" window, I see 8:

  1. freeDebugDebug
  2. freeDebugRelease
  3. freeReleaseDebug
  4. freeReleaseRelease
  5. paidDebugDebug
  6. paidDebugRelease
  7. paidReleaseDebug
  8. paidReleaseRelease

Pretty sure what happened here is that for each flavor, it generated debug&release, and as I have defined 4 flavors, it's 4*2=8 ...

The questions

  1. What have I done wrong here? How can I have 4 items as planned and as existed originally ? Maybe possible to set a buildType for each flavor? Or maybe I need to set 2 dimension values, one for "free" and one for "paid" ?

  2. Are the settings of the signingConfigs items seem fine? For each different file, it uses the same storePassword value, and for each flavor, it should use a different keyAlias and keyPassword . Right?


Solution

  • You don't need to create 4 product flavors, you can use build type to differentiate between debug and release build and set signing config accordingly.

    productFlavors {
        free {
            applicationId "com.free"
        }
    
        paid {
            applicationId "com.paid"
        }
    
    }
    

    Use build type for release/debug to set your signing config.

    buildTypes {
        release {
            productFlavors.free.signingConfig signingConfigs.releaseFree
            productFlavors.paid.signingConfig signingConfigs.releasePaid
            ...
        }
        debug{
            productFlavors.free.signingConfig signingConfigs.debug
            productFlavors.paid.signingConfig signingConfigs.debug
        }
    }
    

    Note: buildTypes block should be placed after productFlavors block