问题
The page http://developer.android.com/tools/building/multidex.html#testing advises
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"
}
}
But that produces a ClassNotFoundException when the tests are run.
The API documentation and dexdump show that there is com.android.test.runner.MultiDexTestRunner.
So if I disbelieve the documentation and instead specify
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"
}
}
Then I get
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
I suspect that the doc page is wrong and the correct path is com.android.test.runner.MultiDexTestRunner ... plus I have some other issue.
Note the multidex application works fine. Somehow a second MultiDexApplication is included in the test apk.
Questions:
Which is the correct path for MultiDexTestRunner?
Why am I getting a second MultiDexApplication in the test apk?
回答1:
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);
}
}
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/28056075/which-package-for-multidextestrunner-android-support-multidex-or-com-android-te