2

I have a regular :app module and a DFM :feature.

I place the tests for the DFM in androidTest source set inside :feature.

When I try to launch the test using run configuration created by Android Studio it silently fails with Test framework quit unexpectedly.

If I launch the test through the terminal with the command displayed by Android Studio in Run tab I will see:

$ adb shell am instrument -w -m    -e debug false -e class 'com.amazing.feature.AmazingTest' com.amazing.application.test/com.amazing.feature.TestRunner
...
Unable to find instrumentation info for: ComponentInfo{com.amazing.application.test/com.amazing.feature.TestRunner}

Which is not surprising, since the path to the runner is different:

$ adb shell pm list instrumentation
instrumentation:com.amazing.feature.test/com.amazing.feature.TestRunner (target=com.amazing.application)

The test works if I use that instrumentation on the commandline.

Is there a way to teach Android Studio do that?

pratclot
  • 326
  • 2
  • 8
  • Which AS version are you using? Looks like some cache is corrupted, which links to the wrong runner path. Maybe some package renaming, an update, or copy/pasting of folders could have caused that. Have you created the dynamic feature module via "Create New Module" or in any other way? – nulldroid Sep 06 '21 at 16:26

2 Answers2

3

I got this issue as well, just after upgrading to AGP 7 and the latest Android Studio. In my case it was a bit different, I have the same repository checked out twice, so work on 2 branches simultaneously. In my case, it happened that one branch + android studio instance would work, it did select the correct TestRunner, and the other instance did not work. This lead me to the simple solution of:

  1. Build -> Clean Project
  2. Remove any remaining build folders (was just 1 for me)

Then retry and it did select the correct TestRunner. Other things ive tried, of which I do not know whether or not they helped, are the following:

  • Make sure I use Java 11 (since i'm on AGP 7)
    • Project Structure -> Gradle settings -> Gradle JDK should be something like 11
  • Disable the experimental feature Do not build Gradle task list during Gradle sync (automatically enabled on Android Studio Arctic Fox)
Hylke
  • 598
  • 1
  • 3
  • 17
1

I had the same issue. Turns out gradle creates a wrong targetPackage in the <instrumentation> tag in the dynamic feature module's AndroidManifest.xml of the androidTest APK. You can fix this by setting the testApplicationId in the module's build.gradle defaultConfig.

defaultConfig {    
    testApplicationId 'com.amazing.feature.test' // Set the test package here!
    testInstrumentationRunner 'com.amazing.feature.TestRunner'
    [...]
}

More documentation on this on developer.android.com.

Oliver Metz
  • 2,712
  • 2
  • 20
  • 32