jademcosta
jademcosta

Reputation: 1528

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.

The resource is a .bks certificate, used for server validation. It is stored on the res/raw folder.

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. When I go to the intermediates/res/debug folder all the resources are there. 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. 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:

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.

Edit 1 to add the code

The code that is getting executed at the onCreate of the application is:

InputStream is = resources.openRawResource(keystoreId);

The variable keystoreId is a R.raw.keystore.

Does anyone knows what could be causing this?

Edit 2 to provid more info

An extra info: I have a different application ID on the build.gradle of the module and on the AndroidManifest. Even when I changed the project to make them be the same, things didn't work and the bug still existed :(

Edit 3: Android studio update

With the last android studio update, the test started to be able to find the resources! 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...

Upvotes: 9

Views: 4296

Answers (1)

Eugen Martynov
Eugen Martynov

Reputation: 20130

I hope Nenick will correct this answer if it is wrong. But you don't need his custom runner with your setup. You can use RobolectricGradleTestRunner instead.

Upvotes: 2

Related Questions