0

Whenever you add a new Android application to a Firebase project you are asked/reminded to apply the GPS plugin:

Plz applie plawg-in

Now, according to this Google Samples project that has mock and prod flavors for convenient testing, you can (or must) get rid of the mockRelease variant because... well we all know why. So a build file that implements this variants approach and use Firebase would look like:

build.gradle

android {
    ...

    buildTypes {
        release {
            ...
        }

        debug {
            applicationIdSuffix ".debug"
            ...
        }
    }

    flavorDimensions "environment"
    productFlavors {
        mock {
            applicationIdSuffix = ".mock"
            dimension "environment"
        }
        prod {
            dimension "environment"
        }
    }

    android.variantFilter { variant ->
        if (variant.buildType.name == 'release' && variant.flavors[0].name == 'mock') {
            variant.setIgnore(true)   // These
            variant.ignore = true     // both work
        }
    }
    ...
}

dependencies {
    ...
}

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

The problem with this setup is that you will need to create a Firebase project for each one of the generated package names, and as harmless as that is:

  • I don't wan't to
  • I can't because I don't own the project
  • My boss says I shouldn't
  • (INSERT ANOTHER VALID EXCUSE HERE)

So why don't we just selectively apply the plugin according to the variant's name? Easy-peasy Japanesy, right?

dependencies {
    ...
}

android.variantFilter { variant ->
    if (/*  whatever selective logic goes  here  */) {
        apply plugin: 'com.google.gms.google-services'
    }
}

Boom! The variant is not ignored anymore. After several painful hours I found out that the only way to get the variant ignored again is to remove the variant filter from the bottom. How is one supposed to apply the Google Services Plugin selectively to a variant AND be able to ignore one of more of them?

Chisko
  • 3,092
  • 6
  • 27
  • 45

1 Answers1

0

Turns out my answer didn't work as expected. Though the variant was ignored, the plugin was still being applied despite the logic seems to say the opposite. Kudos to Jack Feng for this answer which is the solution that works. The code is:

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

android.applicationVariants.all { variant ->
    if (variant.name == 'variantNameYouDontWantFirebaseOn') {
        project.tasks.getByName('process' + variant.name.capitalize() + 'GoogleServices').enabled = false
    }
}
Chisko
  • 3,092
  • 6
  • 27
  • 45