android.content.res.Resources$NotFoundException in Robolectric 2.4
Asked Answered
F

1

6

I have trouble loading resources when running unit tests from command line. It works fine in IntelliJ.

I'm using:

  • com.android.tools.build:gradle:1.1.3
  • org.robolectric:robolectric-gradle-plugin:1.0.1
  • org.robolectric:robolectric:2.4

I have the following project structure (Multi-module):

+---module1
|   |   build.gradle
    |   \---src
    |       +---main
    |       |   |   AndroidManifest.xml
    |       |   |   
    |       |   +---res
    |       |   |   \---values
    |       |   |           strings.xml
    |       \---test
    |           \---java
    |               \---example
    |                    |   FooTest.java
+---module2
etc

My test (simplified) looks like this:

@Config(emulateSdk = 18, reportSdk = 18, manifest = "./src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class FooTest {

    @Test
    public void test() {
        String result = Robolectric.application.getString(R.string.error_message);

         assertThat(result, notNullValue());
    }
 }

I get this stacktrace:

android.content.res.Resources$NotFoundException: unknown resource 2131361826
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:311)
at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:69)
at android.content.res.AssetManager.getResourceText(AssetManager.java)
at android.content.res.Resources.getText(Resources.java:235)
at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:363)
at android.content.res.Resources.getText(Resources.java)
at android.content.res.Resources.getString(Resources.java:325)
at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:41)
at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:96)
at android.content.Context.getString(Context.java)

Note: I'm quite new to Gradle so it is likely that there is a misconfiguration somewhere. I have assumed that by following the project structure convention that I don't need to specify where to find resources. I tried specifying them but without any luck:

android {
    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
    }
}
Facient answered 9/4, 2015 at 8:2 Comment(8)
Can you try to change your config manifest to module1/src/main/AndroidManifest.xml. It is not fix for your issue (now you will probably have failing tests in AS) but just quick check the problemLimon
Hi, I tried and got this: > File '/opt/buildagent/work/22b7e45eb23ab445/module1/module1/src/main/AndroidManifest.xml' specified for property 'manifest' does not exist.Facient
Wrong guess. Can you remove mentioning of the manifest file in config at all?Limon
Another question what version of android gradle plugin do you have?Limon
@EugenMartynov Same error when removing the line ` manifest.srcFile 'src/main/AndroidManifest.xml'` I've updated the post with the gradle android plugin version!Facient
Clear! Last question - does your debug build changes applicationId? like adding ".debug" or something?Limon
With android gradle 1.1.x you can remove usage of gradle robolectric pluginLimon
Removing the robolectric gradle plugin solved my problem. Please post an answer so that I can mark it :)Facient
L
1

During investigation we found the cause of problem. If you use android gradle plugin v1.1.x then you don't need in common robolectric gradle plugin.

Be carefull that incorrect version of robolectric gradle plugin will break your tests.

Limon answered 11/4, 2015 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.