Resources$NotFoundException with Robolectric and Flavors


April 2019


1.1k time


I configured robolectric as seen in the deckard-gradle template, everything is fine except the fact that I get this error message when starting the robolectric tests via terminal:

java.lang.RuntimeException: android.content.res.Resources$NotFoundException: unknown resource 2131296262

I know that it's related to the different flavors I use in the build.gradle. I am already using a custom TestRunner for Robolectric, but it is not working anyway, I tried 2.3 + 2.4 SNAPSHOT.

My TestRunner looks like this:

protected AndroidManifest getAppManifest(Config config) {
    String manifestProperty = System.getProperty("android.manifest");
    if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
        String resProperty = System.getProperty("android.resources");
        String assetsProperty = System.getProperty("android.assets");
        return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
    AndroidManifest appManifest = super.getAppManifest(config);
    return appManifest;

What am I doing wrong? My flavors are declared like that:

 flavors {
            flavor1 {
                packageName = ""
                buildConfigField "String", "GCM_SENDER_ID", "\"807347333395\""
                buildConfigField "java.util.Locale", "locale", "java.util.Locale.GERMANY"
                buildName = "./ ${name}".execute([], project.rootDir).text.trim()
                buildConfigField "String", "BUILD_NAME", "\"${buildName}\""
                versionName = "2.0.4"
                versionCode = 21

It would be awesome if somebody could provide me some more informations on that! Thanks a lot!

2 answers


Try setting the path to the manifest in your test as follows:

@Config(manifest = "/src/main/AndroidManifest.xml")
public class DeckardActivityRobolectricTest {

I've just resolved the same problem with Robolectric (ver: robolectric-2.4-jar-with-dependencies.jar) and returning unknown resources, while loading resouce from different values-* folders e.g.:

    <string name="screen_type">10-inch-tablet</string>

In my case I wanted to recognize different devices. For this reason, I've created screen.xml in following folders:

values/screen.xml - mobile
values-sw600dp/screen.xml - 7-inch tablet
values-sw720dp/screen.xml - 10-inch tablet

My workspace is as following:


My unit test with Robolectric:

@Config(manifest = "./../ExProj/exProj/src/exProj/AndroidManifest.xml")
public class UtilityTest {

   @Config(qualifiers = "sw720dp")
    public void testIfDeviceIsTablet() throws Exception {

Above test code prints out on the console: 10-inch-tablet...