我在 deckard-gradle 模板中配置了 robolectric,一切正常,除了在通过终端启动 robolectric 测试时收到此错误消息:
java.lang.RuntimeException: android.content.res.Resources$NotFoundException: 未知资源 2131296262
我知道这与我在 build.gradle 中使用的不同风格有关。我已经在使用 Robolectric 的自定义 TestRunner,但无论如何它都不起作用,我尝试了 2.3 + 2.4 SNAPSHOT。
我的 TestRunner 看起来像这样:
@Override
protected AndroidManifest getAppManifest(Config config) {
String manifestProperty = System.getProperty("android.manifest");
if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
String resProperty = System.getProperty("android.resources");
String assetsProperty = System.getProperty("android.assets");
return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
Fs.fileFromPath(assetsProperty));
}
AndroidManifest appManifest = super.getAppManifest(config);
appManifest.setPackageName("com.example.android");
return appManifest;
}
我做错了什么?我的口味是这样声明的:
flavors {
flavor1 {
packageName = "com.example.android"
buildConfigField "String", "GCM_SENDER_ID", "\"807347333395\""
buildConfigField "java.util.Locale", "locale", "java.util.Locale.GERMANY"
buildName = "./gittag.sh ${name}".execute([], project.rootDir).text.trim()
buildConfigField "String", "BUILD_NAME", "\"${buildName}\""
versionName = "2.0.4"
versionCode = 21
}
}
如果有人可以向我提供更多相关信息,那就太棒了!非常感谢!
最佳答案
我刚刚解决了与 Robolectric(版本:robolectric-2.4-jar-with-dependencies.jar)相同的问题并返回未知资源,同时从不同的值-* 文件夹加载资源例如:
<resources>
<string name="screen_type">10-inch-tablet</string>
</resources>
就我而言,我想识别不同的设备。为此,我在以下文件夹中创建了 screen.xml:
values/screen.xml - mobile
values-sw600dp/screen.xml - 7-inch tablet
values-sw720dp/screen.xml - 10-inch tablet
我的工作区如下:
workspace
\-ExProj
\-exProj
\-src
\-exProj
\-AndroidManifest.xml
\-ExProjTest
我使用 Robolectric 进行的单元测试:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "./../ExProj/exProj/src/exProj/AndroidManifest.xml")
public class UtilityTest {
@Test
@Config(qualifiers = "sw720dp")
public void testIfDeviceIsTablet() throws Exception {
System.out.println(Robolectric.application.getString(R.string.screen_type));
}
}
上面的测试代码在控制台打印出来:10-inch-tablet...
关于android - 带有 Robolectric 和 Flavors 的 Resources$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24010561/