Gradle中不同block间的引用如何传递?作用域问题?

dependencies {
//..
    if (!is_debug) {
        getModuleNameStartsWith {
            it.startsWith(":module")
        }.each {
            println("implementation  project ===> ${it}")
            implementation project(it)
        }
    }
}


def getModuleNameStartsWith(def start) {
    def settings = new File("${rootDir}/settings.gradle")
    //根据settings内容找到符合条件(start函数)的module
    return names
}

这是我的一个方法,目的是从 settings中找到合适的module然后在app中依赖他们。

我需要在dependencies blockimplementation project(it),如果把这个implementation放到getModuleNameStartsWith函数中就不行了


Could not find method implementation() for arguments [project ':module1'] on project ':app' of type org.gradle.api.Project. Open File

会提示Could not find method ,即使我在dependencies block中将this传入getModuleNameStartsWith.也不行。所以我应该怎么做?可以让我在函数中调用implementation()方法。

因为我想让我的函数改成这样。

//原函数
if (!is_debug) {
        getModuleNameStartsWith {
            it.startsWith(":module")
        }.each {
            println("implementation  project ===> ${it}")
            implementation project(it)
        }
    }
        dependModules(is_debug){
            it.startsWith(":module")
        }
        //...
        def getModuleNameStartsWith(def isDebug, def start)
阅读 1.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进