Different keyStore for different product flavors in Gradle
Asked Answered
E

5

15

I have different productFlavors specified in my build.gradle file

 dev {
            applicationId "com.advancedprogressive.chare.dev"
            versionCode 83
            versionName "2.2.1"
        }
staging {
            applicationId "com.advancedprogressive.chare.qa"
            versionCode 119
            versionName "2.8.1"
        }

and have have signing configurations like

signingConfigs {
        release {
            storeFile 
            storePassword 
            keyAlias 
            keyPassword         }
        debug {
            keyPassword 
            storeFile 
            keyAlias 
            storePassword 
        }
    }

I have different keystors for both flavors. I can specify different keystores for different build types e.g debug/release but how can i specify different keysotre for each flavor.

Ewing answered 24/3, 2016 at 7:0 Comment(0)
D
5

You can use somenthing like this:

android {
    signingConfigs {
        dev {
        }

        staging {
        }
    }

    productFlavors {
        dev {
            signingConfig signingConfigs.dev
        }

        staging {
            signingConfig signingConfigs.staging
        } 
    }
}
Dercy answered 24/3, 2016 at 8:22 Comment(1)
How would you specify a signing config for stagingRelease but not stagingDebug ?Demarcation
G
15

Using below gradle you can achieve multiple productFlavors :

   android {
  signingConfigs {
    release {
        keyAlias 'alias'
        keyPassword 'password'
        storeFile file('first.keystore')
        storePassword 'password'
    }

    debug {
        keyAlias 'alias'
        keyPassword 'password'
        storeFile file('second.keystore')
        storePassword 'password'
    }
  }

  compileSdkVersion 23
  buildToolsVersion "23.0.2"
  defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
     }

      productFlavors{
        dev {
            applicationId "com.advancedprogressive.chare.dev"
            versionCode 83
            versionName "2.2.1"
            signingConfig signingConfigs.debug 
        }
        staging {
            applicationId "com.advancedprogressive.chare.qa"
            versionCode 119
            versionName "2.8.1"
            signingConfig signingConfigs.release
        }
  }

      }

I hope its help you.

Gylys answered 24/3, 2016 at 8:20 Comment(2)
got it. now how can i specify if build should be debug able or not. where should i place "debuggable" flagEwing
You can get your answer here : run/debug release version of appGylys
O
6

You may also want to keep your credentials secured in a separate file.

In your app/build.gradle file

android{
    signingConfigs {
        free {
            def keystorePropertiesFile = rootProject.file("app/src/free/keystore.properties")
            def keystoreProperties = new Properties()
            keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }

        paid {
            def keystorePropertiesFile = rootProject.file("app/src/paid/keystore.properties")
            def keystoreProperties = new Properties()
            keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }

    ...

    flavorDimensions "default"
    productFlavors {
        free {
            applicationId "com.example.freeapp"
            versionCode 1
            versionName "1.0"
            signingConfig signingConfigs.free

        }
        paid {
            applicationId 'com.example.paidapp'
            versionCode 1
            versionName '1.0'
            signingConfig signingConfigs.paid
        }
    }
}

In your app/src/flavor_name/keystore.properties

storePassword=...
keyPassword=..
keyAlias=...
storeFile=src/flavor_name/keystore_file.jks //or any other path of jks file
Outofdoors answered 5/10, 2018 at 18:39 Comment(3)
just to be sure. Does putting the key properties inside the src/flavor won't also include it to the final APK, right?Loseff
@Loseff The build.gradle is not compiled into APK file. It is only used to fetch all dependencies, create flavors, do versioning and sign in an app. So key details will not be included.Outofdoors
What if I have a different key-configuration (storePassword,keyAlias, keyPassword) for debug than release variants? I want to have debug&release variants for each type (free and paid in this example) ? Each item in productFlavors gets automatically 2 types to run in the "Build variants" window (debug, release ). So I already have 4 items there (debug&release for "free" and same for "paid", so it's 2*2=4).Filibeg
D
5

You can use somenthing like this:

android {
    signingConfigs {
        dev {
        }

        staging {
        }
    }

    productFlavors {
        dev {
            signingConfig signingConfigs.dev
        }

        staging {
            signingConfig signingConfigs.staging
        } 
    }
}
Dercy answered 24/3, 2016 at 8:22 Comment(1)
How would you specify a signing config for stagingRelease but not stagingDebug ?Demarcation
N
0

For me, when building, it´s aways using Release signingConfigs.

Nylanylghau answered 16/8, 2022 at 19:44 Comment(0)
C
0

1. Create as many as you need signing configs 2. Specify "signingConfig" in "productFlavors" instead of "buildTypes" and have fun.

signingConfigs {
    funnyRelease {
        // do stuff
    }
    debug {
        storeFile file('debug.keystore')
        storePassword 'android'
        keyAlias 'androiddebugkey'
        keyPassword 'android'
    }        
}

buildTypes {
    release {
        // !!!do not specify signing on the build type!!!
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard- 
            android.txt"),"proguard-rules.pro"
    }
}

productFlavors {
    flavorOne {
       // signing for "flavorOne" specified as debug
        signingConfig signingConfigs.debug
    }
    flavourTwo {
        // signing for "flavourTwo" specified as funnyRelease
        signingConfig signingConfigs.funnyRelease
    }
}
Clachan answered 27/3 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.