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?
By adding this library, your app can manage the access of additional DEX files. In other words, if you are having more than 64K methods, then you will be having more than one DEX file and these DEX files will be managed by using this multidex support library. Then, modify the module-level build.
Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply means that now you can write more than 65536 methods(references) in your application.
AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests. For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With