android-test icon indicating copy to clipboard operation
android-test copied to clipboard

ActivityScenario does not work with MultiDex on API level < 21

Open ViliusSutkus89 opened this issue 3 years ago • 8 comments

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

  1. Use ActivityScenario
  2. Enable MultiDex
  3. 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 :)

ViliusSutkus89 avatar Jul 19 '22 15:07 ViliusSutkus89

logcat.txt contains unedited logcat from device boot. afterTests

ViliusSutkus89 avatar Jul 19 '22 15:07 ViliusSutkus89

#940 might be related, since it is also a MultiDex'ed app supporting pre Lollipop devices.

ViliusSutkus89 avatar Jul 19 '22 15:07 ViliusSutkus89

~~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.~~

ViliusSutkus89 avatar Jul 19 '22 19:07 ViliusSutkus89

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 avatar Jul 19 '22 20:07 TWiStErRob

@TWiStErRob , thanks, this works for me

ViliusSutkus89 avatar Jul 20 '22 08:07 ViliusSutkus89

Can you please push the fix to your repo if anyone needs it?

TWiStErRob avatar Jul 20 '22 08:07 TWiStErRob

Fix was implemented in a separate branch, forgot to mention it in README.md. Now it is mentioned :)

ViliusSutkus89 avatar Jul 20 '22 10:07 ViliusSutkus89

@brettchabot is it possible to change the template to dynamically add the keep file when necessary?

TWiStErRob avatar Jul 20 '22 11:07 TWiStErRob