Can i disable Firebase plugin for specific flavor?

半腔热情 提交于 2019-12-21 03:43:11

问题


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.


回答1:


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.




回答2:


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



回答3:


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?




回答4:


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"/>



回答5:


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
        }

}


来源:https://stackoverflow.com/questions/37693732/can-i-disable-firebase-plugin-for-specific-flavor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!