[英]Resources$NotFoundException with Robolectric and Flavors
I configured robolectric as seen in the deckard-gradle template, everything is fine except the fact that I get this error message when starting the robolectric tests via terminal: 我配置了robolectric,如在deckard-gradle模板中所示,一切正常,除了通过终端启动robolectric测试时收到以下错误消息:
java.lang.RuntimeException: android.content.res.Resources$NotFoundException: unknown resource 2131296262 java.lang.RuntimeException:android.content.res.Resources $ NotFoundException:未知资源2131296262
I know that it's related to the different flavors I use in the build.gradle. 我知道这与我在build.gradle中使用的不同口味有关。 I am already using a custom TestRunner for Robolectric, but it is not working anyway, I tried 2.3 + 2.4 SNAPSHOT. 我已经在Robolectric上使用了自定义TestRunner,但是无论如何它都无法正常工作,我尝试使用2.3 + 2.4 SNAPSHOT。
My TestRunner looks like this: 我的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;
}
What am I doing wrong? 我究竟做错了什么? My flavors are declared like that: 我的口味声明如下:
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
}
}
It would be awesome if somebody could provide me some more informations on that! 如果有人能为我提供更多信息,那将是非常棒的! Thanks a lot! 非常感谢!
I've just resolved the same problem with Robolectric (ver: robolectric-2.4-jar-with-dependencies.jar) and returning unknown resources, while loading resouce from different values-* folders eg: 我刚刚用Robolectric解决了同样的问题(版本:robolectric-2.4-jar-with-dependencies.jar)并返回未知资源,同时从不同的values- *文件夹加载资源,例如:
<resources>
<string name="screen_type">10-inch-tablet</string>
</resources>
In my case I wanted to recognize different devices. 就我而言,我想识别不同的设备。 For this reason, I've created screen.xml in following folders: 因此,我在以下文件夹中创建了screen.xml:
values/screen.xml - mobile
values-sw600dp/screen.xml - 7-inch tablet
values-sw720dp/screen.xml - 10-inch tablet
My workspace is as following: 我的工作区如下:
workspace
\-ExProj
\-exProj
\-src
\-exProj
\-AndroidManifest.xml
\-ExProjTest
My unit test with Robolectric: 我的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));
}
}
Above test code prints out on the console: 10-inch-tablet... 上面的测试代码在控制台上打印出来:10英寸平板电脑...
Try setting the path to the manifest in your test as follows: 尝试如下设置测试清单中的路径:
@Config(manifest = "/src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class DeckardActivityRobolectricTest {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.