11

In my Android project, there are several product flavors:

buildTypes {
    release {}
    debug {}
    staging {}
}

productFlavors {
    freeVersion {}
    proVersion {}
    partnerVersion {}
}

Also, i use Google Analytics:

apply plugin: 'com.google.gms.google-services'

dependencies {
    compile 'com.google.android.gms:play-services-analytics:8.4.0'
}

How to exclude google-services in one of them? For example, in:

freeVersion {}
user3634297
  • 143
  • 2
  • 8
  • I have'nt done it before but [Gradle Plugin User Guide](http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-Flavor-Configuration) may be helpful for you. – iSandeep Feb 02 '16 at 15:36

3 Answers3

15

Another solution is to disable the task the google-services plugin adds - here I enable the task if the flavorName is not "freeVersion" but this logic can clearly be updated to say look at the variants buildType instead.

apply plugin: 'com.google.gms.google-services'

// must be after the plugin is applied otherwise no tasks will be found
android.applicationVariants.all { variant ->
    def googleTask = tasks.findByName("process${variant.name.capitalize()}GoogleServices")
    googleTask.enabled = !"freeVersion".equals(variant.flavorName)
}
appmattus
  • 2,788
  • 25
  • 16
  • 1
    This should be the accepted answer IMO. You can also print the flavorName out to the build console window in case you're having issues identifying it. – Thomas Sunderland Dec 07 '21 at 14:38
12

Please notice the use of freeCompile and declaring a variable flavor to conditionally apply the plugin.

apply plugin: 'com.android.application'

def flavor

android {

    ....

    ....

    productFlavors {
        free {
            applicationId "com.anandbibek.builditbigger.free"
            flavor = "free"
        }
        paid {
            applicationId "com.anandbibek.builditbigger.paid"
            flavor = "paid"
        }
    }
}

dependencies {

    // Added for AdMob
    freeCompile 'com.google.firebase:firebase-ads:9.6.1'

    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.code.findbugs:jsr305:2.0.1'

}
if(flavor == "free") {
    apply plugin: 'com.google.gms.google-services'
}

Make sure you put the google-services.json file in flavor specific folder. In my case, I put that in app/src/free only . This facility is available when you use classpath 'com.google.gms:google-services:3.0.0' in your main project gradle file.

anandbibek
  • 1,659
  • 16
  • 20
  • 3
    This doesn't (no longer?) works, as the value will always be set to the last defined flavor :-( – Kelevandos Oct 25 '16 at 13:02
  • Still works for me. The value of `flavor` variable is decided when gradle tasks run. Maybe you are running both flavors and accessing the variable later on? – anandbibek Oct 25 '16 at 19:14
  • 1
    I tried logging it and actually all 3 flavors are called, one after another, and the final value always ends up being the one set in the last module :( The if code at the bottom is only called once though, alter those 3 :( – Kelevandos Oct 25 '16 at 19:16
  • I'm not an expert in gradle tasks, but how I did was use AssembleFreeRelease to get apk, then another click on AssemblePaidRelease. You can find the clickable tasks in gradle window on right side of Android Studio. – anandbibek Oct 25 '16 at 19:18
  • I tried this recently and it doesn't work for me either. It seems @anandbibek was right, the value gets assigned twice in a single gradle sync so flavor is always the last value assigned. Has anyone found a different solution? – Naveen Dissanayake Feb 19 '18 at 05:36
  • Recent gradle changes have possibly refactored the way it is handled. Have to investigate further on latest plugin – anandbibek Feb 26 '18 at 07:53
-2

I misread the question initially. To exclude the free version you would use proVersionCompile and partnerVersionCompile with the desired dependency to exclude the freeVersion.

dependencies {
    proVersionCompile 'com.google.android.gms:play-services-analytics:8.4.0'
    partnerVersionCompile 'com.google.android.gms:play-services-analytics:8.4.0'
}
Joopkins
  • 1,606
  • 10
  • 16
  • It does not work. Can I disable the dependency, but I can not turn off gradle plugin (apply plugin: 'com.google.gms.google-services'), which each time looking for google-services.json at compile time. – user3634297 Feb 03 '16 at 09:50
  • This is a problem with multiple flavors and google-services.json. Use apply plugin com.google.gms:google-services:2.0.0-alpha3. Then it supports you placing the google-services.json file in each flavor directory. app/src/ freeVersion/google-services.json proVersion/google-services.json partnerVersion/google-services.json – Joopkins Feb 03 '16 at 11:46
  • No. It doesnt work. Error:(135, 0) Plugin with id 'com.google.gms:google-services:2.0.0-alpha3' not found. – user3634297 Feb 03 '16 at 13:10
  • From official guide: Add the dependency to your project-level build.gradle: classpath 'com.google.gms:google-services:2.0.0-alpha5' Add the plugin to your app-level build.gradle: apply plugin: 'com.google.gms.google-services' – user3634297 Feb 03 '16 at 13:12
  • https://developers.google.com/android/guides/google-services-plugin?hl=en take a look at this link and the JSON file info. – Joopkins Feb 03 '16 at 13:13
  • I believe I have misunderstood this question to be about dependencies rather than the plugin. I'm not sure there is a way to have flavor specific plugins without setting up a custom gradle task and preferences. – Joopkins Feb 03 '16 at 13:16
  • http://stackoverflow.com/questions/31379795/how-to-apply-plugin-to-only-one-flavor-in-gradle Not a definitive answer but an example of what you may be able to come up with. – Joopkins Feb 03 '16 at 13:21
  • I found it. It looks like a crutch :D – user3634297 Feb 03 '16 at 14:49