首页 > 解决方案 > 如何在gradle中为任务设置绝对路径

问题描述

我有这样的基本任务

task copy-Library(type: Copy) {
    from 'build/outputs/aar/app-debug_-debug.aar'
    into "D:\\root\\path\\to\\directory\\Plugins\\Android"
    rename { String fileName ->
        fileName.replace("app-debug_-debug.aar", "myLibray-debug.aar")
    }
}

它工作正常

但我想将路径"D:\\root\\path\\to\\directory\\Plugins\\Android"放入某个变量中,以便我可以从其他任务中调用

into myPath

我猜我的 gradle 任务会以这样的方式结束

\\ set the path variable
how.do.I.set myPath = "D:\\root\\path\\to\\directory\\Plugins\\Android"

task copy-Library(type: Copy) {
    from 'build/outputs/aar/app-debug_-debug.aar'
    into myPath
    rename { String fileName ->
        fileName.replace("app-debug_-debug.aar", "myLibray-debug.aar")
    }
}

这样我就可以添加另一个使用相同路径的任务?

task deleteLibrary(type: Delete){
    delete fileTree(myPath) {
        include '**/*.ext'
    }
}

标签: androidgradle

解决方案


我认为def这就是你想要的:

def myPath  = "D:\\root\\path\\to\\directory\\Plugins\\Android" //define variable myPath 



task copy-Library(type: Copy) {
    from 'build/outputs/aar/app-debug_-debug.aar'
    into myPath   //"$myPath" also should work
    rename { String fileName ->
        fileName.replace("app-debug_-debug.aar", "myLibray-debug.aar")
    }
}

task deleteLibrary(type: Delete){
    delete fileTree($myPath) {
        include '**/*.ext'
    }
}

writing_build_scripts


推荐阅读