试图在MultiDex应用程序中测试Android模块,com.android.test.runner.MultiDexTestRunner无法识别

有人知道为什么我不能使用MultiDexTestRunner吗?

试图在MultiDex应用程序中测试Android模块,com.android.test.runner.MultiDexTestRunner无法识别

我的build.gradle包含:

android {
    ...
    defaultConfig {
        ...
        multiDexEnabled true

        testInstrumentationRunner 'com.android.test.runner.MultiDexTestRunner'
    }

dependencies {
...
    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'junit:junit:4.12'

Gradle的Android插件版本为2.0.0-alpha2(com.android.tools.build:gradle:2.0.0-alpha2)

//Runs all unit tests.
@RunWith(Suite.class)
@Suite.SuiteClasses({ACLTest.class})
public class UnitTestSuite {

    public UnitTestSuite() {}
}

测试类:

@RunWith(AndroidJUnit4.class)
@MediumTest
public class ACLTest {

    static Context context;

    public ACLTest() {
    }

    @BeforeClass
    public static void setUpBeforeClass() {
        ...
        context = InstrumentationRegistry.getTargetContext();
    }

    @Before
    public void setUpBeforeTest() throws Exception {
      ...
    }


    @After
    public void tearDown() throws Exception {
        ...
    }

    @Test
    public void aAGetInstance() throws Exception {
        ...
    }
}

解决方法:

我通过实现android.support.test.runner.AndroidJUnitRunner的扩展并覆盖onCreate解决了我的问题

public class MultiDexAndroidJUnitRunner extends AndroidJUnitRunner {
    @Override
    public void onCreate(Bundle arguments) {
        //To make it work on MultiDex environment.
        //https://plus.google.com/+OleksandrKucherenko/posts/i7qZdVEy3Ue
        MultiDex.install(getTargetContext());

        super.onCreate(arguments);
    }
}
上一篇:我的应用程序安装后,如何检查第二个dex是否已加载完成?


下一篇:Android MultiDex应用程序在Android Studio 3.0上的低API上崩溃