简体   繁体   English

Robolectric:Resources$NotFoundException:带有 Android Gradle 插件 3 的字符串资源 ID

[英]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$Edit Configurations我将Working Directory设置为$MODULE_DIR$

Many thanks for any suggestion.非常感谢您的任何建议。

As mentioned by an engineer from Google team (most possibly Xavier Ducrohet ), Robolectric has issues with AAPT2:正如Google 团队的一位工程师(最有可能是Xavier Ducrohet所提到的,Robolectric 存在 AAPT2 问题:

Robolectric is not compatible with aapt2. Robolectric 与 aapt2 不兼容。

Two options here.这里有两个选择。

First option - follow Robolectric guidelines for Android Studio 3.0+第一个选项 - 遵循适用于 Android Studio 3.0+ 的Robolectric指南

Add the following to your build.gradle:将以下内容添加到您的 build.gradle 中:

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

Annotate your test with the Robolectric test runner:使用 Robolectric 测试运行器注释您的测试:

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

Second option: disable AAPT2 adding following line into gradle.properties file:第二个选项:禁用 AAPT2 将以下行添加到gradle.properties文件中:

android.enableAapt2=false

The Robolectric documentation states that the following configuration should be used with Android Studio 3.x: Robolectric 文档指出以下配置应与 Android Studio 3.x 一起使用:

android {
  testOptions {
    unitTests.includeAndroidResources true
  }
}

(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 .这是Resources$NotFoundException可能与Robolectric一起Robolectric的一种情况。

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.如果构建一个失败,因为AAPT2资源处理问题,或者你想使用Roboelectric ,您可以禁用AAPT2通过设置android.enableAapt2=falsegradle.properties文件,并通过运行重新启动摇篮守护./gradlew --stop从命令行。

Official guideline Android Studio 3.0 Release官方指南Android Studio 3.0 发布

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) (或 RuntimeEnvironment.application - 但不赞成使用上述内容)

I was using espresso, and for that you needed to use app resources, not test resources.我使用的是 espresso,为此您需要使用应用程序资源,而不是测试资源。

So instead of所以代替

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

I used我用过

activityRule.activity.getString("key")

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$在我的情况下,以下解决了我的问题:“问题与 android studio 有关。转到“运行”->“编辑配置...”并将“工作目录”值更改为 $MODULE_DIR$

Run your tests.运行您的测试。

More info here under 'Building with Android Studio'." “使用 Android Studio 构建”下的更多信息。”

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

您也可以尝试@Config(manifest = "<projectFolder>/src/main/AndroidManifest.xml")在您不能简单地包含资源的情况下,因为某些项目测试将因包含该资源而失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试中的Gradle Robolectric资源NotFoundException - Gradle Robolectric Resources NotFoundException in Testing Android:android.content.res.Resources $ NotFoundException:字符串资源ID - Android: android.content.res.Resources$NotFoundException: String resource ID android.content.res.Resources$NotFoundException · 字符串资源 ID # - android.content.res.Resources$NotFoundException · String resource ID # android.content.res.Resources$NotFoundException:字符串资源 ID - android.content.res.Resources$NotFoundException: String resource ID Resources $ NotFoundException带Robolectric的字符串数组 - Resources$NotFoundException for String array with Robolectric Android Resources $ NotFoundException:资源ID#0x0 - Android Resources$NotFoundException: Resource ID #0x0 .Resources$NotFoundException: 字符串资源 ID #0x3 - .Resources$NotFoundException: String resource ID #0x3 使用带有Gradle的Robolectric时的资源$ NotFoundException - Resources$NotFoundException when using Robolectric with Gradle 缺少资源 ID android.content.res.Resources$NotFoundException:字符串资源 ID #0xb - missing resource id android.content.res.Resources$NotFoundException: String resource ID #0xb android.content.res.resources notfoundexception $ android.content.res.resources notfoundexception $字符串资源ID#0x1 - android.content.res.resources notfoundexception $android.content.res.resources notfoundexception $ string resource id #0x1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM