Bug 34203 - MultiDexApplication and MultiDex missing
Summary: MultiDexApplication and MultiDex missing
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2015-09-23 08:53 UTC by Dimitar Dobrev
Modified: 2015-09-24 13:46 UTC (History)
1 user (show)

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

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 ANSWERED

Description Dimitar Dobrev 2015-09-23 08:53:58 UTC
These classes are crucial because some Android VM-s lack built-in support for multi-dexing and the latter has to be installed manually.
Comment 1 Atsushi Eno 2015-09-23 22:54:24 UTC
Multidex support is in the project properties. You cannot simply bind and its specific Application class just like normal projects as Xamarin.Android uses its own Application class.
Comment 2 Dimitar Dobrev 2015-09-23 23:00:31 UTC
This answer has nothing to do with my description of the problem.
Comment 3 Atsushi Eno 2015-09-24 04:18:22 UTC
No, it is related. You cannot simply specify arbitrary application class in your AndroidManifest.xml as I explained at comment #1. The option for Multidex support in the project properties is the built-in support for Xamarin.Android + multidex.
Comment 4 Atsushi Eno 2015-09-24 04:23:21 UTC
To make it clearer, (android-sdk)\extras\android\support\multidex\library\libs\android-support-multidex.jar is automatically included as AndroidJavaLibrary if you enable the option.
Comment 5 Dimitar Dobrev 2015-09-24 07:54:50 UTC
Didn't you read the documentation about those classes? Didn't the thought that Google added them for some reason - the same reason I specified in the description?
As I said, the multi-dex option has nothing to do with the problem. There are Android virtual machines for Android which do not support multi-dexing - they post the message of "VM with version 1.6.0 does not have multidex support" (the exact version varies, of course) in the log. This means that a multi-dexed application (that is, one compiled with the option turned on) crashes on such virtual machines. To fix this problem, support for multi-dexing has to be manually installed by the application itself. This is achieved by using one of the 3 approaches described at https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html . Since the first approach does not work with custom Application-derived classes, the last 2 remain - and they are impossible to achieve because Monodroid wraps neither MultiDexApplication nor MultiDex, and it also crashes at overriding attachBaseContext (https://bugzilla.xamarin.com/show_bug.cgi?id=11182).
In the future, if you don't understand the problem, just ask, don't just mark as answered.
Comment 6 Dimitar Dobrev 2015-09-24 09:57:45 UTC
Still, thank you for mentioning that the JAR is included, it helped solve a problem with our workaround (https://bugzilla.xamarin.com/show_bug.cgi?id=34209).
Comment 7 Atsushi Eno 2015-09-24 11:35:13 UTC
Let me explain what you are unaware of.
By enabling multi-dex on the project properties, our msbuild tasks automatically makes the Application class to be derived from MultiDexApplication.

From the list that you mentioned as "3 approaches",

> - Declare this class as the application in your AndroidManifest.xml.
> - Have your Application extends this class.
> - Have your Application override attachBaseContext starting with <snip>

what Xamarin.Android does is the 2nd option.

You can check this intermediate build file for the application:

    obj/Debug/android/src/mono/android/app/Application.java

If you enable multi-dex option on the app project, the first lines would look like this:

    package mono.android.app;

    public class Application extends android.support.multidex.MultiDexApplication {

This should not be applied to ALL app projects (because there is no reason to embed multidex support jar whenever unnecessary and increase the app size), it is not included by default. That's also why the jar is added only when multi-dex option is enabled (you could still manually add it, that will be Distinct()-ed though).

I'm marking this as ANSWERED again, as I'm sure I do understand the requirement :-)
Comment 8 Dimitar Dobrev 2015-09-24 11:44:14 UTC
I didn't know these details, with them it makes sense indeed. I wish you would've told me at the beginning in order to avoid all this, there was no way I could make the connection without them.
The problem is that our app crashes anyway - even though it turns out it was automatically patched to be a MultiDexApplication. I have to investigate some more with this new knowledge in mind and reopen if necessary.
Thank you for sharing that info.
Comment 9 Dimitar Dobrev 2015-09-24 11:53:34 UTC
Could you tell me in which Monodroid version you added that automatic conversion to MultiDexApplication? If it was a very recent change, that might explain the crash since we sometimes forget to update Monodroid at our build server.
Comment 10 Atsushi Eno 2015-09-24 12:16:15 UTC
This one. http://developer.xamarin.com/releases/android/xamarin.android_5/xamarin.android_5.1/#Xamarin.Android_5.1

But that shouldn't be causing problem, because that IDE option did not exist on the earlier versions of Xamarin Studio or Visual Studio.
Comment 11 Atsushi Eno 2015-09-24 13:18:21 UTC
I found this old Android bug from the error message you quoted at comment #5: 
https://code.google.com/p/android/issues/detail?id=80808

That implies that Google had the bug in the older Android SDK build-tools versions (and hopefully they fixed it as mentioned in the above URL).

Anyhow, the error message likely explains why; it is likely because your main dex file does not contain necessary multidex support classes.

One possible cause of the problem is your dx.jar might be old and have the issue mentioned above. Assuming you have fairly up-to-date Xamarin.Android, try installling the latest "build-tools" Android SDK component (build-tools 23.0.1 as of now) and remove older versions.

Or, instead of removing old SDK build-tools, add AndroidSdkBuildToolsVersion build property to the .csproj file within some active <PropertyGroup> :

<AndroidSdkBuildToolsVersion>23.0.1</AndroidSdkBuildToolsVersion>

----

Here's a background information in case you are curious.

Our msbuild tasks use "mainDexClasses(.bat)" tool in the build-tools, which generates a list of classes that should be kept in the main dex. It should be generated at obj/Debug/android/bin/multidex.keep (when you enabled multi-dex option).

It should contain all the required multidex classes.

$ cat obj/Debug/android/bin/multidex.keep | sort
android/support/multidex/MultiDexApplication.class
android/support/multidex/MultiDex.class
android/support/multidex/MultiDexExtractor$1.class
android/support/multidex/MultiDexExtractor.class
android/support/multidex/MultiDex$V14.class
android/support/multidex/MultiDex$V19.class
android/support/multidex/MultiDex$V4.class
android/support/multidex/ZipUtil$CentralDirectory.class
android/support/multidex/ZipUtil.class
<snip>

I believe this mainDexClasses tool should generate a valid list of the required classes. If that list does not contain any expected class, it is likely a bug in Android SDK.
Comment 12 Dimitar Dobrev 2015-09-24 13:46:32 UTC
Thank you very much for this information. Now that's what I call support. :)
I have to get in touch with my colleagues, which can only happen tomorrow. But this might as well explain the problem. I will definitely let you know about the results.


Cheers.