gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:03:45 25 4
gpt4 key购买 nike

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

我正在使用的测试类无法运行:

@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()));
}
}

这是错误的堆栈跟踪:

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)

Edit Configurations 我已将 Working Directory 设置为 $MODULE_DIR$

非常感谢您的任何建议。

最佳答案

As mentioned由 Google 团队的一名工程师(很可能是 Xavier Ducrohet)表示,Robolectric 存在 AAPT2 问题:

Robolectric is not compatible with aapt2.

这里有两个选项。

第一个选项 - 关注 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 {
}

第二个选项:禁用 AAPT2 将以下行添加到 gradle.properties 文件中:

android.enableAapt2=false

关于android - Robolectric:Resources$NotFoundException:带有 Android Gradle 插件 3 的字符串资源 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45922916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com