2016-04-21 148 views
1

我在嘗試調用build.gradle中的自定義方法時遇到錯誤。使用MissingMethodException在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.interna[email protected]1c86d7b4. 

回答

1

洞察一切的buildscript {}閉包有不同的範圍。我是一個常規的noop,並不能真正解釋爲什麼 - 也許有人可以添加這個。但是你可以做什麼來實現你的目標是在buildscript內定義func

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

    dependencies { 
     str = func() 
     println "$str" 
    } 
    repositories { 
     mavenCentral() 
    } 
} 
相關問題