[英]Resources$NotFoundException when using Robolectric with Gradle
[英]Robolectric throws Resources$NotFoundException when trying to access resource in raw folder
當我嘗試訪問原始資源時,我收到android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000
錯誤。
該資源是.bks證書,用於服務器驗證。 它存儲在res/raw
文件夾中。
最尷尬的是,當我將證書(和驗證架構)復制到一個新項目(我為了測試它而構建)時,它起作用了!
我正在使用nenick的自定義運行器 ,因此Robolectric可以正確找到清單,資源和資產的文件夾。 當我轉到intermediates / res / debug文件夾時,所有資源都在那里。 我沒有項目中的風味(項目曾經有過,我刪除了它們。也許這就是問題?)
我正在使用com.android.tools.build:gradle:1.2.2
。 buildToolsVersion '22.0.1'
。 我對該項目有很多依賴:(
另外,我正在使用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
對於包/類名稱上的“xxx”感到抱歉,但我沒有代碼。
編輯1以添加代碼
在應用程序的onCreate
上執行的代碼是:
InputStream is = resources.openRawResource(keystoreId);
變量keystoreId
是R.raw.keystore
。
有誰知道是什么原因引起的?
編輯2以提供更多信息
額外信息:我在模塊的build.gradle和AndroidManifest上有不同的應用程序ID。 即使我改變項目使它們相同,事情也不起作用,而且bug仍然存在:(
編輯3:Android工作室更新
隨着最后的android studio更新,測試開始能夠找到資源! 除了原始文件夾中的那些:(我正在解決它, if
在我的代碼中放一個大丑,但因為這不是我的問題的解決方案,我不會關閉它...
如果錯誤,我希望Nenick會糾正這個答案。 但是你的設置不需要他的自定義跑步者。 您可以使用RobolectricGradleTestRunner
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.