Android Studio: How to exclude google-services module in product flavor?
Asked Answered
N

5

12

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 {}
Nathanaelnathanial answered 2/2, 2016 at 14:13 Comment(1)
I have'nt done it before but Gradle Plugin User Guide may be helpful for you.Duero
A
16

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)
}
Antilles answered 28/3, 2018 at 19:33 Comment(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.Reifel
S
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.

Somewhat answered 29/9, 2016 at 5:11 Comment(6)
This doesn't (no longer?) works, as the value will always be set to the last defined flavor :-(Quarrelsome
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?Somewhat
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 :(Quarrelsome
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.Somewhat
I tried this recently and it doesn't work for me either. It seems @Somewhat 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?Overspread
Recent gradle changes have possibly refactored the way it is handled. Have to investigate further on latest pluginSomewhat
S
0

After trying these and other examples, none of the solutions have worked for me. I imagine that these responses are a bit outdated, considering that the answer is from 2018, and Gradle has changed a lot, especially with the migration from Groovy to Kotlin. In the end, the code that worked for me is the following:

val variantsWithoutFirebase = arrayListOf(
    "variantDebug", "variantStaging", "variantRelease"
)
afterEvaluate {
    android.applicationVariants.all { _ ->
        project.tasks.filter { task ->
            variantsWithoutFirebase.any { task.name.contains(it, true) }
        }.map { task ->
            if (
                task.name.contains("GoogleServices") ||
                task.name.contains("uploadCrashlyticsMapping")
            ) {
                // Remove google services plugin
                tasks.findByName(task.name)?.enabled = false
                println("Disable task: ${task.name}")
            }
        }
        false
    }
}
Syce answered 8/10, 2023 at 18:8 Comment(0)
G
0

Thanks for answer from @appmattus

This is my kts version:

android {
    androidComponents {
        onVariants { variant ->
            val googleTask =
                tasks.findByName("process${variant.name.replaceFirstChar(Char::uppercase)}GoogleServices")
            googleTask?.enabled = "debug" != variant.flavorName
        }
    }
}

Garate answered 6/5 at 10:47 Comment(0)
M
-3

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'
}
Microelectronics answered 2/2, 2016 at 16:2 Comment(8)
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.Nathanaelnathanial
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.jsonMicroelectronics
No. It doesnt work. Error:(135, 0) Plugin with id 'com.google.gms:google-services:2.0.0-alpha3' not found.Nathanaelnathanial
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'Nathanaelnathanial
developers.google.com/android/guides/… take a look at this link and the JSON file info.Microelectronics
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.Microelectronics
#31380295 Not a definitive answer but an example of what you may be able to come up with.Microelectronics
I found it. It looks like a crutch :DNathanaelnathanial

© 2022 - 2024 — McMap. All rights reserved.