使用MissingMethodException

时间:2016-04-21 09:48:24

标签: gradle android-gradle build.gradle

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

1 个答案:

答案 0 :(得分:1)

关于buildscript {}闭包的一切洞察力都有不同的范围。我是一个时髦的noop,无法解释为什么 - 也许有人可以添加它。但是,为实现目标,您可以做的是在func中定义buildscript

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

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