android - 将多种产品 flavor 与 flavor 维度组合时,文件 google-services.json 丢失

标签 android android-gradle-plugin build.gradle google-play-services

我正在尝试配置一个 Android 项目,将多种产品 flavor 与 flavor 维度相结合。

这是 build.gradle 的一个 fragment

android {
    ...
    flavorDimensions "vendor", "type"
    productFlavors {

        development {
            dimension "vendor"
        }

        production {
            dimension "vendor"
        }

        free {
            dimension "type"
        }

        paid {
            dimension "type"
        }
    }
    ...
}

我正在使用谷歌服务,我需要有 google-services.json 文件,我想为每个供应商准备一个不同的文件,一个用于开发 另一个用于生产

所以我在 app/src/development 中有一个 google-services.json,在 app/src/production 中有另一个。

构建时出现此错误:

File google-services.json is missing. The Google Services Plugin cannot function without it. 
  Searched Location: 
  <path>/app/src/developmentPaid/debug/google-services.json
  <path>/app/src/debug/developmentPaid/google-services.json
  <path>/app/src/developmentPaid/google-services.json
  <path>/app/src/debug/google-services.json
  <path>/app/google-services.json

根据错误,我需要在 app/src/developmentFreeapp/src/developmentPaid 中使用相同的 google-services.json另一个在 app/src/productionFreeapp/src/productionPaid 中。

使用 flavor 维度时,我需要在两个地方使用相同的文件吗?

编辑:

我最终通过为每个合并的 productFlavor 执行几个额外任务来解决这个问题。我在 android 之后添加了这段代码:

后评估{

android.productFlavors.all { flavor ->
    if (flavor.dimension == "vendor") {
        task("copy${flavor.name.capitalize()}GoogleServicesFile", type: Copy) {
            description = 'Switches to google-services.json depending on flavor'
            from "src/${flavor.name}"
            include "google-services.json"
            into "."
        }
        task("delete${flavor.name.capitalize()}GoogleServicesFile", type: Delete) {
            description = 'Delete google-services.json from base folder'
            delete "./google-services.json"
        }
    }
}

android.applicationVariants.all { variant ->
    def buildType = variant.buildType.name.capitalize()
    def typeFlavorName = variant.productFlavors.get(0).name.capitalize()
    def vendorFlavorName = variant.productFlavors.get(1).name.capitalize()
    def copyFileTaskName = "copy${vendorFlavorName}GoogleServicesFile"
    def deleteFileTaskName = "delete${vendorFlavorName}GoogleServicesFile"
    def processGoogleServicesTaskName = "process${typeFlavorName}${vendorFlavorName}${buildType}GoogleServices"
    tasks."${processGoogleServicesTaskName}".dependsOn "${copyFileTaskName}"
    tasks."${processGoogleServicesTaskName}".finalizedBy "${deleteFileTaskName}"
}

这样,对于每个 typeFlavorNamevendorFlavorNamebuildType 变体,google-services.json 文件在 process{typeFlavorName}{vendorFlavorName}{buildType}GoogleServices 之前复制到 app/ 并在之后删除。

最佳答案

我最终通过为每个合并的 productFlavor 执行几个额外任务来解决这个问题。我在 android 之后添加了这段代码:

afterEvaluate {

    android.productFlavors.all { flavor ->
        if (flavor.dimension == "vendor") {
            task("copy${flavor.name.capitalize()}GoogleServicesFile", type: Copy) {
                description = 'Switches to google-services.json depending on flavor' from "src/${flavor.name}"
                include "google-services.json" into "."
            }
            task("delete${flavor.name.capitalize()}GoogleServicesFile", type: Delete) {
                description = 'Delete google-services.json from base folder'
                delete "./google-services.json"
            }
        }
    }

    android.applicationVariants.all { variant ->
        def buildType = variant.buildType.name.capitalize()
        def typeFlavorName = variant.productFlavors.get(0).name.capitalize()
        def vendorFlavorName = variant.productFlavors.get(1).name.capitalize()
        def copyFileTaskName = "copy${vendorFlavorName}GoogleServicesFile"
        def deleteFileTaskName = "delete${vendorFlavorName}GoogleServicesFile"
        def processGoogleServicesTaskName = "process${typeFlavorName}${vendorFlavorName}${buildType}GoogleServices"
        tasks."${processGoogleServicesTaskName}".dependsOn "${copyFileTaskName}"
        tasks."${processGoogleServicesTaskName}".finalizedBy "${deleteFileTaskName}"
    }
}

这样,对于每个 typeFlavorNamevendorFlavorNamebuildType 变体,google-services.json 文件在 process{typeFlavorName}{vendorFlavorName}{buildType}GoogleServices 之前复制到 app/ 并在之后删除。

关于android - 将多种产品 flavor 与 flavor 维度组合时,文件 google-services.json 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646311/

相关文章:

java - svg 亮度/透明度效果和滤镜 (Java Android)

android - 错误 :(47, 35) 找不到与给定名称匹配的资源(在 'resource' 处,值为 '@xml/searchable')

gradle - 我怎样才能只用一种产品口味呢?

android - 如何解决与相同依赖项的多个版本的冲突

maven - Gradle:将子模块的发布任务作为依赖添加到另一个子模块

用于大屏幕 xhdpi 的 android 模拟器无法正常工作

android - 我如何修复 ssl 异常错误并在 android webview 中加载 https

java - 改造:java.net.ProtocolException:未知方法 'PATCH'

android - Gradle 即使在第一次运行时也无法同步

Android 无法识别变体 arm-debug 和设备的 apk