我的计划是编写更少的样板代码,并且我已经计划做一个会添加依赖的方法抛出它,这是我的方法
project.ext{
forEachWithKNp1 = { String[] items, Closure op ->
final int length = items.length / 2;
for(int index = 0; index < length; index++){
op(items[index], items[index + 1]);
}
}
addSquareUpArtifacts = { String... names ->
forEachWithKNp1(names, { name, version ->
project.dependencies {
getDependencies().add("compile", "com.squareup.$name:$name:$version")
}
})
}
}
当我尝试同步项目时,错误正在发生
Error:(32, 0) A problem occurred evaluating project ':my-project'.
我做错了什么? (我在groovy和gradle中是noobie,我试图在gradle文档中找到,并且搜索了Github,没有找到任何有用的东西)
答案 0 :(得分:-1)
我做了一些研究,所以我理解什么是错的,在addSquareUpArtifacts
方法中我使用变量project
这是根项目,而不是我应该使用指定项目来添加依赖
这是最终解决方案
project.ext{
forEachWithKNp1 = { String[] items, Closure op ->
final int length = items.length / 2;
for(int index = 0; index < length; index++){
op(items[index], items[index + 1]);
}
}
addSquareUpArtifact = {String projectPath, String... names ->
final Project target = project.project(projectPath)
forEachWithKNp1(names, { name, version ->
target.getDependencies().add('compile', "com.squareup.$name:$name:$version")
})
}
}