背景:

我试图为我的应用程序设置谷歌分析(有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.


当前回答

进入你的项目目录,在app/src路径上创建文件夹,命名你的产品口味(如Alpha, Beta, Store或任何你的口味的名字)。然后复制你的谷歌服务。Json文件到每个文件夹。不要忘记在每个产品类型的google服务上更改“package_name”字段。子文件夹的Json文件。

其他回答

如果有人改变了他们现有的包名,那么有时会出现这样的问题,所以你应该改变本地包名,build。Gradle文件包名称以及你的项目是否有任何google服务。json文件,那么你只需要改变你的json文件包的名称。

旧档案是这样的

"client": [
{
  "client_info": {
    "mobilesdk_app_id": "1:517104510151:android:8b7ed3989db24a6f928ae2",
    "android_client_info": {
      "package_name": "com.techsajib.oldapp"
    }
  },

新建文件是这样的

"client": [
{
  "client_info": {
    "mobilesdk_app_id": "1:517104510151:android:8b7ed3989db24a6f928ae2",
    "android_client_info": {
      "package_name": "com.techsajib.newapp"
    }
  },

注意:有时JSON文件有几个包名,所以要全部更改

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.package.name"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

确保Gradle。app的applicationId与你的包名相同。我的问题是这样的,我用这种方法解决了

1.验证包名称和应用程序ID应该相同

2.验证谷歌服务。Json包名,可在应用程序文件夹

在你的谷歌服务上检查你的包名。Json,它应该与你的应用程序的本地包名相同

例子

"client_info": {
    "mobilesdk_app_id": "1:6596814400689:android:65d6f25f5006145",
    "android_client_info": {
      "package_name": "com.my.app.package.name"
    }

在这种情况下,我的本地包名称是com.my.app.package.name,所以我也在google-services中更改了我的包名称。Json和我的本地包名

如果您的applicationId在您的构建类型之间是不同的,您可以将那些特定的工件添加到特定的依赖类型中,例如releaseImplementation。