0

Edit: Please note the following issue does not occur when using Proguard, or in debug. Only issue with R8 in release

Step to reproduce:

  • Create a new blank Xamarin.Android application
  • Without making any change, simply run in debug make sure the project compile and run
  • Enable r8 and multi-dex:

enter image description here

  • Create release APK

When running the release APK, it will crash on startup with error:

2021-06-04 11:35:01.818 20194-20194/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.companyname.app1, PID: 20194
    java.lang.RuntimeException: Unable to instantiate application androidx.multidex.MultiDexApplication: java.lang.ClassNotFoundException: Didn't find class "androidx.multidex.MultiDexApplication" on path: DexPathList[[zip file "/data/app/com.companyname.app1-S2R0G2ABxfLbmKhyk54SLg==/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.app1-S2R0G2ABxfLbmKhyk54SLg==/lib/x86, /data/app/com.companyname.app1-S2R0G2ABxfLbmKhyk54SLg==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1226)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431)
        at android.app.ActivityThread.access$1300(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.multidex.MultiDexApplication" on path: DexPathList[[zip file "/data/app/com.companyname.app1-S2R0G2ABxfLbmKhyk54SLg==/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.app1-S2R0G2ABxfLbmKhyk54SLg==/lib/x86, /data/app/com.companyname.app1-S2R0G2ABxfLbmKhyk54SLg==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1148)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1218)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431) 
        at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

I've played around with Linking options, but they all result in the same result.

A simple project can be found here: https://drive.google.com/file/d/1oihKvJnRjxGiOaa2JS0FYKlQYI615p91/view?usp=sharing

Any help is highly appreciated!

Shirley G
  • 352
  • 2
  • 9
  • follow the official document about multidex:[Configure your app for multidex](https://developer.android.com/studio/build/multidex.html#mdex-gradle) – Cherry Bu - MSFT Jun 07 '21 at 05:41
  • Hi @CherryBu-MSFT I'm having trouble with Xamarin, not native Android. Thanks. – Shirley G Jun 07 '21 at 15:20
  • Just an update that I was able to build with a Mac with the same project... So I suspect it's a Visual Studio issue. On Mac, perform a clean then build the APK works fine. – Shirley G Jun 15 '21 at 21:33
  • Maybe, I also encounter same problem sometimes, glad to hear you have solved your problem. – Cherry Bu - MSFT Jun 16 '21 at 06:55

1 Answers1

-1

When enabling multidex the correct multidex library (either com.android.support:multidex:1.0.3 or androidx.multidex:multidex:2.0.1) has to be used. As class androidx.multidex.MultiDexApplication is missing use androidx.multidex:multidex:2.0.1. Could be that the project template in Xamarin defailts to com.android.support:multidex:1.0.3.

See Multidex support prior to Android 5.0 for details.

sgjesse
  • 3,793
  • 14
  • 17
  • If you read the original post carefully, multi-dex is only an issue when using R8 and in Xamarin, this post is for Xamarin only and related to R8 only. My project runs fine with Proguard or in debug... – Shirley G Jun 07 '21 at 15:22
  • Sorry for missing that the project works in debug mode and with ProGuard. Do you know which version or R8 is used, and have you tried the latest (latest stabke is 2.2.64 - https://maven.google.com/web/index.html#com.android.tools:r8)? If possibly you could try to produce a dump (see https://r8.googlesource.com/r8/+/refs/heads/main/doc/compilerdump.md) from Xamarin if it is possible to pass a system property to the R8 invocation. You can then create a report on https://b.corp.google.com/issues/new?component=326788&template=1025938. – sgjesse Jun 09 '21 at 13:04