Resources$NotFoundException with Robolectric and Flavors

Refresh

April 2019

Views

1.1k time

1

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:

@Override
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),
                Fs.fileFromPath(assetsProperty));
    }
    AndroidManifest appManifest = super.getAppManifest(config);
    appManifest.setPackageName("com.example.android");
    return appManifest;
}

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

 flavors {
            flavor1 {
                packageName = "com.example.android"
                buildConfigField "String", "GCM_SENDER_ID", "\"807347333395\""
                buildConfigField "java.util.Locale", "locale", "java.util.Locale.GERMANY"
                buildName = "./gittag.sh ${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

0

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

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

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.:

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

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:

workspace
   \-ExProj
       \-exProj
            \-src
                \-exProj
                    \-AndroidManifest.xml
   \-ExProjTest

My unit test with Robolectric:

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

  @Test
   @Config(qualifiers = "sw720dp")
    public void testIfDeviceIsTablet() throws Exception {
     System.out.println(Robolectric.application.getString(R.string.screen_type));
    }
}

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