发布于 2018-07-09 11:51:58
在您的build.gradle
(应用程序)文件中添加以下代码:
if (getGradle().getStartParameter().getTaskRequests()
.toString().contains("YourGCMFlavorName")){
apply plugin: 'com.google.gms.google-services'
}
N.B.:首字母风味名称必须在大写字母中
更新2021-11-23:请注意下面Prasoon的解决方案 .
发布于 2019-06-21 00:27:38
我也遇到了同样的问题--我有一个构建多个应用程序的项目,每个应用程序都有两个变体--一个是通过Google Play发布的,另一个是通过Google Play Services和Firebase API发布的,另一个变体是通过Web下载(主要用于AOSP设备),不能包括Google Play Services或Firebase API。
在我们的应用程序/build.gradle文件中,我们不想要任何看似不太明显的奇怪的状态测试,我们的意思是“如果变体的==网络不应用即插即用的google服务”。我们也不想要google-services.json文件的多份拷贝,即每个应用程序一个,我们想要一个包含为Google启用的所有应用程序包的副本。这是因为我们经常添加和删除应用程序,并希望将这些应用作为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'
}
分发维度有两个值--“存储”和"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'
}
最后,Google服务插件在build.gradle的末尾按照https://firebase.google.com/docs/android/setup中的指示在全球应用
apply plugin: 'com.google.gms.google-services'
发布于 2021-09-30 07:10:29
只需将依赖项应用于build.gradle中特定的构建风味定义
android{
productFlavors {
flavourName{
apply plugin: 'com.google.gms.google-services'
}
}
}
https://stackoverflow.com/questions/39651693
复制相似问题