Search code examples
androidgroovyandroid-gradle-pluginbuild-processbuild.gradle

Differentiate between productFlavors in Android app.gradle file during build process


    android {
    final String analyticsJSON = "lite"
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.ralok.apps"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    productFlavors {
        findlostandroidphone {
            versionCode 1
            versionName 'v1.0'
            applicationId 'com.ralok.apps.findlostandroidphone'
        }
        findlostandroidphonepro {
            versionCode 1
            versionName 'v1.0'
            applicationId 'com.ralok.apps.findlostandroidphonepro'
        }
        if (analyticsJSON.equals("lite")) {
            println "--> FLPLite JSON copied!"
            copy {
                from 'src/findlostandroidphone/'
                include 'google-services.json'
                into '.'
            }
        } else {
            println "--> FLPPro JSON copied!"
            copy {
                from 'src/findlostandroidphonepro/'
                include 'google-services.json'
                into '.'
            }
        }
    }

    signingConfigs {
        lite_release {
            keyAlias 'ASDFGHJKL'
            keyPassword 'ASDFGHJKL'
            storeFile file('ASDFGHJKL.jks')
            storePassword 'ASDFGHJKL'
        }
        pro_release {
            keyAlias 'POIUYTREWQ'
            keyPassword 'POIUYTREWQ'
            storeFile file('POIUYTREWQ.jks')
            storePassword 'POIUYTREWQ'
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            debuggable true
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Every time I have to manually change the final String analyticsJSON = "lite/pro" so that the if loop copies the right json file to the root directory. Is there anyway I can automate this and always have the correct productFlavour in analyticsJSON String instead of me changing the value manually every time I switch between productFlavors. My groovy is bad and I have tried but failed to achieve this.


Solution

  • Finally a solution for handling different flavors is implemented in version com.google.gms:google-services:2.0.0-alpha3

    https://developers.google.com/android/guides/google-services-plugin#adding_the_json_file https://github.com/googlesamples/google-services/issues/54

    Another alternative solution is on

    How do i keep different configurations for my android app with GCM 3.0