更新:GCM已弃用,使用FCM

我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南

我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。

有没有办法让谷歌的服务。Json配置不同的许多口味?


当前回答

    ...
    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插件的目的是简化谷歌特性的集成。

因为它只从google服务中生成android资源。json文件,过于复杂的gradle逻辑否定了这一点,我认为。

因此,如果google文档没有说明特定的google特性需要哪些资源,我建议为每个相关的buildtype/flavor生成json文件,看看插件生成了哪些资源,然后手动将这些资源放入各自的src/buildtypeORflavor/res目录中。

删除对google-services插件和json文件的引用,就完成了。

有关google-services gradle-plugin内部工作原理的详细信息,请参阅我的另一个答案:

https://stackoverflow.com/a/33083898/433421

简短的回答:

实现: 默认情况下,您应该复制google-services。Json到app dir。

如果想要获得其他效果,可以复制谷歌的服务。Json到app/src/{flavor-name}目录

测试: 尝试构建,打开构建选项卡,然后使用解析json文件检查输出消息:.....

remove the existing google-services.json from your project. Build > Clean Project compile and run your app look at the error message that comes up to figure out where you can put your google-services.json..mine looked like this File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: C:\Users\username\Desktop\HelloWorld\app\src\devSuffixYes_EnvQaApistaging_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\devSuffixYes_EnvQaApistaging_Debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\devDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\devSuffixYes_EnvQaApistaging_\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\debug\devSuffixYes_EnvQaApistaging_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffixDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_Debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\envDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qaDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\apistaging_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\apistaging_Debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\apistaging_\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\google-services.json

注意:它还关心flavorDimensions中声明的顺序。我的是flavorDimensions“dev_suffix”,“environment”

根据@ZakTaccardi的回答,并且假设您不想在两个版本中都使用一个项目,请将此添加到构建的末尾。gradle文件:

def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'

task switchToStaging(type: Copy) {
    outputs.upToDateWhen { false }
    def flavor = 'staging'
    description = "Switches to $flavor $googleServicesJson"
    delete "$appModuleRootFolder/$googleServicesJson"
    from "${srcDir}/$flavor/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

task switchToProduction(type: Copy) {
    outputs.upToDateWhen { false }
    def flavor = 'production'
    description = "Switches to $flavor $googleServicesJson"
    from "${srcDir}/$flavor/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

afterEvaluate {
    processStagingDebugGoogleServices.dependsOn switchToStaging
    processStagingReleaseGoogleServices.dependsOn switchToStaging
    processProductionDebugGoogleServices.dependsOn switchToProduction
    processProductionReleaseGoogleServices.dependsOn switchToProduction
}

你需要src/staging/google-services文件。Json和src/production/google-services.json。替换您所使用的风味名称。

根据Firebase文档,你也可以使用字符串资源来代替google-services.json。

因为这个提供者只是读取已知名称的资源,另一种选择是直接将字符串资源添加到应用程序中,而不是使用谷歌Services gradle插件。你可以这样做: 从根build.gradle中移除google-services插件 删除google服务。来自项目的Json 直接添加字符串资源 删除应用插件:'com.google.gms。你的应用build.gradle中的Google-services

示例strings.xml:

<string name="google_client_id">XXXXXXXXX.apps.googleusercontent.com</string>
<string name="default_web_client_id">XXXX-XXXXXX.apps.googleusercontent.com</string>
<string name="gcm_defaultSenderId">XXXXXX</string>
<string name="google_api_key">AIzaXXXXXX</string>
<string name="google_app_id">1:XXXXXX:android:XXXXX</string>
<string name="google_crash_reporting_api_key">AIzaXXXXXXX</string>
<string name="project_id">XXXXXXX</string>