Gradle robolectric 项目可以在 OSX 计算机上运行,​​但不能在 Linux 上运行

Gradle robolectric project works on OSX computer but not on Linux

我使用的是相同版本的 Gradle, 1.7 jdk 因此不完全相同的版本。

测试的主要抱怨是:

java.lang.NoClassDefFoundError: org/apache/maven/artifact/ant/DependenciesTask Caused by: java.lang.ClassNotFoundException: org.apache.maven.artifact.ant.DependenciesTask

我已尝试清理 gradle 缓存以便重新下载所有依赖项。 我也尝试清理项目。

这没有用。

哪些变量会影响 gradle 测试的执行?

对我来说有:

还有什么可以干扰? 插件版本是否依赖于平台

Executing task ':robolectric-tests:test' (up-to-date check took 0.432 secs) due to:
  Output file /home/ec2-user/repositories/MyProject/robolectric-tests/build/test-results has changed.
  Output file /home/ec2-user/repositories/MyProject/robolectric-tests/build/reports/tests has changed.
  Output file /home/ec2-user/repositories/MyProject/robolectric-tests/build/test-results/binary/test has changed.
Starting process 'Gradle Test Executor 1'. Working directory: /home/ec2-user/repositories/MyProject/robolectric-tests Command: /usr/java/jdk1.7.0_72/bin/java -Djava.security.manager=jarjar.org.gradle.process.internal.child.BootstrapSecurityManager -Dfile.encoding=UTF-8 -Duser.country=ES -Duser.language=ca -Duser.variant -ea -cp /home/ec2-user/.gradle/caches/2.3/workerMain/gradle-worker.jar jarjar.org.gradle.process.internal.launcher.GradleWorkerMain 'Gradle Test Executor 1'
Successfully started process 'Gradle Test Executor 1'
Gradle Test Executor 1 started executing tests.
Gradle Test Executor 1 finished executing tests.

com.tubtale.MyProject.robolectrictests.tests.RobTMyAndroidClassTest > testWhenActivityCreatedHelloTextViewIsVisible2 FAILED
    java.lang.NoClassDefFoundError: org/apache/maven/artifact/ant/DependenciesTask

        Caused by:
        java.lang.ClassNotFoundException: org.apache.maven.artifact.ant.DependenciesTask

com.tubtale.MyProject.robolectrictests.tests.RobTMyAndroidClassTest > testWhenActivityCreatedHelloTextViewIsVisible FAILED
    java.lang.NoClassDefFoundError: org/apache/maven/artifact/ant/DependenciesTask

2 tests completed, 2 failed
Finished generating test XML results (0.217 secs) into: /home/ec2-user/repositories/MyProject/robolectric-tests/build/test-results
Generating HTML test report...
Finished generating test html results (0.11 secs) into: /home/ec2-user/repositories/MyProject/robolectric-tests/build/reports/tests
:robolectric-tests:test FAILED
:robolectric-tests:test (Thread[main,5,main]) completed. Took 4.005 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':robolectric-tests:test'.
> There were failing tests. See the report at: file:///home/ec2-user/repositories/MyProject/robolectric-tests/build/reports/tests/index.html

* Try:
Run with --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':robolectric-tests:test'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:306)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.access0(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter.proceed(DefaultBuildExecuter.java:68)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:80)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:169)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Caused by: org.gradle.api.GradleException: There were failing tests. See the report at: file:///home/ec2-user/repositories/MyProject/robolectric-tests/build/reports/tests/index.html
    at org.gradle.api.tasks.testing.Test.handleTestFailures(Test.java:1118)
    at org.gradle.api.tasks.testing.Test.executeTests(Test.java:549)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:218)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:211)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:200)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:585)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:568)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
    ... 44 more


BUILD FAILED

我也在 Linux 上 运行 关注这个问题,但 OSX 没有。我通过添加

找到了解决方法

org.apache.maven:maven-ant-tasks:2.1.3

我的 'testCompile' 依赖项。

dependencies {
  repositories {
    mavenCentral()
  }
  testCompile 'junit:junit:4.12'
  testCompile 'org.apache.maven:maven-ant-tasks:2.1.3'
  testCompile 'org.robolectric:robolectric:2.4'
  testCompile 'org.mockito:mockito-all:1.9.5'
}

我不知道为什么 linux 没有获取 maven-ant-tasks。它似乎是 robolectric.

的有效依赖项

所选答案正确,在 OSX 上将 Gradle 从 2.2.1 升级到 2.3 时也适用。如果您不添加 testCompile 'org.apache.maven:maven-ant-tasks:2.1.3' 的依赖项,那么 Robolectric 测试将在 Gradle 版本为 2.3 时失败。

另外值得注意的是,我的 Robolectric 测试有一个单独的模块,所以我将新的依赖项添加到我的 Robolectric 模块中的 build.gradle 文件,而不是我的 Android 应用程序模块。

刚拿到一个新的 Mac,所以我的旧 Mac 和新的 Mac 是并排的,而我配置的是新的。我使用 Homebrew 构建了我的新 Mac,并不知道 Gradle 2.3 刚刚发布...这就是为什么 Robolectric 测试在我的旧 Mac(使用 Gradle 2.2.1)但不在我的新 Mac(使用 Gradle 2.3)。