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

你可能感兴趣的:(android)