Which package for MultiDexTestRunner? android.support.multidex or com.android.test.runner

北城以北 提交于 2019-11-30 11:32:22

UPDATE: here's the fix. That's a common pattern, when you see such error message had used a different L<package>; during pre-verification, you need to exclude the package when running the test.

build.gradle

android {
    // ...
    defaultConfig {
        // ...
        multiDexEnabled true
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }
}

dependencies {
    // ...
    // Need to exclude this when running test
    androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
        exclude group: 'com.android.support', module: 'multidex'
    }
}

Application.java

public class Application extends android.app.Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

Note: When you write instrumentation tests for multidex apps, no additional configuration is required if you use a MonitoringInstrumentation (or an AndroidJUnitRunner) instrumentation.

Thus, don't use MultiDexTestRunner, which is deprecated; use AndroidJUnitRunner instead. (This applies to multidex support library v1.0.2+)

android {
    // ...
    defaultConfig {
        // ...
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

Only if you have a custom implementation of the test runner bootstrap with:

public void onCreate(Bundle arguments) {
  MultiDex.install(getTargetContext());
  super.onCreate(arguments);
  ...
}

See: https://developer.android.com/studio/build/multidex.html#testing

The exact problems described in the original question may be related to older versions. I'm adding my experience here, because this is one of the first Google hits when researching the problem.

With recent versions of tools and android plugin you don't need anything else than this in build.gradle:

...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }
}

(I have com.android.tools.build:gradle:1.5.0)

EDIT: As @igor-ganapolsky pointed out, MultiDexTestRunner is deprecated. I don't have access to the sources where this came up anymore, but I suspect problems with MultiDexTestRunner arise when the whole testing setup of the project is in need of overhaul.

The documentation points to the new Testing Support Library as the solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!