Use Gradle function from other gradle file

こ雲淡風輕ζ 提交于 2021-02-20 19:23:31

问题


I want to split my 300 lines build.gradle logically into multiple build files to make it easier to maintain and extend.

As I've noticed, it is possible to split gradle tasks into multiple files and use them with:

apply from: "myGradleFile"

With this approach I sadly have no access to functions, defined in the second build script.

Is it also possible to split Gradle functions into multiple files?

Example:

Let's say I have my default build.gradle with a Task which uses a function

task doSomethingWithMyFunction {
    myFunction()
}

Now I have functions.gradle

def myFunction(){
}

So I want to access myFunction defined in functions.gradle from build.gradle


回答1:


It's not possible to be done with functions, but if you turn the functions into closures the following example will work well:

lol.gradle

project.ext.lolFunction = {
   println it
}

build.gradle

apply from: 'lol.gradle'

ext.lolFunction(1)                                                             



回答2:


See 59.4 at https://docs.gradle.org/current/userguide/organizing_build_logic.html

You can move your functions to a

buildSrc

directory.



来源:https://stackoverflow.com/questions/30796146/use-gradle-function-from-other-gradle-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!