当我尝试在build.gradle
中调用自定义方法时出错。
在build.gradle
档案中:
def func() {
String str = "hello world!"
str
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
String str = func()
println "$str"
...... // Other code
}
}
我得到的错误如下:
Caused by: org.gradle.api.internal.MissingMethodException: Could not find method func() for arguments [] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@1c86d7b4.
答案 0 :(得分:1)
关于buildscript {}闭包的一切洞察力都有不同的范围。我是一个时髦的noop,无法解释为什么 - 也许有人可以添加它。但是,为实现目标,您可以做的是在func
中定义buildscript
:
buildscript {
func = {
String str = "hello world!"
str
}
dependencies {
str = func()
println "$str"
}
repositories {
mavenCentral()
}
}