背景:

我试图为我的应用程序设置谷歌分析(有4个自定义buildTypes和多个productFlavors)

当我选择有applicationId设置为com.my.app.package.name(生成google-services.json时使用的包名)的Build Variant时,它工作得很好。但是,我的其他口味有不同的应用id。

我按照官方的开发指南来设置它。

当在build variables选项卡中选择任何其他构建变量时(具有不同的applicationId(包名)),我得到的错误如下:

错误:任务执行失败 “应用程序:processAllcategoriesDebugfreeGoogleServices”。 没有为包名'com.my.app.package.name.debug'找到匹配的客户端

错误信息解释:

在上面错误消息中的任务名称中,Debugfree是我的自定义buildType, Allcategories是我的productFlavors之一。

我理解这个错误,知道包的名称与我在生成json时使用的不同,但这就是我试图解决的问题。

问题:

现在,谷歌的服务。json驻留在app文件夹中,因此我无法通过将它们放入特定于风味的源集文件夹中将它们分离出来。

问题:

My custom byildType for debugging suffixes .debug to the applicationId (package name). Is there a way I can make it work with the google-services.json which has my release applicationId (i.e. without the suffix .debug) Is there a way to have multiple product flavors configured in the same google-services.json file without using separate files and tasks to copy the required file to app folder. I know it can be done using task as mentioned in this post. Is there a simpler way to just have one file or pick right file based on gradle configuration? I see the package_name field in google-services.json has the package name com.my.app.package.name. Can I add multiple package names manually to work for all build variants? If yes, how to do that? Could not find any instructions in documentations.


当前回答

首先,在Gradle中为应用程序包添加一个后缀:

yourFlavor {
   applicationIdSuffix "yourFlavor"
}

这将使您的包被命名

com.domain.yourapp.yourFlavor

然后转到当前项目下的Firebase,用这个包名添加另一个android应用程序。

然后替换现有的谷歌服务。Json文件,其中包含你生成的新应用程序。

然后,您将得到具有多个“client_info”部分的内容。所以你应该得到这样的结果:

"client_info": {
    "mobilesdk_app_id": "YOUR APP INFO",
    "android_client_info": {
      "package_name": "com.domain.yourapp"
    }
  }

"client_info": {
    "mobilesdk_app_id": "YOUR APP INFO - will be different then other app info",
    "android_client_info": {
      "package_name": "com.domain.yourapp.yourFlavor"
    }
  }

其他回答

当您在项目中下载的谷歌Services Json文件的包名称与项目的包名称不匹配时,会发生此错误。

最近重命名的项目包名称?

它会更新所有的类,但不会更新Gradle File。所以检查你的包名在Gradle中是否正确,Manifest也可以。

如果你的包名在Google-services上。Json文件, 去gradle。属性文件。然后更改您的APPLICATION_ID="com.yourpackage.name"

我找到了解决方案:

我们必须在firebase项目中创建两个“应用程序”,然后我们可以从任何一个应用程序下载json文件(我从调味应用程序下载)。它将包含两个应用程序的数据。我们可以简单地将json文件复制到应用程序的根目录。

来源:https://firebase.googleblog.com/2016/08/organizing-your-firebase-enabled-android-app-builds.html

解决这个问题很简单:只要确保你的app/build中的包名。gradle文件与你的App Manifest文件中的包名相同。并确保包名称正确。这就能解决问题了

就我而言,我忘记了谷歌服务。Json在应用程序根目录,这是不同的口味。它在默认情况下使用,而不是使用存储在flavor文件夹中的json。这个混乱的插件,因为包名不匹配,它失败了。

我教的智慧是,检查所有的谷歌服务。Json在你的项目结构中,当这个问题发生时。