Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

unit testing - Robolectric: Resources$NotFoundException: String resource ID with Android Gradle Plugin 3

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...