我目前正在试用 Firebase 分析套件,但是,我遇到了一个小问题,我的应用程序同时分布在 google play 和 amazon store(不支持 google play 服务)上,所以我想要亚马逊风格删除对 Firebase 的依赖(我已经知道该怎么做),但是,我还需要删除 Firebase 插件,这样它在构建时不会抛出异常。
这是我目前所拥有的:
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'
但是,只有亚马逊口味的时候我才需要删除插件。
这可能吗?或者至少有什么接近我可以尝试的东西吗?
更新:
应史蒂夫的要求,我去试用了我的亚马逊 Kindle 平板电脑上的 Firebase 版本,即使没有安装 Google Play 服务,它也能正常工作。
根据史蒂夫的回答,即使没有谷歌,Firebase 分析也能正常工作 玩服务。但我们仍然可以禁用谷歌服务插件 口味。
尝试添加这样的代码:
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)
}
}
}
}
在这里,我禁用了他们名字中所有口味的谷歌服务 不包含“flavorName”。您应该修改条件以适应 你的要求。 注意这个应该加在之后
。希望对您有所帮助。apply plugin: 'com.google.gms.google-services'
我终于得到了一个可以与新 gradle 一起工作的版本。使用 gradle 4.6、构建工具 3.0.1、google-services 插件 3.1.1 进行测试
应用插件:'com.google.gms.google-services' android.applicationVariants.all { 变体 -> if (variant.name == 'someVariantNameYouDontwantFirebase') { project.tasks.getByName('process' + variant.name.capitalize() + 'GoogleServices').enabled = false } }
虽然 Firebase 不正式支持没有 Google Play 服务的设备,但 Analytics 实际上应该可以在此类设备上运行,因此您实际上可能不需要在 Amazon 构建中禁用 Firebase(或删除插件)。你试过了吗?
有可能,因为某些 Google Play 服务库仍然需要包含 在您的 firebase-free 风格中,一些与 firebase 相关的条目最终会出现在最终合并的 AndroidManifest.xml 中。
所以,如果除了删除由 Google Services 插件添加的 gradle 任务(如 Junyue Cao's answer 中所述),您还想从最终的 Firebase 中删除与 Firebase 相关的接收器、服务、提供者、使用权限或其他标签合并 AndroidManifest,您可以将 node markers 添加到位于应用程序的 flavor、build config 或 build variant 子目录中的 AndroidManifest.xml。
如果节点标记设置为“remove”,则相应的receiver、service、provider、uses-permission标签将不会出现在最终合并的AndroidManifest.xml中。
例如,在项目假设的“nofirebase”风味源目录 (app/src/nofirebase/) 中,您可以将以下内容添加到 AndroidManifest.xml:
<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"/>
有了上面的答案,我收到了一个任务不存在的错误(?它是在构建过程中生成的?)。对我有用的是简单地要求任务自我纠正。在我的例子中,我在 UAT 构建上禁用了 Fabric。
tasks.all {
if (it.name.contains("Uat") && (
it.name.contains("GoogleServices") ||
it.name.contains("fabric"))
){
it.enabled = false
}
}
我仅针对特定风格(基于 kotlin 的 gradle 脚本)包含 firebase 的方法:
//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")
}
}