Tests fail when using different applicationId im build.gradle file with RobolectricGradleTestRunner

291 views
Skip to first unread message

Alexander Stark

unread,
Jul 17, 2015, 7:44:23 AM7/17/15
to robol...@googlegroups.com
Hi,


in our project we're using a different applicationId than package name.
In build.gradle file we have defined:

...
defaultConfig {
minSdkVersion 13
targetSdkVersion 21
applicationId = "com.xxx.xxxsuffix"
}
...


The package name is only "com.xxx.xxx"
For our tests we're using RobolectricGradleTestRunner:

@Config(constants = BuildConfig.class)
@RunWith(RobolectricGradleTestRunner.class)

When we run the test we get the following error:

android.content.res.Resources$NotFoundException: unknown resource 2131558435
    at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:351)
    at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:70)
    at android.content.res.AssetManager.getResourceText(AssetManager.java)
    at android.content.res.Resources.getText(Resources.java:270)
    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:358)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:360)
    at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:33)
    at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:81)
    at android.content.Context.getString(Context.java)
    at com.citrix.g2acommon.G2AApplication.getContentTitle(G2AApplication.java:218)
    at com.citrix.g2acommon.G2AApplication.onCreate(G2AApplication.java:191)
    at com.citrix.g2acustomer.G2ACustomerApplication.onCreate(G2ACustomerApplication.java:137)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    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: unknown resource 2131558435
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:244)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    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:62)
    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: unknown resource 2131558435
    at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:351)
    at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:70)
    at android.content.res.AssetManager.getResourceText(AssetManager.java)
    at android.content.res.Resources.getText(Resources.java:270)
    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:358)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:360)
    at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:33)
    at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:81)
    at android.content.Context.getString(Context.java)
    at com.citrix.g2acommon.G2AApplication.getContentTitle(G2AApplication.java:218)
    at com.citrix.g2acommon.G2AApplication.onCreate(G2AApplication.java:191)
    at com.citrix.g2acustomer.G2ACustomerApplication.onCreate(G2ACustomerApplication.java:137)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1 more

I found a workaround.
When I add the package name to my config annotation it works:
@Config(constants = BuildConfig.class, packageName = "com.citrix.g2acustomer")
I have to add this for each test suite.

Is this a known Issue?
Is there a better solution?

Thanks
Alex

Kanishk Tripathi

unread,
Jul 17, 2015, 10:18:30 AM7/17/15
to robol...@googlegroups.com
Robolectric by default picks up the package name based on your applicationId. It's specified in Config annotation. 
So if the package hierarchy in your source code is not the same as your applicationId, you get this error.
Your workaround is correct. This is not an issue.
But instead of adding this in every test suite, you can create a base class and annotate it with Config. Then extend each test case with that class.
If you don't want to do that, then you can define a robolectric.properties and make sure its found on the classpath. You can specify all the config options in that properties file.

Alexander Stark

unread,
Jul 21, 2015, 9:33:31 AM7/21/15
to robol...@googlegroups.com
Thanks for response.
I already tried to use robolectric.properties but it is ignored. I tried to put the file in a subfolder of my test sources. I tried res and resources as subfolder. Bot neither res nor resources works.
Where do I have to put the properties file that it can be found on the classpath?
Thanks

Erich Douglass

unread,
Jul 21, 2015, 9:59:11 AM7/21/15
to robol...@googlegroups.com

It goes in src/test/resources


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Stark

unread,
Jul 21, 2015, 10:13:32 AM7/21/15
to robol...@googlegroups.com
Ok seems to work. But now I get an error that sdk level 1 is not supported. Means I have to set this manually?
After i tried to set to e.g. 18  next error occurs: java.lang.RuntimeException: java.lang.RuntimeException: build/intermediates/manifests/full/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml
In the documentation I found how to set:
manifest=some/build/path/AndroidManifest.xml
But i have different build types. How can I use a variable for the active running build type?

Kanishk Tripathi

unread,
Jul 21, 2015, 12:03:35 PM7/21/15
to robol...@googlegroups.com
If you are using RobolectricGradleTestRunner, then it will automatically pick the manifest based on your build flavor(if there are different manifests for different flavors).

Alexander Stark

unread,
Jul 22, 2015, 1:32:52 AM7/22/15
to Robolectric
This doesn't work for me. I have to set correct manifest property. But how can I set a dynamic path?
I don't have different flavors. I only have different build types.

Next issue I'm facing is that my tests only run with build type "debug" but not e.g. with release.
Currently I'm still using my CustomBuildConfig class with these static fields:

public final class CustomBuildConfig {
public static final boolean DEBUG = BuildConfig.DEBUG;
public static final String APPLICATION_ID = "
com.xxx.xxx";
public static final String BUILD_TYPE = BuildConfig.BUILD_TYPE;
public static final String FLAVOR = BuildConfig.FLAVOR;
public static final int VERSION_CODE = BuildConfig.VERSION_CODE;
public static final String VERSION_NAME = BuildConfig.VERSION_NAME;
}

Application Id is without prefix as my package name is.
When I execute the tests with ./gradlew testDebug everthing works fine. But when I execute e.g ./gradlew testRelease I get again NotFoundException.


android.content.res.Resources$NotFoundException: unknown resource 2131558435
    at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:351)
    at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:70)
    at android.content.res.AssetManager.getResourceText(AssetManager.java)
    at android.content.res.Resources.getText(Resources.java:235)

    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:358)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:325)
    at android.content.res.Resources.getText(Resources.java:235)

    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:358)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:325)

    at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:33)
    at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:81)
    at android.content.Context.getString(Context.java)
    at com.citrix.g2acommon.G2AApplication.getContentTitle(G2AApplication.java:218)
    at com.citrix.g2acommon.G2AApplication.onCreate(G2AApplication.java:191)
    at com.citrix.g2acustomer.G2ACustomerApplication.onCreate(G2ACustomerApplication.java:137)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1 more

android.content.res.Resources$NotFoundException: unknown resource 2131558435
    at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:351)
    at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:70)
    at android.content.res.AssetManager.getResourceText(AssetManager.java)
    at android.content.res.Resources.getText(Resources.java:235)

    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:358)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:325)

    at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:33)
    at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:81)
    at android.content.Context.getString(Context.java)
    at com.citrix.g2acommon.G2AApplication.getContentTitle(G2AApplication.java:218)
    at com.citrix.g2acommon.G2AApplication.onCreate(G2AApplication.java:191)
    at com.citrix.g2acustomer.G2ACustomerApplication.onCreate(G2ACustomerApplication.java:137)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    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)

Process finished with exit code 255

Would be great tp get help with both issues.
Message has been deleted

Alexander Stark

unread,
Jul 22, 2015, 1:59:31 AM7/22/15
to Robolectric
I debugged in RobolectricGradleTestRunner.
When I use robolectric.properties the build type of config parameter is not set. And some other properties as well.
In my robolectric.properties I have only set

packageName = "com.xxx.xxx"

I expected that all other properties will be taken from the generated BuildConfig.java

Alexander Stark

unread,
Aug 13, 2015, 2:24:31 AM8/13/15
to Robolectric
Is my expectation wrong?
Reply all
Reply to author
Forward
0 new messages