我的计划是编写更少的样板代码,并且我计划做一个方法来添加依赖项,这是我的方法
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,没有发现有用的东西)
最佳答案
我做了一些研究,所以我理解了问题所在,在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")
})
}
}
关于android - Gradle添加依赖项抛出方法dependencies.add,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29520094/