This article was translated from Japanese by Claude Code.
Versions used:
Android Studio: 3.0 stable version
Robolectric: 3.5.1
The issue is as the title states: when running unit tests with the above versions of Android and Robolectric, a ResourceNotFoundException occurs when accessing Android Resources, causing tests to fail. (In my case, execution locally worked fine but failed only when running on CI (Bitrise).)
While this solution may only be valid for my specific case, here’s the solution:
Solution for ResourceNotFoundException#
Add testOptions within the android {} block in app/build.gradle and describe the configuration for unit test execution.
testOptions {
unitTests {
includeAndroidResources = true
}
}The documentation for includeAndroidResources is here:
UnitTestOptions - Android Plugin 3.0.0-dev DSL Reference
By setting this option to true, the plugin merges assets, resources, and manifests before executing unit tests.
By specifying this, I was able to prevent ResourceNotFoundException and run tests successfully.
Solution for ZoneRulesException (When Depending on ThreeTenBP During Test Execution)#
After setting the includeAndroidResources option, most tests ran successfully, but then I encountered an error log like the one below causing tests to terminate:
org.threeten.bp.zone.ZoneRulesException
at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:303)
at android.net.LocalServerSocket.__constructor__(LocalServerSocket.java:48)
at android.net.LocalServerSocket.<init>(LocalServerSocket.java)
at com.facebook.stetho.server.LocalSocketServer.bindToSocket(LocalSocketServer.java:142)
at com.facebook.stetho.server.LocalSocketServer.listenOnAddress(LocalSocketServer.java:78)
at com.facebook.stetho.server.LocalSocketServer.run(LocalSocketServer.java:74)
at com.facebook.stetho.server.ServerManager$1.run(ServerManager.java:40)The app uses ThreeTenABP, and I previously faced issues with tests failing with:
IllegalStateException: TZDB.dat missing from assetsReference: IllegalStateException: TZDB.dat missing from assets · Issue #24 · JakeWharton/ThreeTenABP · GitHub
Based on the solution mentioned in that issue, I had configured tests to depend on ThreeTenBP during execution. When the ZoneRulesException appeared, I tried removing this dependency as a test, and tests passed successfully. (oh..)
I suspect includeAndroidResources is the cause, but I haven’t investigated the specifics.
Below is a list of sites that helped during this investigation. It’s rough, but that’s all!