更新:GCM已弃用,使用FCM

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

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

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


当前回答

我们对调试构建有不同的包名(*.debug),所以我想要一些基于flavor和buildType的东西,而不必在processDebugFlavorGoogleServices的模式中编写任何与flavor相关的东西。

我在每个版本中创建了一个名为“google-services”的文件夹,包含了json文件的调试版本和发布版本:

在gradle文件的buildTypes部分,添加以下内容:

    applicationVariants.all { variant ->
            def buildTypeName = variant.buildType.name
            def flavorName = variant.productFlavors[0].name;

            def googleServicesJson = 'google-services.json'
            def originalPath = "src/$flavorName/google-services/$buildTypeName/$googleServicesJson"
            def destPath = "."

            copy {
                if (flavorName.equals(getCurrentFlavor()) && buildTypeName.equals(getCurrentBuildType())) {
                    println originalPath
                    from originalPath
                    println destPath
                    into destPath
                }
            }
    }

当你切换构建变量时,它会自动在你的应用模块的根处复制正确的json文件。

在build.gradle的根中添加这两个被调用的方法,以获取当前风格和当前构建类型

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

    Pattern pattern;

    if( tskReqStr.contains( "assemble" ) )
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher( tskReqStr )

    if( matcher.find() ) {
        println matcher.group(1).toLowerCase()
        return matcher.group(1).toLowerCase()
    }
    else
    {
        println "NO MATCH FOUND"
        return "";
    }
}

def getCurrentBuildType() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

        if (tskReqStr.contains("Release")) {
            println "getCurrentBuildType release"
            return "release"
        }
        else if (tskReqStr.contains("Debug")) {
            println "getCurrentBuildType debug"
            return "debug"
        }

    println "NO MATCH FOUND"
    return "";
}

就是这样,你不必担心从你的gradle文件中删除/添加/修改口味,它得到调试或发布谷歌服务。自动json。

其他回答

我正在使用谷歌服务。json文件,从这里创建:https://developers.google.com/mobile/add?platform=android&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fandroid%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue

在json结构中,有一个称为客户机的json数组。如果你有多种口味,只需在这里添加不同的属性。

{
  "project_info": {
    "project_id": "PRODJECT-ID",
    "project_number": "PROJECT-NUMBER",
    "name": "APPLICATION-NAME"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR1",
        "client_id": "android:PACKAGE-NAME-1",
        "client_type": 1,
        "android_client_info": {
          "package_name": "PACKAGE-NAME-1"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    },
    {
      "client_info": {
        "mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR2",
        "client_id": "android:PACKAGE-NAME-2",
        "client_type": 1,
        "android_client_info": {
          "package_name": "PACKAGE-NAME-2"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    }
  ],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}

在我的项目中,我使用相同的项目id,当我在上面的url中添加第二个package-name时,谷歌为我提供了一个包含json-data中的多个客户端的文件。

事实上,只有一个谷歌——服务。MyApp/app/目录下的com.google.gms:google-services:3.0.0是很好的,不需要额外的脚本。但是要小心删除文件google-services。为了避免错误类型为“:app:processDebugGoogleServices”的任务执行失败,请从应用目录MyApp/app/src/flavor1/res/”中删除。没有为包找到匹配的客户端

简化@Scotti说的话。您需要为特定项目创建多个具有不同包名的应用程序,具体取决于产品风格。

假设您的项目是ABC,具有不同的产品风味X,Y,其中X有一个包名com。x和Y有一个包名为com。然后在firebase控制台中,你需要创建一个项目ABC,其中你需要创建2个包名为com的应用程序。X和com.y。然后你需要下载谷歌服务。Json文件中有2个client-info对象,其中包含这些包,你就可以开始了。

json的代码片段如下所示

{
  "client": [
    {
      "client_info": {
        "android_client_info": {
          "package_name": "com.x"
        }

    {
      "client_info": {
        "android_client_info": {
          "package_name": "com.y"
        }
      ]

    }

根据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>

根据@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。替换您所使用的风味名称。