Bug 58449 - Custom Application subclass extend from MultiDexApplication when MultiDex is enabled crashed when have multiple dex file
Summary: Custom Application subclass extend from MultiDexApplication when MultiDex is ...
Status: RESOLVED DUPLICATE of bug 55268
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 7.3 (15.2)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2017-07-28 03:33 UTC by cheansiong
Modified: 2017-07-31 02:55 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Simulation project (57.29 KB, application/x-zip-compressed)
2017-07-28 03:33 UTC, cheansiong
Details
Platform Build Tool (50.65 KB, image/png)
2017-07-28 03:39 UTC, cheansiong
Details
Decompiled JAR image (14.19 KB, image/png)
2017-07-29 04:35 UTC, cheansiong
Details
Classes Jar 1 (5.39 MB, application/zip)
2017-07-29 04:36 UTC, cheansiong
Details
Classes Jar 2 (153.08 KB, application/zip)
2017-07-29 04:37 UTC, cheansiong
Details
Multidex.keep file (232 bytes, text/plain)
2017-07-29 04:38 UTC, cheansiong
Details
Latest MultiDexTest Project (67.83 KB, application/x-zip-compressed)
2017-07-29 04:41 UTC, cheansiong
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 55268

Description cheansiong 2017-07-28 03:33:32 UTC
Created attachment 23857 [details]
Simulation project

Environment:
- Enabled MultiDex
- Apk has more than 1 DEX file.
- Subclass application.

Problem:
FATAL EXCEPTION: main
Process: MultiDexTest.MultiDexTest, PID: 2232
java.lang.NoClassDefFoundError: mono.MonoPackageManager
	at com.foxsports.test.CustomApp.<init>(CustomApp.java:19)
	at java.lang.Class.newInstanceImpl(Native Method)
	at java.lang.Class.newInstance(Class.java:1208)
	at android.app.Instrumentation.newApplication(Instrumentation.java:990)
	at android.app.Instrumentation.newApplication(Instrumentation.java:975)
	at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
	at android.app.ActivityThread.access$1500(ActivityThread.java:135)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5017)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
	at dalvik.system.NativeStart.main(Native Method)

Note:
- The problem only occurs when more than 1 DEX file is created.
- The problem only occurs when sub-classing application.
- Attached project is taken from https://bugzilla.xamarin.com/show_bug.cgi?id=40976

Simulate to work:
- Get the attached project and remove library, (eg: Xamarin.Facebook which would exceed the maximum method count), to make it runnable.
- Remove Custom Application.

Suggestion on the Multidex:
- It maybe good that Xamarin automatically inject the MultiDexApplication to the code, but it would be better and straight forward if developer are allowed to subclass the MultiDexApplication.
Comment 1 cheansiong 2017-07-28 03:39:38 UTC
Created attachment 23858 [details]
Platform Build Tool
Comment 2 Jon Douglas [MSFT] 2017-07-28 14:51:17 UTC
Hello there,

I believe you are running into the following similar issue where I've posted a workaround:

https://bugzilla.xamarin.com/show_bug.cgi?id=55268#c8

I believe this is an issue of the respective Xamarin.Android glue classes and your custom application class not making the main dex list when they need to be on it prior to startup. The workaround above demonstrates how you can "By hand" fix this. My comments in the following bug below demonstrate that there is a need to fix this:

https://bugzilla.xamarin.com/show_bug.cgi?id=55268#c11

https://bugzilla.xamarin.com/show_bug.cgi?id=55268#c12

Thus I am marking this bug as a duplicate of 55268 as they are more or less the same issue of things not making the main dex list when they should.

*** This bug has been marked as a duplicate of bug 55268 ***
Comment 3 cheansiong 2017-07-29 04:34:23 UTC
Hi there,

I'm aware the exist of the resolution of putting the multidex.keep file, and in the attached project, the binary is also output in the first classes.dex.
I will upload the capture of java decompiled classses image, multidex.keep later

For comment #11, not sure on my understanding, but I had seen the following in the build, and I believe it should compile with the dex file I passed in.
C:\Program Files\Java\jdk1.8.0_102\\bin\java.exe -jar C:\Users\cheansiong\AppData\Local\Xamarin\MonoForAndroid\AndroidSDK\build-tools\26.0.1\\lib\dx.jar --no-strict --dex --multi-dex --main-dex-list=obj\Debug\multidex.keep --output=obj\Debug\android\bin obj\Debug\android\bin\classes "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v7.1\mono.android.jar" "C:\Program Files (x86)\MSBuild\Xamarin\Android\android-support-multidex.jar" 
.... (More parameters goes on)

For comment #12, it is new to me and after I added it, the application still not run-able with other error.
Attached the multidex.keep for your reference.

Please at least download and run it instead of "believing that it is a similar issue"

I reported this after refer to the following references:
https://przemekraciborski.eu/2016/09/30/multidex-in-xamarin/
https://forums.xamarin.com/discussion/64234/multi-dex-app-with-a-custom-application-class-that-runs-on-pre-lollipop
https://tim.klingeleers.be/2017/05/08/android-64k-method-limit-xamarin/
http://www.jon-douglas.com/2016/09/05/xamarin-android-multidex/
https://www.majormojo.co.uk/blog/using-android-java-libraries-with-xamarin-pt-1.html
https://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/
https://bugzilla.xamarin.com/show_bug.cgi?id=55268
Comment 4 cheansiong 2017-07-29 04:35:31 UTC
Created attachment 23887 [details]
Decompiled JAR image
Comment 5 cheansiong 2017-07-29 04:36:40 UTC
Created attachment 23888 [details]
Classes Jar 1
Comment 6 cheansiong 2017-07-29 04:37:02 UTC
Created attachment 23889 [details]
Classes Jar 2
Comment 7 cheansiong 2017-07-29 04:38:24 UTC
Created attachment 23890 [details]
Multidex.keep file

This file is grab from \obj\Debug folder.
Comment 8 cheansiong 2017-07-29 04:41:11 UTC
Created attachment 23891 [details]
Latest MultiDexTest Project

Update the project after adding.

mono/MonoPackageManager.class
mono/MonoPackageManager_Resources.class
mono/MonoRuntimeProvider.class

mono/android/app/NotifyTimeZoneChanges.class
mono/android/app/ApplicationRegistration.class
Comment 9 cheansiong 2017-07-29 05:04:51 UTC
Hello there,

Updates, just realized that the error was a different error this time, and removing Mono Shared Runtime, and mark the solution to use "Shared Runtime" works.

After including the Xamarin.Adnroid "Glue" classes, I mark this issue as duplicate.

This is important knowledge to know the Xamarin.Android glue classes.
https://bugzilla.xamarin.com/show_bug.cgi?id=55268#c12

May I know how can I identify those glue classes to be includes in future?

*** This bug has been marked as a duplicate of bug 55268 ***
Comment 10 Jon Douglas [MSFT] 2017-07-29 16:18:23 UTC
Hi Cheansiong,

One of the easiest ways to figure out the Glue classes is to create a new Xamarin.Android Project, enable multidex and look at obj\Debug\android\bin inside either the classes/ or classes.dex file. Both should have these glue classes.
Comment 11 cheansiong 2017-07-31 02:55:39 UTC
Hello Jon,

Alright, thanks. Xamarin multidex implementation are changing and improving. It seems a better documentation on what need to be done necessary.