在Eclipse中使用Gradle添加依赖关系

4

我正在使用Eclipse的Gradle IDE插件,我在build.gradle文件中创建了一个自定义任务:

sourceSets {
    unitTest {
        java.srcDirs = ['tests']
    }
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/debug")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'org.robolectric:robolectric:2.1.1'
    unitTestCompile 'com.google.android:android:4.0.1.2' 
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}

build.dependsOn unitTest

问题在于我引用的依赖项没有添加到Eclipse中,导致出现错误,无法解析import org.junit.Test;import org.robolectric.Robolectric;等等。我尝试了右键单击项目 -> Gradle -> 刷新依赖项,但没有效果。

1个回答

3

我自己找到了解决方案。将以下内容添加到build.gradle文件中:

apply plugin: 'eclipse'
eclipse {
    classpath {    
        plusConfigurations += configurations.unitTestCompile
      }
}

然后右键点击项目 -> Gradle -> 刷新依赖项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接