简体   繁体   English

尝试访问raw文件夹中的资源时,Robolectric抛出Resources $ NotFoundException

[英]Robolectric throws Resources$NotFoundException when trying to access resource in raw folder

When I try to access a raw resource I receive an android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000 error. 当我尝试访问原始资源时,我收到android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000错误。

The resource is a .bks certificate, used for server validation. 该资源是.bks证书,用于服务器验证。 It is stored on the res/raw folder. 它存储在res/raw文件夹中。

The most awkward thing is that when I copied the certificate (and the validation architecture) to a fresh project (that I built just to test this), it worked! 最尴尬的是,当我将证书(和验证架构)复制到一个新项目(我为了测试它而构建)时,它起作用了!

I'm using nenick's custom runner , so Robolectric can correctly find the folders for manifest, res and assets. 我正在使用nenick的自定义运行器 ,因此Robolectric可以正确找到清单,资源和资产的文件夹。 When I go to the intermediates/res/debug folder all the resources are there. 当我转到intermediates / res / debug文件夹时,所有资源都在那里。 I don't have flavours in the project (The project used to have, I removed them. Maybe this is the problem?) 我没有项目中的风味(项目曾经有过,我删除了它们。也许这就是问题?)

I'm using com.android.tools.build:gradle:1.2.2 . 我正在使用com.android.tools.build:gradle:1.2.2 buildToolsVersion '22.0.1' . buildToolsVersion '22.0.1' I have a lot of dependencies on the project :( 我对该项目有很多依赖:(

Also, I'm using testCompile 'org.robolectric:robolectric:3.0-rc2' and testCompile 'org.robolectric:shadows-support-v4:3.0-rc2' The stacktrace: 另外,我正在使用testCompile 'org.robolectric:robolectric:3.0-rc2'testCompile 'org.robolectric:shadows-support-v4:3.0-rc2'

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000
    at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:346)
    at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:385)
    at android.content.res.Resources.openRawResource(Resources.java)
    at com.xxx.xxx.xxx.ssl.CertificateHelper.loadKeyStoreFromRaw(CertificateHelper.java:145)
    at com.xxx.xxx.xxx.xxx.XXX.createSSLSocketFactory(XXX.java:432)
    at com.xxx.xxx.xxx.xxx.xxx.XXX.<init>(XXX.java:92)
    at com.xxx.xxx.xxx.xxx.xxx.XXX.initiate(XXX.java:73)
    at com.xxx.xxx.xxx.MainApplication.onCreate(MainApplication.java:60)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:131)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:431)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:224)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

java.lang.RuntimeException: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:228)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000
    at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:346)
    at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:385)
    at android.content.res.Resources.openRawResource(Resources.java)
    at com.xxx.xxx.xxx.xxx.CertificateHelper.loadKeyStoreFromRaw(CertificateHelper.java:145)
    at com.xxx.xxx.xxx.xxx.XXX.createSSLSocketFactory(XXX.java:432)
    at xxx.xxx.xxx.xxx.xxx.xxx.XXX.<init>(TrafficManagerImpl.java:92)
    at xxx.xxx.xxx.xxx.xxx.xxx.XXX.initiate(XXX.java:73)
    at com.xxx.xxx.xxx.MainApplication.onCreate(MainApplication.java:60)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:131)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:431)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:224)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1 more

Sorry for the "xxx" on the package/class names, but I don't own the code. 对于包/类名称上的“xxx”感到抱歉,但我没有代码。

Edit 1 to add the code 编辑1以添加代码

The code that is getting executed at the onCreate of the application is: 在应用程序的onCreate上执行的代码是:

InputStream is = resources.openRawResource(keystoreId);

The variable keystoreId is a R.raw.keystore . 变量keystoreIdR.raw.keystore

Does anyone knows what could be causing this? 有谁知道是什么原因引起的?

Edit 2 to provid more info 编辑2以提供更多信息

An extra info: I have a different application ID on the build.gradle of the module and on the AndroidManifest. 额外信息:我在模块的build.gradle和AndroidManifest上有不同的应用程序ID。 Even when I changed the project to make them be the same, things didn't work and the bug still existed :( 即使我改变项目使它们相同,事情也不起作用,而且bug仍然存在:(

Edit 3: Android studio update 编辑3:Android工作室更新

With the last android studio update, the test started to be able to find the resources! 随着最后的android studio更新,测试开始能够找到资源! Except for the ones in the raw folder :( I'm solving it by putting a big ugly if in my code, but as this is not a solution for my question, I'll not close it... 除了原始文件夹中的那些:(我正在解决它, if在我的代码中放一个大丑,但因为这不是我的问题的解决方案,我不会关闭它...

I hope Nenick will correct this answer if it is wrong. 如果错误,我希望Nenick会纠正这个答案。 But you don't need his custom runner with your setup. 但是你的设置不需要他的自定义跑步者。 You can use RobolectricGradleTestRunner instead. 您可以使用RobolectricGradleTestRunner

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM