MultiDexApplication的使用导致Application类的Robolectric测试中断

时间:2014-10-22 16:16:50

标签: android robolectric dex

使用gradle def(https://plus.google.com/+IanLake/posts/JW9x4pcB1rj)在支持v4-r21上添加Multi dex支持

apply plugin: 'com.android.application'

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'), 'proguard-rules.pro'
    }
}

dexOptions {
    preDexLibraries = false
}

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

现在这适用于应用程序本身并且我能够构建和部署但是当我为我的Application类运行robolectric测试时,我从ZipUtils(在MultiDex.java中捕获)中获得了失败。其他测试运行正常。这是痕迹 -

  

引起:java.lang.RuntimeException:Multi dex安装失败(/ Users / Code / android-code / android /。(是一个目录))。       在android.support.multidex.MultiDex.install(MultiDex.java:178)       在android.support.multidex.MultiDexApplication.attachBaseContext(MultiDexApplication.java:39)       在android.app.Application.attach(Application.java:181)       在org.fest.reflect.method.Invoker.invoke(Invoker.java:112)       在org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:155)       在org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:430)       在org.robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:236)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)       在org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:177)       在org.junit.runners.ParentRunner.run(ParentRunner.java:309)       在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)       在org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)       在org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)       在org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)       在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)       在org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)

3 个答案:

答案 0 :(得分:52)

添加多dex影子作为依赖

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

这将模拟MultiDex.install调用并且什么也不做,因为Robolectric中没有dex

答案 1 :(得分:4)

这是一个已知错误,等待修复:https://github.com/robolectric/robolectric/issues/1328

答案 2 :(得分:3)

您需要创建一个继承自MultiDexApplication的Application类 attachBaseContext(Context)是Override,如果在JavaVM上运行,则会破坏异常。

  import android.content.Context;
    import android.support.multidex.MultiDexApplication;

    public class Application extends MultiDexApplication {

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