50
Android Studio 3.0 Beta2
classpath 'com.android.tools.build:gradle:3.0.0-beta3'
testCompile 'org.robolectric:robolectric:3.4.2'

Test class that I am using that fails to run:

@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricTestRunner.class)
public class RecipeAdapterTest {
    private MainActivity activity;

    @Before
    public void setup() {

    activity = Robolectric.setupActivity(MainActivity.class);

    /* Also tried this same Error
     activity = Robolectric.buildActivity(MainActivity)
                .create()
                .resume()
                .get();
    */
    }

    @Test
    public void testActivityShouldNotBeNull() {
        assertThat(activity, is(notNullValue()));
    }
}

This is the stack trace of the error:

android.content.res.Resources$NotFoundException: String resource ID #0x7f0c0020

    at android.content.res.Resources.getText(Resources.java:274)
    at android.content.res.Resources.getString(Resources.java:360)
    at android.content.Context.getString(Context.java:376)
    at org.robolectric.shadows.ShadowActivity.getActivityTitle(ShadowActivity.java:100)
    at org.robolectric.shadows.ShadowActivity.callAttach(ShadowActivity.java:110)
    at org.robolectric.android.controller.ActivityController.attach(ActivityController.java:56)
    at org.robolectric.android.controller.ActivityController.of(ActivityController.java:25)
    at org.robolectric.Robolectric.buildActivity(Robolectric.java:98)
    at org.robolectric.Robolectric.buildActivity(Robolectric.java:94)
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:102)
    at me.androidbox.busbybaking.adapters.RecipeAdapterTest.setup(RecipeAdapterTest.java:63)

In the Edit Configurations I have set the Working Directory to $MODULE_DIR$

Many thanks for any suggestion.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • Do you see same error when you run it from command line and gradle? – Eugen Martynov Aug 29 '17 at 04:32
  • This works if I open the project in Android Studio 2.3.3 and use gradle version 2.3.3 and set jackOptions { enabled true } and compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } – ant2009 Aug 29 '17 at 17:16
  • `jack` is depricated. If you don't do your modifications in the `build.gradle` file can you run it successfully from the command line? – Eugen Martynov Aug 30 '17 at 10:35
  • I have reverted back to the AS 3.0. I have run the test on the command line `./gradlew test` and I get the same problem as before `NotFoundException` – ant2009 Aug 30 '17 at 15:16
  • Are you using macOS or Windows? – Prokash Sarkar Aug 30 '17 at 16:50
  • @sarkar I'm using Linux 26 fedora 26 – ant2009 Aug 30 '17 at 17:11
  • 1
    https://androidstudio.googleblog.com/2017/06/android-studio-30-canary-5-is-now.html – IntelliJ Amiya Sep 01 '17 at 10:42
  • I wasn't using Robolectric. It was occurring because I was getting context with `InstrumentationRegistry.getInstrumentation().context`. Once I replaced `.context` with `.targetContext` the exception was gone. – Sufian May 28 '20 at 06:54

8 Answers8

93

As mentioned by an engineer from Google team (most possibly Xavier Ducrohet), Robolectric has issues with AAPT2:

Robolectric is not compatible with aapt2.

Two options here.

First option - follow Robolectric guidelines for Android Studio 3.0+

Add the following to your build.gradle:

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

Annotate your test with the Robolectric test runner:

@RunWith(RobolectricTestRunner.class)
public class SandwichTest {
}

Second option: disable AAPT2 adding following line into gradle.properties file:

android.enableAapt2=false
azizbekian
  • 60,783
  • 13
  • 169
  • 249
12

The Robolectric documentation states that the following configuration should be used with Android Studio 3.x:

android {
  testOptions {
    unitTests.includeAndroidResources true
  }
}
sfera
  • 1,138
  • 1
  • 15
  • 21
4

(for anyone that might be looking for a solution to a similar problem)


Be sure to use

RuntimeEnvironment.application

and not:

RuntimeEnvironment.systemContext

when you're trying to resolve resources "manually".

That's one case in which Resources$NotFoundException might show up with Robolectric.

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
4

Not a direct answer to the question, but if you are testing something that needs a context to query resources against I have found the following to work quite well:

ApplicationProvider.getApplicationContext()

(or RuntimeEnvironment.application -- but this is deprecated in favor of the above)

NPike
  • 13,136
  • 12
  • 63
  • 80
2

If your build fails due to an AAPT2 resource processing issue or you want to use Roboelectric, you can disable AAPT2 by setting android.enableAapt2=false in your gradle.properties file and restarting the Gradle daemon by running ./gradlew --stop from the command line.

Official guideline Android Studio 3.0 Release

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

I was using espresso, and for that you needed to use app resources, not test resources.

So instead of

InstrumentationRegistry.getInstrumentation().context.resources.getString("key")

I used

activityRule.activity.getString("key")
tonisives
  • 1,962
  • 1
  • 18
  • 17
-2

In my case the following solved my issue: "Problem is related to android studio. Go to 'Run' -> 'Edit configurations...' and change 'Working directory' value to $MODULE_DIR$

Run your tests.

More info here under 'Building with Android Studio'."

reference: https://github.com/robolectric/robolectric/issues/2653

Murat
  • 3,084
  • 37
  • 55
-2

You can also try @Config(manifest = "<projectFolder>/src/main/AndroidManifest.xml") in the case that you can not simply include the resources as some projects tests will fail with that included.

Tony
  • 4,609
  • 2
  • 22
  • 32