如何自动添加gradle依赖项

时间:2014-10-02 13:25:49

标签: java android build gradle dependencies

我想知道是否有任何gradle任务或任何其他工具添加依赖,例如。

dependencies {
   ... // so far dependencies    
   compile 'com.new.dependency:x.y"
}

build.gradle 文件中,而不是手动输入。

我正在尝试编写IntelliJ插件,该插件会自动将库添加到任何项目中。到目前为止,我需要分析/解析文档内容,这很乏味。

例如,当您转到IntelliJ中的项目设置并添加库时,代码会自动添加。

2 个答案:

答案 0 :(得分:4)

您可以将集合指定为依赖项,例如:

dependencies {
   compile ['org.slf4j:slf4j-api:1.7.5','org.apache.commons:commons-lang3:3.1']
}

这意味着您可以使用任何可以生成列表的内容。如果您的插件维护了如下文件:

<强> compile.dependencies:

org.slf4j:slf4j-api:1.7.5
org.apache.commons:commons-lang3:3.1

然后,您可以将依赖项包含在项目中,如:

dependencies {   
    compile file('compile.dependencies').readLines()
}

您的插件的用户必须知道将这些行添加到他们的build.gradle中。或者,更好的是,您可以将配置捆绑到包含文件中,例如:

subprojects() {
  dependencies {   
     if (file('compile.dependencies').exists()) {
       compile file('compile.dependencies').readLines()
     }

     if (file('runtime.dependencies').exists()) {
      runtime file('runtime.dependencies').readLines()
     }
  }
}

然后,您的用户只需使用“apply from:”来包含配置。

答案 1 :(得分:0)

没有这样的工具,也没有实用工具。可以使用tooling API向项目添加依赖项,但引入的更改不会序列化为配置(build.gradle)文件。