I have an app set up with Multidex and appears to load and work on older Android APIs however, I sometimes get errors from the instrumented test runner where it cannot find any tests. I can find details on on how to add multidex to an Android app, but there are several solutions depending on the support library used and none seem to directly address running tests with multidex using the current recommended AndroidX libraries. On Android API level 19, I currently get this error:
$ ./gradlew connectedCheck
> Task :app:connectedDebugAndroidTest
Starting 0 tests on API-19(AVD) - 4.4.2
com.android.build.gradle.internal.testing.ConnectedDevice > No tests found.[API-19(AVD) - 4.4.2] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner
expects (e.g. don't inherit from TestCase or lack @Test annotations).
Running this on an emulator with API level 21 successfully runs all the tests. I do not have Proguard or minify enabled. To enable Multidex, I have made the following changes. I added this to the app's build.gradle:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
}
dependencies {
implementation "androidx.multidex:multidex:2.0.1"
androidTestImplementation 'org.mockito:mockito-core:3.4.6'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker:2.28.0'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.0'
}
My manifest defines a class that inherits from androidx.multidex.MultiDexApplication
in the android:name attribute.
I have seen mention of adding a multidex test runner to the AndroidManifest.xml for the Test app, but I don't currently have an explicit manifest for it and it seemed to only be in the context for the old Android Support Library, not AndroidX. What do I need to do to get instrumented tests to work with Multidex and AndroidX on API levels 16-19?
My manifest defines a class that inherits from androidx.multidex.MultiDexApplication
This only works when you do not override the Application class in the code. Otherwise your Application class should extend MultiDexApplication
as described in documentation
Now tests will be found but will be crashed, I solved the issue by keeping test runner classes in the first dex by adding this to build.gradle
:
android {
...
buildTypes {
...
debug {
multiDexKeepProguard file('multidex-config.pro')
}
}
}
and multidex-config.pro
contains:
-keep class androidx.test.** { *; }