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.

Jay Sidri
  • 6,271
  • 3
  • 43
  • 62
SAIR
  • 1,121
  • 11
  • 31

4 Answers4

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.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • got it. now how can i specify if build should be debug able or not. where should i place "debuggable" flag – SAIR Mar 24 '16 at 09:07
  • You can get your answer here : [run/debug release version of app](http://stackoverflow.com/a/29027892/2949612) – pRaNaY Mar 24 '16 at 09:13
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
bikram
  • 7,127
  • 2
  • 51
  • 63
  • just to be sure. Does putting the key properties inside the src/flavor won't also include it to the final APK, right? – Cristiano Apr 07 '20 at 14:07
  • @Cristiano 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. – bikram Apr 08 '20 at 09:21
  • 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). – android developer Feb 13 '23 at 09:40
5

You can use somenthing like this:

android {
    signingConfigs {
        dev {
        }

        staging {
        }
    }

    productFlavors {
        dev {
            signingConfig signingConfigs.dev
        }

        staging {
            signingConfig signingConfigs.staging
        } 
    }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

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

GSFZamai
  • 64
  • 6