task ‘:app:xxxxx‘ without declaring an explicit or implicit dependency

目录
  • 我之前写过类似的gradle copy语法
  • 需求
  • 错误代码与提示
  • 解决问题
我之前写过类似的gradle copy语法

debug/release 修改包名,取不同包名下的agconnect-services.json 文件 V2
目的都是为了区分,正式环境使用正式的配置,测试环境使用测试的配置,当时的gradle 版本还是 4.+

需求

正式环境需要正式的域名,测试环境需要测试环境的域名

错误代码与提示

使用的是 gradle 7.3版本


    task copyHttpToRelease(type:Copy){
        from "src/httpconfig/release/"
        include "http.json"
        into "./src/main/assets"

    }
    
    task copyHttpToDebug(type:Copy){
        from "src/httpconfig/debug/"
        include "http.json"
        into "./src/main/assets"
    }

  
    task deleteAgconnecFile(type: Delete) {
        delete("src/main/assets/http.json")
    }

    afterEvaluate {
        tasks.matching {
            it.name == "assembleDebug" || it.name == "assembleRelease" || it.name == "bundleRelease"
        }.each {task->
            if(task.getName() == "assembleDebug" || task.getName() == "assembleRelease" || task.getName() == "bundleRelease"){
                if(task.getName() == "assembleDebug"){
                    task.dependsOn(copyHttpToDebug)
                }else{
                    task.dependsOn(copyHttpToRelease)
                }
            }
        }
    }

以上想法是想根据你使用的gradle命令去,拿不同的网络配置文件信息,但是会有如下的提示错误

Task :app:copyHttpToRelease
Execution optimizations have been disabled for task ':app:copyHttpToRelease' to ensure correctness due to the following reasons:
  - Gradle detected a problem with the following location: 'D:\companyProject\xxxxxxxxxx\android\app\src\main\assets'. 
Reason: Task ':app:mergeReleaseAssets' uses this output of task ':app:copyHttpToRelease' without declaring an explicit or implicit dependency.
 This can lead to incorrect results being produced, depending on what order the tasks are executed.

 Please refer to 'https://docs.gradle.org/7.3/userguide/validation_problems.html#implicit_dependency' for more details about this problem.

按提示的说法,:app:mergeReleaseAssets 任务去执行另外一个任务–> :app:copyHttpToRelease 的时候不是一个明确的申明方式,我也很好奇,为啥会说这样的事情,但是从我目前的角度来说,这个已经写的很明确了呀。。。。 当然也给了你网站让你看 说明文档,我看完后,还是没有理解到位,感觉像是强调任务的依赖性

解决问题
tasks.register('copyHttpToRelease') {
        doFirst {
            copy {
                from "src/httpconfig/release/http.json"
                into "./src/main/assets"
            }
        }
    }

    tasks.register('copyHttpToDebug') {
        doFirst {
            copy {
                from "src/httpconfig/debug/http.json"
                into "./src/main/assets"
            }
        }
    }

    //删除文件
    tasks.register('deleteAgconnecFile', Delete) {
        delete("src/main/assets/http.json")
    }

    afterEvaluate {
        tasks.matching {
            it.name == "assembleDebug" || it.name == "assembleRelease" || it.name == "bundleRelease"
        }.each {task->
            if(task.getName() == "assembleDebug" || task.getName() == "assembleRelease" || task.getName() == "bundleRelease"){
                if(task.getName() == "assembleDebug" && isDeBugBuildType()){
                   task.dependsOn(copyHttpToDebug)
                }else{
                   task.dependsOn(copyHttpToRelease)
                }
            }
        }
    }

def isDeBugBuildType(){
    boolean isDebugTypes = false;
    for(String s : gradle.startParameter.taskNames) {
        if (s == "assembleDebug" || s == "bundleDebug" || s == ":app:assembleDebug" || s == ":app:bundleDebug") {
            isDebugTypes = true;
            break;
        }
    }
    return isDebugTypes;
}
我的博客

简书
csdn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值