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']
        }
    }
}
Alix
  • 2,630
  • 30
  • 72
  • 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 problem – Eugen Martynov Apr 09 '15 at 13:19
  • Hi, I tried and got this: > File '/opt/buildagent/work/22b7e45eb23ab445/module1/module1/src/main/AndroidManifest.xml' specified for property 'manifest' does not exist. – Alix Apr 10 '15 at 07:17
  • Wrong guess. Can you remove mentioning of the manifest file in config at all? – Eugen Martynov Apr 10 '15 at 07:54
  • Another question what version of android gradle plugin do you have? – Eugen Martynov Apr 10 '15 at 07:55
  • @EugenMartynov Same error when removing the line ` manifest.srcFile 'src/main/AndroidManifest.xml'` I've updated the post with the gradle android plugin version! – Alix Apr 10 '15 at 08:46
  • Clear! Last question - does your debug build changes applicationId? like adding ".debug" or something? – Eugen Martynov Apr 10 '15 at 09:07
  • 1
    With android gradle 1.1.x you can remove usage of gradle robolectric plugin – Eugen Martynov Apr 10 '15 at 09:08
  • Removing the robolectric gradle plugin solved my problem. Please post an answer so that I can mark it :) – Alix Apr 10 '15 at 14:49

1 Answers1

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.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114