将以下内容添加到您的 build.gradle:android {测试选项{单元测试{包括AndroidResources = true}}}使用 Robolectric 测试运行器注释您的测试:@RunWith(RobolectricTestRunner.class)公共类三明治测试{}第二个选项:禁用 AAPT2 将以下行添加到 gradle.properties 文件中:android.enableAapt2=falseAndroid Studio 3.0 Beta2classpath '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. 解决方案 As mentioned by an engineer from Google team (most possibly Xavier Ducrohet), Robolectric has issues with AAPT2:Two options here.First option - follow Robolectric guidelines for Android Studio 3.0+android { testOptions { unitTests { includeAndroidResources = true } }}@RunWith(RobolectricTestRunner.class)public class SandwichTest {}Second option: disable AAPT2 adding following line into gradle.properties file:android.enableAapt2=false 这篇关于Robolectric:Resources$NotFoundException:带有 Android Gradle 插件 3 的字符串资源 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 01:27