简体   繁体   English

重命名类后,Gradle测试运行失败

[英]Gradle test run fails after class rename

We have Gradle set up to build and test our Android app. 我们已设置Gradle来构建和测试我们的Android应用。 Now we ran into the problem, that Gradle test runs fail reproducibly after a class was renamed somewhere in the project: 现在我们遇到了一个问题,即在项目中重命名一个类后, Gradle测试运行可再现地失败

Execution failed for task ':PROJ:compileDebugUnitTestJavaWithJavac'.
> Unable to read class file: '/path/to/class/with/the/name/before/renaming.class'

This error is reproducible both.. 两者均可重现此错误。

  • locally (run gradle tests from Android Studio, or on command line using ./gradlew test ), and 在本地 (从Android Studio运行gradle测试,或使用./gradlew test在命令行上./gradlew test ),以及
  • remotely , when the tests run on our CI (a Teamcity server). 远程 ,当测试在我们的CI(Teamcity服务器)上运行时。

What solves the issue, is manually triggering a rebuild locally (eg in Android Studio Build > Rebuild Project ) or re-running the Teamcity task with the flag clean all files in the checkout directory before the build set. 解决该问题的方法是手动触发本地重建(例如,在Android Studio Build> Rebuild Project中 ),或者clean all files in the checkout directory before the buildclean all files in the checkout directory before the build使用带有标志clean all files in the checkout directory before the build的标志重新运行Teamcity任务。

Is there any way we can get our tests to not fail after a class has been renamed? 重命名类后,有什么方法可以使测试不失败?

While the above solution is simple enough it is kinda annoying that a simple rename which makes it to our repo will blow up the CI builds... We really want those to stay green. 尽管上述解决方案非常简单,但令人讨厌的是,将其更改为我们的存储库的简单重命名将破坏CI的构建……我们真的希望那些保持绿色。 :) :)

You could use the Swabra build feature of TC that cleans out the working directory either before or after each build, so that all generated files are blown away and the build always starts with a clean working copy. 您可以使用TC的Swabra构建功能,该功能可以在每次构建之前或之后清除工作目录,以便删除所有生成的文件,并且构建始终以干净的工作副本开始。 For reproducible builds this is a must-be-used feature in my opinion. 在我看来,对于可复制的版本,这是必须使用的功能。

Alternatively you can of course also add the clean Gradle task to your TC configuration so that Gradle cleans away previous build artefacts. 另外,您当然也可以将clean Gradle任务添加到TC配置中,以便Gradle清除以前的构建工件。 But if using TC I'd prefer Swabra as it restores VCS state reliably. 但是,如果使用TC,我宁愿使用Swabra,因为它可以可靠地恢复VCS状态。

This is a known issue in Gradle 2.14 and 2.14.1 and there will be a workaround for it in upcoming Android Gradle plugin release. 这是Gradle 2.14和2.14.1中的一个已知问题 ,在即将发布的Android Gradle插件版本中将有解决方法。

As a workaround you can do a clean build for your project. 作为解决方法,您可以为您的项目进行干净的构建。 For a CI build, a clean build is a good idea anyway. 对于CI构建,无论如何,干净构建都是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM