42

So I recently merged my robolectric 3.0 upgrade with our mainline, which had added multidex support to our application. Unfortunately, this had the unfortunate side effect of causing our tests to fail/not run - they'd simply stop after the first failed attempt to install MultiDex, with this stack trace:

java.lang.RuntimeException: java.lang.RuntimeException: Multi dex installation failed (/Users/me/Data/Projects/my-android/myapp/. (Is a directory)).
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:257)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:194)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

I tried to implement various solutions, such as catching the RuntimeException in my Multidex.Install (as noted here https://github.com/robolectric/robolectric/issues/1328 ) and using the shadow MultiDex provided by Robolectric, but that simply fails with a Gradle TaskExecutionException and exits with value 255.

Has anybody figured out a way to fix this yet? It's really slowing my workflow down.

Mikey Chen
  • 2,370
  • 1
  • 19
  • 31
  • I didn't expect multidex installation to be performed since tests are running on jvm. But who knows – Eugen Martynov Jul 08 '15 at 05:19
  • 2
    Probable duplicate of http://stackoverflow.com/questions/26512170/usage-of-multidexapplication-causes-robolectric-test-for-the-application-class-t/33609900#33609900 – Shubham Chaudhary Nov 09 '15 at 13:12

1 Answers1

131

Just add multi-dex shadow dependency:

testImplementation "org.robolectric:shadows-multidex:4.0.1"

If you are using Robolectric < 3.4 instead use:

testImplementation "org.robolectric:multidex:3.4.2"
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DoronK
  • 4,837
  • 2
  • 32
  • 37
  • 10
    Make sure that the version stated in this line is same as the one in testCompile 'org.robolectric:robolectric:3.0', otherwise it won't work – user2924714 Nov 01 '16 at 10:57
  • 19
    FYI: in Robelectric v3.4+, the sub-project 'shadows-multidex' was renamed to 'multidex" so be sure to rename accordingly e.g `testCompile "org.robolectric:multidex:3.4.2"` – kip2 Aug 24 '17 at 15:06
  • 1
    Why can't I find this in the documentation? – stoefln Apr 30 '18 at 12:53
  • 1
    @stoefln It is in docs in Add-on section. See here:http://robolectric.org/using-add-on-modules/ – SILINIK May 17 '18 at 10:05