task ':app:xxxxx' without declar

2023-12-11  本文已影响0人  一个冬季

目录

我之前写过类似的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;
}
额外发现问题

使用上面的命令,会有一个奇怪的问题,你先跑 assembleDebug 模式,http.json会帮你放到assets文件下面,你翻阅debug.apk文件,可以发现是debug环境的配置,此时,你直接打release apk出来(assembleRelease 任务),会发现未将正式环境的http.json文件进行替换到apk里面,但是!!!你的project assets里面的http.json文件是更换了的。所以我严重怀疑有3点,
1、是 UP-TO-DATE ,也就是更新,但是没有更新到apk里面去,
2、是缓存问题导致,感觉跟1差不多
3、我执行的时机不对,或者说不应该在assembleRelease的任务下执行

再次解决问题
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 == "mergeReleaseAssets" || it.name == "mergeDebugAssets"
        }.each {task->
            if(task.getName() == "mergeReleaseAssets" || task.getName() == "mergeDebugAssets"){
                if(task.getName() == "mergeDebugAssets" &&  isDeBugBuildType()){
                     task.dependsOn(copyHttpToDebug)
                }else if(task.getName() == "mergeReleaseAssets"){
                     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;
}

上一篇下一篇

猜你喜欢

热点阅读