android – Usage of MultiDexApplication causes Robolectric test for the Application class to break-ThrowExceptions

Exception or error:

Adding Multi dex support with the support v4-r21 using gradle def (

apply plugin: ''

android {
compileSdkVersion 19
buildToolsVersion "20.0.0"

defaultConfig {
    applicationId "info.osom.multidex"
    minSdkVersion 19
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dexOptions {
    preDexLibraries = false

afterEvaluate {
    tasks.matching {'dex')
    }.each { dx ->
    if (dx.additionalParameters == null) {
        dx.additionalParameters = []
    dx.additionalParameters += '--multi-dex'
    dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()

Now this works for the app itself and I’m able to build and deploy but when I run a robolectric test for my Application class, I get a failure from ZipUtils (which is caught in The other tests are running fine. Here is the trace –

Caused by: java.lang.RuntimeException: Multi dex installation failed (/Users/Code/android-code/android/. (Is a directory)).
at org.fest.reflect.method.Invoker.invoke(
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(
at org.robolectric.RobolectricTestRunner.setUpApplicationState(
at org.robolectric.RobolectricTestRunner$2.evaluate(
at org.junit.runners.ParentRunner.runLeaf(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.ParentRunner$
at org.junit.runners.ParentRunner$1.schedule(
at org.junit.runners.ParentRunner.runChildren(
at org.junit.runners.ParentRunner.access$000(
at org.junit.runners.ParentRunner$2.evaluate(
at org.robolectric.RobolectricTestRunner$1.evaluate(
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(

How to solve:

It’s a known bug, wait for a fix:


Add multi-dex shadow as your dependency:

testCompile "org.robolectric:shadows-multidex:3.0"

This will mock MultiDex.install call and do nothing, since there are no dex in Robolectric


You needs to create an Application class that inherits from MultiDexApplication
attachBaseContext (Context) is Override, and crush an exception if run on JavaVM.

  import android.content.Context;

    public class Application extends MultiDexApplication {

       protected void attachBaseContext(Context base) {
          try {
          } catch (Exception e) {
            String vmName = System.getProperty("");
            if (!vmName.startsWith("Java")) {
               throw e;

Leave a Reply

Your email address will not be published. Required fields are marked *