MultiDexTestRunner的哪个包?android.support.multidex或com.android.test.runner

pzu*_*ulw 14 android android-multidex

页面http://developer.android.com/tools/building/multidex.html#testing 建议

   dependencies {
     compile 'com.android.support:multidex:1.0.1'
     androidTestCompile 'com.android.support:multidex-instrumentation:1.0.1'
   }
   android {
     defaultConfig {
        multiDexEnabled true
        testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"
     }
  }
Run Code Online (Sandbox Code Playgroud)

但是在运行测试时会产生ClassNotFoundException.

API文档和dexdump显示有com.android.test.runner.MultiDexTestRunner.

所以如果我不相信文档而是指定

   dependencies {
     compile 'com.android.support:multidex:1.0.1'
     androidTestCompile 'com.android.support:multidex-instrumentation:1.0.1'
   }
  android {
     defaultConfig {
        multiDexEnabled true
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
     }
   }
Run Code Online (Sandbox Code Playgroud)

然后我明白了

com/company/myapp/MyApp; had used a different Landroid/support/multidex/MultiDexApplication; during pre-verification 
...
IllegalAccessExceptionIllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
Run Code Online (Sandbox Code Playgroud)

我怀疑doc页面是错误的,正确的路径是com.android.test.runner.MultiDexTestRunner ...加上我还有其他一些问题.

请注意,multidex应用程序工作正常.不知何故,第二个MultiDexApplication包含在测试apk中.

问题:
MultiDexTestRunner的正确路径是哪条?为什么我在测试apk中获得第二个MultiDexApplication?

mbm*_*bmc 6

更新:这是修复.这是一种常见的模式,当您看到此类错误消息时had used a different L<package>; during pre-verification,您需要在运行测试时排除该包.

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

Application.java

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

  • 从Gradle插件1.5.0开始,androidTest自动依赖于`com.android.support:multidex-instrumentation`,因此我们不再需要显式依赖.https://sites.google.com/a/android.com/tools/tech-docs/new-build-system (4认同)