Robolectric says "AndroidManifest.xml not found"
Solution 1
I'm assuming you're trying to run the tests with JUnit. You can try two different things:
- Create a Custom TestRunner class, as shown here. Check the CustomTestRunner section, where you basically create a TestRunner that actually knows the right manifest to use. Specify your tests for them to run with your test runner, with the
@Config
annotation. - (My preferred choice) Go the your JUnit configuration, Run > Edit Configurations. Notice the 'Working Directory' textbox. Append
/app
(for OSX and Linux) or\app
(Windows), to the path written in the textbox. Try running again and it should work.
Solution 2
Same problem on Android Studio. I've solved this problem to edit the configuration of Unit4. you can follow these things.
On Android Studio.
- Edit Configurations
- In Junit, you have to change the working directory to $MODULE_DIR$.
The important thing is $MODULE_DIR$.
you can reference the following screenshot. thanks.
Solution 3
In case you are still getting this error with Android Studio 3.0, please be sure that your gradle configuration has these parameters:
android {
...
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
Solution 4
I have faced same errors, We used several flavors and buildtypes So, there are steps to make it working:
- Android studio tests run configuration
You have to set working directory to $MODULE_DIR$ in Windows too. http://robolectric.org/getting-started/ should say that.
-
Unit test should be annotated like this:
@RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject") public class SomeFragmentTest {
Solution 5
Note that intellij 16 EAP has a bug around this $MODULE_DIR$ variable (it's pointing to the wrong place), causing the tests to fail with this exception. See https://youtrack.jetbrains.com/issue/IDEA-149802#tab=History. Should get fixed mid Jan 2016.
Related videos on Youtube
Christine
I am a Java software developer, serial entrepreneur, inventor, consultant, writer. I have published a book, working on the second, and I write columns and articles, both on software related subjects and other. I play contract bridge, I love snowboarding, I ride a fast bike (GSX-R1000) to work. I have programmed in Java since 1995, between 2008 and 2015 I built Android apps for various clients.
Updated on July 09, 2022Comments
-
Christine almost 2 years
While trying to get Robolectric RC3 to work in Android Studio, I get
Caused by: java.lang.RuntimeException: build/intermediates/bundles/debug/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml at org.robolectric.manifest.AndroidManifest.validate(AndroidManifest.java:120) at org.robolectric.manifest.AndroidManifest.getResourcePath(AndroidManifest.java:469) at org.robolectric.manifest.AndroidManifest.getIncludedResourcePaths(AndroidManifest.java:475) at org.robolectric.RobolectricTestRunner.createAppResourceLoader(RobolectricTestRunner.java:479) at org.robolectric.RobolectricTestRunner.getAppResourceLoader(RobolectricTestRunner.java:471) at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:73) at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:421) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
I tried
@Config(manifest = "app/src/main/AndroidManifest.xml", constants = BuildConfig.class, sdk=21)
and I tried setting the manifest location in my TestRunner, nothing worked. In the file system I see that the manifest is in
./app/build/intermediates/manifests/full/debug/AndroidManifest.xml
not in the location Robolectric is looking for it. At one point the manifest just got ignored, then a similar issue occurred for resources, the app or Robolectric couldn't find a raw resource my app uses. This is in my build file:
sourceSets { main { java.srcDirs = ['src/main/java'] resources.srcDirs = ['src/main/res'] } test { java.srcDirs = ['src/test/java', 'src/main/java'] resources.srcDirs = ['src/test/res', 'src/main/res'] } }
How do I tell Robolectric where to look for a manifest, and more importantly, where to look for resources?
Edit: I have checked out Robolectric from github, I've built it, installed it in my local .m2 repo, the gradle file now refers to the local SNAPSHOT build, and I made sure Gradle doesn't get a new version from a remote repo. Then I copied the RobolectricGradleTestRunner to my project, I have changed the lines where the file locations are defined: it didn't contain the module name. Now it works.
-
Christine almost 9 yearsI did. Didn't work. Also, in Robolectric RC3 it's "sdk=18", not "emulateSdk=18".
-
Christine almost 9 yearsI don't put the xml file in the build directory. The build process does that.
-
Jorge E. Hernández almost 9 yearsThis is the new version of the post suggested bignerdranch.com/blog/…
-
Christine almost 9 yearsYes, adding the "app" string turned out to be essential.
-
Leo supports Monica Cellio over 8 yearsThank you big time Chaparro!
-
Ari over 8 yearsThanks for saving me so much time!
-
Matt Accola over 8 yearsThis is the correct answer. It is an issue that affects Mac users. This is actually documented on the Robolectric Getting Started page, robolectric.org/getting-started.
-
Matt Accola over 8 yearsA better solution is to just set the Working directory to $MODULE_DIR$ as recommended on the Robolectric Getting Started page, robolectric.org/getting-started
-
carlrice over 8 yearsMake sure you are importing the correct BuildConfig - I wasn't paying attention and pulled BuildConfig from a library which causes a similar issue to the OP.
-
pepan over 8 yearsI'm new in Robolectric; just setup the project and probably did something wrong. Still, this answer resolved my issue on Windows.
-
Oliver Dixon about 7 yearsWhat to do if you're using CI?
-
Vladyslav Ulianytskyi almost 7 yearsBut why AS need it to every new test ? Is it any way to Indicate "Working directory" for all tests?
-
Nunes D. over 6 yearsMany thanks! That saved my day! I was really struggling with new gradle plugin 3.0.0, gradle 4.1 and Android studio 3. My Robolectric tests went nuts! Now everything is back to normal.
-
HBB20 over 4 yearsI don't know why based on other comments it should work but I can not get it working on AS 3.5. Anyone else facing the same?
-
rickchristie about 3 yearsThank you. This saved me possibly hours of debugging.