嘿,朋友们,还要注意使用小写字母的名称,这样就不会出现错误了。
简单回答:
google-services.json
复制到 app 目录。对于其他的 flavor,请将 google-services.json
复制到 app/src/{flavor-name} 目录中。
实际上,在MyApp/app/
目录中只需要一个google-services.json文件即可,无需额外使用com.google.gms:google-services:3.0.0
的脚本。但是请注意删除应用程序目录MyApp/app/src/flavor1/res/
中的文件google-services.json
,以避免出现错误类型Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package
我目前在同一个应用程序包中使用两个 GCM 项目 ID。我放置了第一个 GCM 项目的 google-service.json,但我只需更改 SENDER_ID 就可以从第一个切换到第二个。
String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
我认为此时google-services.json并非强制必需品。
android{
// set build flavor here to get the right Google-services configuration(Google Analytics).
def currentFlavor = "free" //This should match with Build Variant selection. free/paidFull/paidBasic
println "--> $currentFlavor copy!"
copy {
from "src/$currentFlavor/"
include 'google-services.json'
into '.'
}
//other stuff
}
...
gradle.taskGraph.beforeTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.flavorName}(Debug|Release)GoogleServices/) {
copy {
from "src/tenants/${variant.flavorName}"
include 'google-services.json'
into '.'
}
}
}
}
}
gradle.taskGraph.afterTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.flavorName}(Debug|Release)GoogleServices/) {
delete fileTree(".").matching {
include 'google-services.json'
}
}
}
}
}
将您的 "google-services.json" 文件分别放置在 app/src/flavors 下,然后在应用程序的 build.gradle 中,在 android 下添加以下代码
gradle.taskGraph.beforeTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
copy {
from "/src/${variant.flavorName}"
into '.'
include 'google-services.json'
}
}
}
}
}
apply plugin: 'com.google.gms.google-services'
行似乎会将gcm
字符串放入app/build/generated/res/google-services/debug/values/values.xml
。 - Alexander Farber