我曾遇到同样的问题 - 我有一个项目,构建了多个应用程序,每个应用程序都有两个变体,一个通过Google Play分发并使用Google Play服务和Firebase API,另一个变体通过Web下载(主要供AOSP设备使用),不能包含任何Google Play服务或Firebase API。
在我们的app/build.gradle文件中,我们不想要任何看起来不太明显的奇怪条件测试,也就是说,“如果变体==web,则不应用插入Google Play服务”。我们也不想要google-services.json文件的多个副本,即每个应用程序一个,我们希望有一个包含启用了Google Play服务的所有应用程序包的文件。这是因为我们经常添加和删除应用程序,并希望将这些应用程序作为Firebase控制台中的一个项目来管理。
解决方法是创建一个分发维度,这个维度必须放在flavorDimensions数组的第一位( com.google.gms.google-services插件只会查找第一个维度的google-services.json)。
flavorDimensions 'distribution', 'application'
productFlavors {
store {
dimension 'distribution'
}
web {
dimension 'distribution'
applicationIdSuffix ".nogms"
}
app1 {
dimension 'application'
applicationId 'com.example.app1'
}
app2 {
dimension 'application'
applicationId 'com.example.app2'
}
发布维度有两个值 - “store”和“web”
Firebase控制台生成的google-services.json文件被放置在app/src/store目录中。
在app/src/web目录中,我们放置了一个带有以下内容的虚拟google-services.json文件:
{
"project_info": {
"project_number": "0",
"project_id": "api-project-0"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:0:android:0",
"android_client_info": {
"package_name": "com.example.app1.nogms"
}
},
"api_key": [
{
"current_key": "none"
}
]
},
{
"client_info": {
"mobilesdk_app_id": "1:0:android:0",
"android_client_info": {
"package_name": "com.example.app2.nogms"
}
},
"api_key": [
{
"current_key": "none"
}
]
}
]
}
这可以让插件保持正常运作。(非GMS应用程序包需要根据需要添加到"client":[]数组中)。
GMS和Firebase库只有在商店风味中条件地包含:
dependencies {
storeImplementation 'com.google.firebase:firebase-core:16.0.8'
storeImplementation 'com.google.firebase:firebase-iid:17.1.2'
storeImplementation 'com.google.firebase:firebase-messaging:17.6.0'
}
最后,根据https://firebase.google.com/docs/android/setup中的指示,在build.gradle文件末尾全局应用了Google Play服务插件。
apply plugin: 'com.google.gms.google-services'
android.applicationVariants.all{ it.variantData.getVariantConfiguration().getFullName().capitalize(); //if here}
? - Leonard Arnold