user3634297
user3634297

Reputation: 163

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

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 {}

Upvotes: 13

Views: 7120

Answers (5)

NanoNova
NanoNova

Reputation: 953

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
        }
    }
}

Upvotes: 0

AndroidZen
AndroidZen

Reputation: 606

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
    }
}

Upvotes: 0

appmattus
appmattus

Reputation: 2808

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)
}

Upvotes: 16

anandbibek
anandbibek

Reputation: 2594

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.

Upvotes: 12

Joopkins
Joopkins

Reputation: 1644

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'
}

Upvotes: -3

Related Questions