2
    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.

ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
LokiDroid
  • 972
  • 10
  • 22
  • 1
    Does http://stackoverflow.com/questions/17697154/gradle-android-plugin-add-custom-flavor-attribute help? – tim_yates Nov 29 '15 at 21:06

2 Answers2

1

You don't need to differentiate the build system will do it for you. Simply place the files in the correct path per build flavor. For example.

For findlostandroidphone place your google-services.json in the following location.

src/findlostandroidphone/google-services.json the build the file will be copied into the final location automatically for that product flavor. Same for the pro version.

What you have right now will be executed in the configuration phase. which probably works... for a single build type. If you want to build both types at the same time then you should move the files and remove the final String analyticsJSON = "lite" entirely. Then during the execution phase gradle will copy the file and execute the build with the correct file for that productFlavor.

JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • Without a conditional check, the latter seems to override the former copied file in the root folder of the project. I mean the first findlostandroidphone flavor copies the JSON file to the root folder and then the file is overwritten by findlostandroidphonepro flavor JSON file at the root. If I'm taking a build of findlostandroidphone then I still end up with findlostandroidphonepro JSON file. I need to have a conditional check here somehow. Don't know which variable will have that information for me to do a check on it. – LokiDroid Nov 30 '15 at 04:32
  • If I put the respective JSON files in the different flavors then it copies the JSON file to the build but I need it in the root folder of the package. Which is why I have the below code written copy { from 'src/findlostandroidphonepro/' include 'google-services.json' into '.' } – LokiDroid Dec 01 '15 at 07:11
0

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

Community
  • 1
  • 1
LokiDroid
  • 972
  • 10
  • 22