3

Suddenly and seemingly for no reason at all, the following error starts presenting itself. After fighting it all day, I discovered enabling Multi-Dex resolved it. Is this error potentially caused by my breaching the 64k reference threshold?

java.lang.RuntimeException: Unable to instantiate application md52323d240c954a07dbdbeb3f1c41a0cab.MainApplication: java.lang.ClassNotFoundException: Didn't find class "md52323d240c954a07dbdbeb3f1c41a0cab.MainApplication" on path: DexPathList[[zip file "/data/app/com.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp-1/lib/arm64, /data/app/com.myapp-1/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:823)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5529)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1576)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6281)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Somewhere I can see the count of references? How do I know if I surpassed this - in VS specifically?

enter image description here

maplemale
  • 2,006
  • 21
  • 35
  • Bug: https://developer.xamarin.com/releases/android/xamarin.android_8/xamarin.android_8.1/#Known_Issues You can clean/rebuild as a workaround on Windows in order to recreate the Java classes.zip file – SushiHangover Dec 31 '17 at 02:12
  • Ah! This makes sense... Well, I have tried clean - rebuilding many times. Also, tried deleting the build, obj directories. Anything else I might need to manually delete? – maplemale Dec 31 '17 at 02:18
  • Try turning multi-dex back off and delete the obj dir and rebuild, that *should* work, but.... otherwise downgrading Xamarin.Android is an option till a fix is released : https://github.com/xamarin/xamarin-android/pull/1142 – SushiHangover Dec 31 '17 at 02:49
  • @SushiHangover Actually deleting the obj dir DID in fact work. Not sure why it didn't before, maybe I was trying too many things at once. I think the reason clicking / unclicking the Multi-Dex setting appeared to work was because it rebuilds the classes zip file. Feel free to write an answer if you want the points. I upvoted Eddiel's answer. But, in the end my question about analyzing for number of references in visual studio (which seems not possible) is a separate issue. Guess I'll download a 3rd party apk analyzer for that. – maplemale Dec 31 '17 at 03:07
  • I added an answer as if you accept it, there are at least half a dozen questions with the same problem that can be marked as duplicates that point to this one. Thanks. – SushiHangover Dec 31 '17 at 03:31
  • 1
    Although this question is most likely resolved by this known issue that I've documented in various mediums(included in the answer below), it should be noted that the answer to this question can also be because there is a known limitation of custom `[Application]` classes not making the main dex list due to fast deployment - https://bugzilla.xamarin.com/show_bug.cgi?id=55050 It can also be because the Application subclass sometimes does not make the main dex list due to it being left from the `multiDexKeepFile` - https://bugzilla.xamarin.com/show_bug.cgi?id=55268 – Jon Douglas Dec 31 '17 at 08:55

2 Answers2

4

java.lang.ClassNotFoundException: Didn't find class

This is a known issue in the creation of the Java classes.zip file in the following release:

December 4th, 2017 - Xamarin.Android 8.1.0.24

  • This version is included in the Visual Studio 2017 version 15.5 release.

Java.lang.ClassNotFoundException

We have had multiple reports regarding applications running into a "Java.Lang.ClassNotFoundException: Didn't find class on path: DexPathList" exception.

Bug: xamarin.android_8.1/#Known_Issues

GitHub PR fix: https://github.com/xamarin/xamarin-android/pull/1142

Workaround:

You can clean/rebuild as a workaround on Windows in order to recreate the Java classes.zip file

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
2

First, you can count the method references (and see other useful insight) using the APK Analyzer tool. It is shipped with Android Studio; you can find it under the Build menu and it's also available from the command line with apkanalyzer.

Second, you will get a build error indicating you need to use multi dex. It looks like:

trouble writing output: Too many field references: 131000; max is 65536. You may try using --multi-dex option.

More here: https://developer.android.com/studio/build/multidex.html

Eddie Lopez
  • 1,101
  • 7
  • 15
  • I saw that... however I'm using Visual Studio and could not find any documentation stating that there is a handled build error such as this. Should it be the same? Also, cannot find an APK Manager in VS. Maybe it's there, I just don't see it. – maplemale Dec 31 '17 at 02:17
  • Take a look at this post: https://stackoverflow.com/questions/10150899/runtimeexception-unable-to-instantiate-application#10158241 – Eddie Lopez Dec 31 '17 at 02:23
  • I saw that... that's VERY old and likely occurring for a different reason. Also, the symptoms are different (in my case it does crash the app). – maplemale Dec 31 '17 at 02:26
  • You do not need to count your own method references. `dx.jar` will throw an error out when compiling via `java -jar dx.jar` and reaching the dex limit. It will come in the form of a generic `java.exe exited with code 2`. – Jon Douglas Dec 31 '17 at 08:34