Question

When I add a packageNameSuffix to my build.gradle debug build type (see https://plus.google.com/108967384991768947849/posts/XGXH3arvbrK), all of my Robolectric tests are failing due to the following:

Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f050000
   at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:354)
   at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:387)
   at android.content.res.Resources.openRawResource(Resources.java)
   at com.myapp.utilities.CSVUtility.parseRawResourceCSV(CSVUtility.java:38)

The problem seems to be that the raw folder src/main/res/main no longer being found. This folder contains a csv which is parsed at application start... which is where the tests go boom.

Architecture/data restructuring suggestions aside (I know CSV may not be the best way to get this data loaded at app start), does anyone know how I might remedy this problem?

Edit: I tried moving the file to the assets folder instead, and then my tests failed on a Context.getString() call. Resources look to be getting completely hosed when adding packageNameSuffixes.

Was it helpful?

Solution

Edit: tmurakami posted on the github issue - https://github.com/robolectric/robolectric/issues/1001#issuecomment-42740897

I've copied the full response:


Try using this gradle snippet. This works fine in my environment.

def hasLibraryVariants = android.hasProperty('libraryVariants')
def variants = hasLibraryVariants ? android.libraryVariants : android.applicationVariants
tasks.withType(Test).whenTaskAdded {
    it.systemProperty 'android.package', variants.iterator().next().processResources.packageForR
}

The original package name has been stored in the following fields of any variant:

variantData.variantConfiguration.originalPackageName
processResources.packageForR
generateBuildConfig.buildConfigPackageName

However these are internal only, so might become inaccessible in the future.

If you don't want to use these fields, try the following snippet:

tasks.withType(Test).whenTaskAdded {
    it.systemProperty 'android.package', android.defaultConfig.packageName
}

To use this, you need to add the main package name in the 'android.defaultConfig' section.

android {
    defaultConfig {
        packageName 'main.package.name'
    }
}

Looks like I need to add an android.package system property for the package name. See this issue conversation on Github - https://github.com/robolectric/robolectric/issues/1001

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top