在build.gradle中找不到自定义方法,MissingMethodException

在build.gradle中找不到自定义方法,MissingMethodException,gradle,android-gradle-plugin,build.gradle,Gradle,Android Gradle Plugin,Build.gradle,我试图在build.gradle中调用自定义方法时出错 在build.gradle文件中: def func() { String str = "hello world!" str } buildscript { repositories { mavenCentral() } dependencies { String str = func() println "$str" ......

我试图在
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.

buildscript{}闭包的作用域不同。我是一个非常棒的noop,无法解释为什么——也许有人可以加上这个。但要实现目标,您可以在
buildscript
中定义
func

buildscript {
    func =  {
        String str = "hello world!"
        str
    }

    dependencies {
        str = func()
        println "$str"
    }
    repositories {
        mavenCentral()
    }
}