android-test
android-test copied to clipboard
ActivityScenario does not work with MultiDex on API level < 21
Description
Hello,
I've noticed that ActivityScenario does not launch Activities when running MultiDex'ed applications on devices before API level 21.
Steps to Reproduce
- Use ActivityScenario
- Enable MultiDex
- Deploy on pre Lollipop device
Expected Results
ActivityScenario launching Activity
Actual Results
> Task :app:connectedDebugAndroidTest
Starting 1 tests on test(AVD) - 4.4.2
com.viliussutkus89.activityscenarioissue.ExampleInstrumentedTest > useAppContext[test(AVD) - 4.4.2] FAILED
java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, STARTED, RESUMED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")
at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338)
Tests on test(AVD) - 4.4.2 failed: There was 1 failure(s).
AndroidX Test and Android OS Versions
API levels 16, 17, 18 and 19.
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Link to a public git repo demonstrating the problem:
Recreated the issue from "Empty Activity" template project: https://github.com/ViliusSutkus89/ActivityScenarioIssue Emulator run: https://github.com/ViliusSutkus89/ActivityScenarioIssue/actions/runs/2698898492
Thank you for looking into this :)
logcat.txt contains unedited logcat from device boot.

#940 might be related, since it is also a MultiDex'ed app supporting pre Lollipop devices.
~~Workaround is ActivityTestRule for pre Lollipop, ActivityScenario for Lollipop and later.~~
~~https://github.com/ViliusSutkus89/ActivityScenarioIssue/blob/workaround/app/src/androidTest/java/com/viliussutkus89/activityscenarioissue/ExampleInstrumentedTest.kt~~
~~Probably does not cover all cases, but works for me.~~
That hack won't work as it negates the scenario which you would need, otherwise you wouldn't be trying to run it. You can achieve a similar effect by ignoring on specific API levels.
But it's much simpler and more effective to actually solve the problem...
If you read the logcat error message (the answer is always in the first error's root cause, not the last):
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.viliussutkus89.activityscenarioissue.test-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.viliussutkus89.activityscenarioissue.test-1, /system/lib]]
and even before this error it's showing signs of standard multidex setup missing.
(Sidenote: It would help with diagnostics if you clear logcat before trying a repro, so that it's clear when the repro started. Nothing before starting instrumentation is usually helpful as it's a different process/timeline.)
Set up multiDexKeepProguard as described here:
https://issuetracker.google.com/issues/78108767#comment12
and keep whatever is necessary to run your tests. Note: you cannot keep everything in Espresso since it will likely not fit into the first dex.
@TWiStErRob , thanks, this works for me
Can you please push the fix to your repo if anyone needs it?
Fix was implemented in a separate branch, forgot to mention it in README.md. Now it is mentioned :)
@brettchabot is it possible to change the template to dynamically add the keep file when necessary?