简体   繁体   English

build.gradle如何应用另一个文件的闭包

[英]build.gradle how to apply a closure from another file

I have a closure of defined in a another build.gradle file called other.gradle . 我在另一个名为other.gradle的 build.gradle文件中关闭了define Here is the contents: 这里是内容:

Closure callback =  {

    productFlavors {  
        ...
        devel {
            ...
        }

        prod {
            ...
        }
    }
}

Now in my build.gradle file I want to call this closure like this: 现在在我的build.gradle文件中,我想这样称呼这种闭合:

apply from: 'other.gradle'
productFlavors(callback());

but I keep getting an error that callback() cant be found. 但我不断收到错误消息,找不到callback()。 Both files are in the same directory. 这两个文件都在同一目录中。 My issue is how do i get the build.gradle file to see the callback closure in the 'other.gradle' file. 我的问题是如何获取build.gradle文件以查看“ other.gradle”文件中的回调关闭。

It should be done in the following way: 应该通过以下方式完成:

other.gradle 其他等级

project.ext.callback = { c ->
    println(c)
}

build.gradle build.gradle

apply from: 'other.gradle'

callback('a')

Or in same cases callback should be referred via project.instance , eg project.callback('a') . 或者在相同情况下, callback应通过project.instance引用,例如project.callback('a')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM