【问题标题】:Gradle add dependency throw the method dependencies.addGradle添加依赖抛出方法dependencies.add
【发布时间】:2015-04-08 16:19:59
【问题描述】:

我的计划是编写更少的样板代码,并且我计划做一个添加依赖项的方法,这是我的方法

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 的菜鸟,我试图在 gradle 文档中找到,并在 Github 上搜索,没有找到任何有用的东西)

【问题讨论】:

    标签: android groovy gradle


    【解决方案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")
            })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 2014-07-26
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多