robolectric / robolectric

Android Unit Testing Framework
http://robolectric.org
Other
5.89k stars 1.37k forks source link

ResourcesNotFound Exception when building from the Command Line vs Android Studio #1598

Closed pivotal-christopher-larsen closed 9 years ago

pivotal-christopher-larsen commented 9 years ago

We have encountered a problem when building a project through Command Line. The Android Studio IDE is able to fully compile and test with Robolectric and Gradle plug-ins.

Running from the Command Line with ./gradlew clean assembleDebug testDebug, the error appears to be unable to find any resources for the Robolectric tests: android.content.res.Resources$NotFoundException

When running the tests through Android Studio, there are no issues. We need the Command Line version to work in order to use it with Travis CI.

The dependencies we are using in the project:

com.android.tools.build:gradle:1.1.0 org.robolectric:robolectric:2.4 org.roboguice:roboguice:2.0 junit:junit:4.12 com.android.support:support-v4:21.0.3 org.robolectric.gradle:gradle-android-test-plugin:0.10.+ com.android.support:support-v4:21.0.3

compileSdkVersion 19 buildToolsVersion "19.1.0" minSdkVersion 16 targetSdkVersion 19 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

We've tried to Specifically set the Source Dirs as discussed in other issues, but that hasn't worked either.

Trying to understand how the IDE is different from the CL, it calls in to java instead of gradlew to build and test, and references directories within the AndroidStudio.app bundle itself.

Any help would be appreciated, Thanks!

erd commented 9 years ago

Version 1.1.0 of the android plugin requires version 1.0.1 of the robolectric-gradle-plugin (if you are using it). The plugin only works with the CLI. If you want to support Android Studio, you'll need to create a custom test runner that sets up the android resource path, asset path, manifest path, and package name.

nenick commented 9 years ago

@pivotal-christopher-larsen That is a known issue and tracked at https://code.google.com/p/android/issues/detail?id=158015 there is also a workaround example

pivotal-christopher-larsen commented 9 years ago

We've figured out a way to get this working on CLI as well as in Android Studio. Without a custom Test Runner actually, which is nice.

By setting the tests to point at the manifest, the project will execute tests from the CLI. As Nick mentioned these are executed with the module as the relative root directory. @Config(manifest = "src/main/AndroidManifest.xml")

This works fine for CLI, but tests won't run in the Android Studio IDE. To fix this we could just change the config path: @Config(manifest = "application/src/main/AndroidManifest.xml")

With this set the project will build and test in the IDE but no longer works on CLI.

To get both working at the same time, we used the first arrangement that supports the CLI. Then in Edit Configuration -> JUnit Test -> Your "AllTests" Entry, (Create one if you don't have one set up that runs all your tests) modify the Working Directory path. (/Users/me/workspace/myProject/) and add your module path component. (/Users/me/workspace/myProject/myModule/)

So long as your Build Variants are set to "Unit Test" and the Experimental Gradle option is enabled, you can now run from the IDE as well as CLI without changing config paths between runs.

You will note trying to run any individual class tests will now fail. This again you can correct by setting the default Working Directory for new tests. Again go to Run/Debug Configurations, select Default this time, JUnit, and update this default Working Directory by appending your module path as above.

Thanks to David Nowak (@pivotal-david-nowak) and Sukhil Suresh (@pivotal-sukhil-suresh) in helping root cause this issue!