[英]Resources$NotFoundException when using Robolectric with Gradle
I've been following this tutorial on setting up Robolectric to test my Android Gradle project. 我一直在关注这个教程上设置Robolectric来测试我的Android摇篮工程。
I keep hitting this error: 我一直在遇到这个错误:
android.content.res.Resources$NotFoundException: no such label com.mypackage.dev:string/app_name
By downloading the sample project from the tutorial I've established that the problem is my productFlavors
(dev, staging, production). 通过从教程中下载示例项目,我已经确定问题是我的
productFlavors
(dev,staging,production)。 Adding flavors to the working sample project causes the same issue. 向工作示例项目添加flavor会导致同样的问题。 You can see an outline of my build.gradle in this answer .
你可以在这个答案中看到我的build.gradle的大纲。
I've seen various answers (eg here ) which suggest I need to specify the sourceSets
for each flavor. 我已经看到了各种答案(例如这里 ),这表明我需要为每种风格指定
sourceSets
。 I've tried lots of combinations but can't quite get it correct. 我已经尝试了很多组合,但不能完全正确。 Can anybody help?
有人可以帮忙吗?
The other thing confusing me is that all the Robolectric samples I've seen seem to be specifying the sourceSets and dependencies for "instrumentTest", even though the Robolectric tests are always only in the "test" folder. 令我困惑的另一件事是我见过的所有Robolectric样本似乎都在为“instrumentTest”指定sourceSets和依赖项,即使Robolectric测试总是只在“test”文件夹中。 In my case I already have Robotium tests in the instrumentTest folder, and I don't see why I would need to add Robolectric dependencies for the Robotium code.
在我的情况下,我已经在instrumentTest文件夹中进行了Robotium测试,我不明白为什么我需要为Robotium代码添加Robolectric依赖项。
I stumbled upon the same problem (resources not found) and found a solution on the robolectric source. 我偶然发现了同样的问题(未找到资源),并在robolectric源上找到了解决方案。 There is a setter for PackageName, so in my custom test runner I set the package name before returning the manifest.
PackageName有一个setter,所以在我的自定义测试运行器中,我在返回清单之前设置了包名。 Something like:
就像是:
@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");
AndroidManifest manifest = new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
Fs.fileFromPath(assetsProperty));
manifest.setPackageName("com.mypackagename");
return manifest;
}
return super.getAppManifest(config);
}
I hope this helps. 我希望这有帮助。
Robolectric fixed this issue in version 3.0. Robolectric在3.0版本中解决了这个问题。 So just use latest version of Robolectric with defined constans param in
@Config
annotation. 因此,只需在
@Config
注释中使用最新版本的Robolectric和已定义的constans参数。
@Config(
sdk = 21,
manifest = "src/main/AndroidManifest.xml",
constants = BuildConfig.class
)
@RunWith(RobolectricTestRunner.class)
Also, one important item - for correct Robolectric work your applicationId
in gradle.build
should be equals to package
in AndroidManifest
. 此外,一个重要的项目-正确Robolectric工作,你
applicationId
在gradle.build
应该等于package
在AndroidManifest
。 Usage of applicationIdSuffix
doesn't affect Robolectric. applicationIdSuffix
使用不会影响Robolectric。
You can fix this by specifying 'packageName' in your @Config so that it becomes: 您可以通过在@Config中指定'packageName'来解决此问题,使其成为:
@Config(
sdk = 21,
manifest="src/main/AndroidManifest.xml",
constants = BuildConfig.class,
packageName = "com.mypackage" //this is whatever value is your AndroidManifest.xml at <manifest ... package="com.mypackage".../>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.