如何在gradle中为任务设置绝对路径

时间:2019-09-27 15:36:17

标签: android 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'
    }
}

1 个答案:

答案 0 :(得分:0)

我认为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