'Android coreLibraryDesugaringEnabled, connected test crash with NoSuchMethodError

I added coreLibraryDesugaringEnabled to our app, and it works fine for normal app runs.

  compileOptions {
    coreLibraryDesugaringEnabled true
  }

and

  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'

However, when I run a connected test it crashes with no such method error on the Stream class:

16:03:56.426 11998 12041 E AndroidRuntime: FATAL EXCEPTION: com....
16:03:56.426 11998 12041 E AndroidRuntime: Process: ...
16:03:56.426 11998 12041 E AndroidRuntime: java.lang.NoSuchMethodError: No static method e([Ljava/lang/Object;)Lj$/util/stream/Stream; in class Lj$/util/n; or its super classes (declaration of 'j$.util.n' appears in /data/app/com....-qU6nwXthxze4O_9rcf1hGg==/base.apk!classes2.dex)
16:03:56.426 11998 12041 E AndroidRuntime:  at j$.util.stream.Stream$-CC.of(:1000)
16:03:56.426 11998 12041 E AndroidRuntime:  at ...

I found some vague reference that this might be because the class is referenced prior to the call to Multidex.install? I'm first referencing the class as a static initializer so it is accessed very early. Not sure if this is a red herring but thought I'd mention it.

If it helps the tests are run with AndroidJUnit4ClassRunner.

EDIT: I found a vague reference, in only one article stating that this is not supported in instrumentation tests:

Please note in Android Studio 4.0 using these library desugared types in instrumented tests is not supported.

https://medium.com/androiddevelopers/support-for-newer-java-language-apis-bca79fc8ef65

but even the embedded video from Google on the feature doesn't say anything about that. Also not sure what the AS version would have to do with it. Perhaps they meant the AGP version.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source