I have configured my app build.gradle with free and paid version with different keystores. look at the code below.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    signingConfigs {
        freeProdRelease {
            keyAlias 'free.prod.release.key'
            keyPassword 'password'
            storeFile file('F:/freeProdRelease.jks')
            storePassword 'password'
        }
        paidProdRelease {
            keyAlias 'paid.prod.release.key'
            keyPassword 'password'
            storeFile file('F:/paidProdRelease.jks')
            storePassword 'password'
        }
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.the.app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        buildConfigField "long", "BUILD_DATE", "${System.currentTimeMillis()}L"
    }
    buildTypes {
        release {
            resValue "string", "some_api_key", "key for release"
            shrinkResources true //remove unused resources... will reduce APK size
            debuggable false
            useProguard true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            resValue "string", "some_api_key", "key for debug"
            debuggable true
            useProguard false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    flavorDimensions "version", "environment"
    productFlavors {
        free {
            dimension "version"
            buildConfigField 'boolean', 'IS_PAID', 'false'
            versionNameSuffix "-free"
            applicationIdSuffix ".free"
        }
        paid {
            dimension "version"
            buildConfigField 'boolean', 'IS_PAID', 'true'
            versionNameSuffix "-paid"
            applicationIdSuffix ".paid"
        }
        dev {
            dimension "environment"
            versionCode 2000 + android.defaultConfig.versionCode
            versionNameSuffix "-dev"
            applicationIdSuffix ".dev"
            manifestPlaceholders = [appLabel: "The App - Dev"]
        }
        prod {
            dimension "environment"
            versionCode android.defaultConfig.versionCode
            manifestPlaceholders = [appLabel: "The App"]
        }
    }
    applicationVariants.all { variant ->
        if(variant.buildType.name == "release"){
            if(variant.productFlavors[0].name == "paid"){
                android.buildTypes.release.signingConfig android.signingConfigs.paidProdRelease
            } else {
                android.buildTypes.release.signingConfig android.signingConfigs.freeProdRelease
            }
        }
        variant.outputs.each { output ->
            def apkName = "theApp-v" + android.defaultConfig.versionName + "-c" +
                    variant.productFlavors[1].versionCode

            apkName +=
                    "-${variant.productFlavors[0].name}-" +
                            "${variant.productFlavors[1].name}-${variant.buildType.name}-${releaseTime()}"
            apkName += ".apk"

            output.outputFileName = new File("$apkName")
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == 'com.android.support') {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion '27.1.1'
                }
            }
        }
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

static def releaseTime() {
    return new Date().format("yyyy-MMM-dd")
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.android.support.constraint:constraint-layout:$constraintLayoutVersion"
    implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
    implementation "com.android.support:recyclerview-v7:$supportLibraryVersion"
    implementation "com.android.support:design:$supportLibraryVersion"
    implementation "com.android.support:support-v4:$supportLibraryVersion"
    implementation "android.arch.persistence.room:runtime:$roomVersion"
    kapt "android.arch.persistence.room:compiler:$roomVersion"
    implementation "android.arch.lifecycle:extensions:$archLifecycleVersion"
    kapt "android.arch.lifecycle:compiler:$archLifecycleVersion"

    //dagger 2

    //    implementation "com.google.dagger:dagger:$daggerVersion"

    //    kapt "com.google.dagger:dagger-compiler:$daggerVersion"

    //to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes

    //    implementation "com.google.dagger:dagger-android:$daggerVersion"

    //    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"

    //support libraries with dagger 2

    //    implementation "com.google.dagger:dagger-android-support:$daggerVersion"
//    implementation "com.google.code.gson:gson:$gsonVersion"
//    implementation "pub.devrel:easypermissions:$easyPermissionVersion"
//    implementation "com.github.ybq:Android-SpinKit:$spinKitVersion"

    //progress view
//    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
//    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"

    //    implementation "org.greenrobot:eventbus:$eventBusVersion"
//    implementation "com.github.bumptech.glide:glide:$glideVersion"
//    kapt "com.github.bumptech.glide:compiler:$glideVersion"
//    implementation "com.joooonho:selectableroundedimageview:$selectableRoundedImageViewVersion"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

What I want is when I change my variant freeProdRelease or paidProdRelease and create release build then keystore file should be selected according to variant. for that I have setup code below

if(variant.buildType.name == "release"){            
     if(variant.productFlavors[0].name == "paid"){           
          android.buildTypes.release.signingConfig android.signingConfigs.paidProdRelease
     } else {
          android.buildTypes.release.signingConfig android.signingConfigs.freeProdRelease
   }
}

When I remove the above code then It gives error for release build and that is fine.

But when I commented else block and creating release build it still works and doesn't give error for keystore file, It means each time keystore file will be used paidProdRelease that I don't want.

if(variant.buildType.name == "release"){            
     if(variant.productFlavors[0].name == "paid"){           
          android.buildTypes.release.signingConfig android.signingConfigs.paidProdRelease
     } else {
//          android.buildTypes.release.signingConfig android.signingConfigs.freeProdRelease
   }
}

What should I do to use different keystore file for respected version free/paid?

0 Answers