16

I'm currently trying out the Firebase analytics suit, but, i have faced one small issue, my app is distributed on both google play and amazon store (which doesn't support google play services), so for the amazon flavor i want to remove the dependency to Firebase (which i already know how to do), but, i also need to remove the Firebase plugin, so that it doesn't throw an exception while building.

This is what i have as far now:

productFlavors {
    google {
        applicationId 'google app id'
    }
    amazon {
        applicationId 'amazon app id'
    }
}

dependencies {
    googleCompile 'com.google.firebase:firebase-analytics:9.0.0'
    amazonCompile 'com.amazonaws:aws-android-sdk-mobileanalytics:2.2.12'
    amazonCompile('com.crashlytics.sdk.android:crashlytics:2.5.1@aar') {
        transitive = true;
    }
}

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

But, i need to remove the plugin only if is the amazon flavor.

Is this even possible? Or at least is there something close that i can try ?

UPDATE:

As per Steve request, i went and try the version with Firebase on my Amazon Kindle tablets and it does work even thou there's no Google Play Services installed on them.

Jorge Aguilar
  • 3,442
  • 30
  • 34

6 Answers6

12

As per Steve's answer Firebase analytics works even without Google play services. But we still can disable google services plugin for flavors.

Try add code like this:

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

 android.applicationVariants.all { variant ->
     if (!variant.name.contains("flavorName")) {
         project.tasks.each { t ->
             if (t.name.contains("GoogleServices")) {
                 // Remove google services plugin
                 variant.getVariantData().resourceGenTask.getTaskDependencies().values.remove(t);
                 // For latest gradle plugin use this instead
                 // variant.getVariantData().taskContainer.sourceGenTask.getTaskDependencies().getDependencies().remove(t)
             }
         }
     }
 }

Here I disable google services for all flavors which their name doesn't contains "flavorName". You should modify the conditions to fit your requirement. And notice that this should be added after apply plugin: 'com.google.gms.google-services'. Hope it helps.

Junyue Cao
  • 421
  • 6
  • 13
  • 2
    this does not seem to remove the permissions auto-added by firebase (INTERNET, WAKE_LOCK, C2D_MESSAGE, etc). Is there anything else I shoud add to the build file? – Angel Koh Mar 29 '17 at 10:16
  • Hi Angel, maybe we should remove play services dependencies for these flavors and make sure nothing of play services is used in these flavors. – Junyue Cao Mar 31 '17 at 02:08
  • Eventually, I used the method recommended here to remove the play services - http://stackoverflow.com/a/39761871/908821 – Angel Koh Mar 31 '17 at 02:33
  • I see the first comment of that answer, maybe it does not work. – Junyue Cao Apr 06 '17 at 09:57
9

I finally got a version to work with new gradle. Tested with gradle 4.6, build tools 3.0.1, google-services plugin 3.1.1

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

android.applicationVariants.all { variant ->
    if (variant.name == 'someVariantNameYouDontwantFirebase') {
        project.tasks.getByName('process' + variant.name.capitalize() + 'GoogleServices').enabled = false
    }
}
Jack Feng
  • 1,635
  • 1
  • 11
  • 7
2

Although Firebase does not officially support devices without Google Play services, Analytics should in fact work on such devices and so you may not actually need to disable Firebase (or remove the plugin) in your Amazon build. Have you tried it yet?

Steve Ganem
  • 10,591
  • 2
  • 39
  • 32
  • I went ahead and try it anyways, and it fails to send the data because it can't generate the instance id that it needs, and from what i gather is because it needs Google Play Services in order to generate that instance (Which all amazon devices don't have installed by default, you need to sideload it in order to get it to work). – Jorge Aguilar Jun 08 '16 at 15:31
  • That actually has no impact on the collection of Analytics. – Steve Ganem Jun 08 '16 at 16:37
  • Ok, i'll try plugin it to fiddler tonight just to make sure the data is going trough. Thanks. – Jorge Aguilar Jun 08 '16 at 23:25
  • it went trough, is there any way to disable all the other things so that it stops trying to generate that instance id? – Jorge Aguilar Jun 09 '16 at 14:44
  • That error should be harmless, but it will likely be addressed in a future SDK update. – Steve Ganem Jun 09 '16 at 20:37
1

It's possible that, because some Google Play Services libraries still need to be included in your firebase-free flavor, that some firebase related entries end up in the final merged AndroidManifest.xml.

So, if, in addition to removing the gradle tasks which were added by the Google Services plugin (as described in Junyue Cao's answer), you want to remove Firebase related receiver, service, provider, uses-permission or other tags from the final merged AndroidManifest, you can add node markers to the AndroidManifest.xml located in the app's flavor, build config, or build variant subdirectory.

If the node markers are set to "remove", then the corresponding receiver, service, provider, uses-permission tags will not be present in the final merged AndroidManifest.xml.

For example, here's what you might add to the AndroidManifest.xml in a project's hypothetical 'nofirebase' flavor source dir (app/src/nofirebase/):

    <receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
        tools:node="remove" />

    <receiver
        android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
        tools:node="remove" />

    <receiver
        android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver"
        tools:node="remove" />

    <receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
        tools:node="remove" >
    </receiver>

    <service
        android:name="com.google.android.gms.measurement.AppMeasurementService"
        tools:node="remove" />

    <service
        android:name="com.google.firebase.iid.FirebaseInstanceIdService"
        tools:node="remove"/>

    <provider
        android:name="com.google.firebase.provider.FirebaseInitProvider"
        android:authorities="com.you.yourapp.firebaseinitprovider"
        tools:node="remove"/>
albert c braun
  • 2,650
  • 1
  • 22
  • 32
  • Good point. Anyway the items change over time, so better study merged manifest in `app/build/intermediates/merged_manifests//AndroidManifest.xml`, and add any firebase/gms/crashlytics items to app's manifest with `tools:node="remove"`. – Pointer Null Oct 15 '19 at 10:46
0

With answers above I was receiving an error that task doesn't exist (?it was generated during build?). What worked for me was to simply ask tasks to correct themselves. In my case I was disabling Fabric on UAT builds.

tasks.all {

        if (it.name.contains("Uat") && (
                it.name.contains("GoogleServices") ||
                it.name.contains("fabric"))
        ){
            it.enabled = false
        }

}
robotoaster
  • 3,082
  • 1
  • 24
  • 23
0

My way to include firebase only for specific flavor (kotlin-based gradle script):

//somewhere at beginning of app's gradle file
var includeFirebase = true

...

//at flavors section
productFlavors {
        create("foss") {
            dimension = "appstore"
            //do not include firebase in the foss build
            includeFirebase = false

...

//at the end of script
if(includeFirebase) {
    plugins {
        id("com.google.gms.google-services")
        id("com.google.firebase.crashlytics")
    }
}
Fedir Tsapana
  • 1,283
  • 16
  • 19