草庐IT

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

coder 2023-12-04 原文

我正在尝试配置一个 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/

有关android - 将多种产品 flavor 与 flavor 维度组合时,文件 google-services.json 丢失的更多相关文章

  1. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  2. ruby - nanoc 和多种布局 - 2

    是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'

  3. ruby-on-rails - 如何使用 Rack 接收 JSON 对象 - 2

    我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":

  4. 神州数码无线产品(AC+AP)配置 - 2

    注意:本文主要掌握DCN自研无线产品的基本配置方法和注意事项,能够进行一般的项目实施、调试与运维AP基本配置命令AP登录用户名和密码均为:adminAP默认IP地址为:192.168.1.10AP默认情况下DHCP开启AP静态地址配置:setmanagementstatic-ip192.168.10.1AP开启/关闭DHCP功能:setmanagementdhcp-statusup/downAP设置默认网关:setstatic-ip-routegeteway192.168.10.254查看AP基本信息:getsystemgetmanagementgetmanaged-apgetrouteAP配

  5. 阿里云RDS——产品系列概述 - 2

    基础版云数据库RDS的产品系列包括基础版、高可用版、集群版、三节点企业版,本文介绍基础版实例的相关信息。RDS基础版实例也称为单机版实例,只有单个数据库节点,计算与存储分离,性价比超高。说明RDS基础版实例只有一个数据库节点,没有备节点作为热备份,因此当该节点意外宕机或者执行重启实例、变更配置、版本升级等任务时,会出现较长时间的不可用。如果业务对数据库的可用性要求较高,不建议使用基础版实例,可选择其他系列(如高可用版),部分基础版实例也支持升级为高可用版。基础版与高可用版的对比拓扑图如下所示。优势 性能由于不提供备节点,主节点不会因为实时的数据库复制而产生额外的性能开销,因此基础版的性能相对于

  6. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  7. ruby - 用 YAML.load 解析 json 安全吗? - 2

    我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("

  8. 语法类似于 GitHub Flavored Markdown 的 Ruby markdown 解释器? - 2

    我使用Jekyll运行博客,并认为我会解决RedcarpetMarkdown解释器,因为它是developedandusedbyGitHub.好吧,我只是碰巧遇到了一个错误,去检查问题,然后foundthis.Maintainersays,"Asyouprobablyhavenoticed(harharharhar)Idon'thavetimetomaintainRedcarpetanymore.It'snotapriorityforme(IfindMarkdownthoroughlyboring)andit'snotapriorityforGitHub,becausewenolong

  9. ruby-on-rails - 将 Amazon Simple Notification service SNS 与 ruby​​ 结合使用 - 2

    很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭9年前。我需要从基于ruby​​的应用程序使用AmazonSimpleNotificationService,但不知道从哪里开始。您对从哪里开始有什么建议吗?

  10. ruby-on-rails - Rails 渲染带有驼峰命名法的 json 对象 - 2

    我在一个简单的RailsAPI中有以下Controller代码:classApi::V1::AccountsControllerehead:not_foundendendend问题在于,生成的json具有以下格式:{id:2,name:'Simpleaccount',cash_flows:[{id:1,amount:34.3,description:'simpledescription'},{id:2,amount:1.12,description:'otherdescription'}]}我需要我生成的json是camelCase('cashFlows'而不是'cash_flows'

随机推荐