Multidex error after adding App Center Push SDK’S to Your Xamarin.Forms Project

Reading Time: 2 minutes

Are you wondering: what’s this multidex error about? It appears that App Center’s Push SDK uses the Firebase libraries underneath for your app to receive push notifications on Android.

If you’re working with Xamarin.Forms and Microsoft’s App Center and you need push notifications going for you on the mobile target platforms, chances are that you also need to add the App Center Push SDK’s to your project so that the Android version can receive push notification correctly.

But, as soon as I added the Push SDK’s as a Nuget, I got the following problem:

trouble writing output: Too many field references to fit in one dex file: 82376; max is 65536.
You may try using multi-dex. If multi-dex is enabled then the list of classes for the main dex list is too large.
References by package:
2 android.accounts 30 android.app
3069 android.arch.core
8 android.arch.core.executor
14 android.arch.core.internal
3164 android.arch.lifecycle
3069 android.arch.lifecycle.livedata
3069 android.arch.lifecycle.livedata.core
3069 android.arch.lifecycle.viewmodel
1 android.content
38 android.content.pm
...

The solution is to enable Multidex for your project as indicated in this App Center Github issue: https://github.com/Microsoft/AppCenter-SDK-DotNet/issues/249

You can do this by selecting the Android project and opening its properties, selecting Android Build > Enable Multi-Dex:

After setting this option to true for the Debug and Release configurations, I committed the code and tried to run the build in MS App Center

While I was happily building the Release configuration, it suddenly stopped and the build breaks for my Xamarin Forms android project with error “java.exe” exited with code 2

Cycling up through the logs I found the same Too many field references to fit in one dex file issue had arisen.

Luckily, this Xamarin Forum answer pointed me into the right direction: https://forums.xamarin.com/discussion/comment/273277/#Comment_273277

I opened the x.Droid project file by right-clicking it in Visual Studio for Mac and selecting Tools > Edit File:

As you can see in the image below, the key “AndroidEnableMultiDex” was set to “true” for the Debug Configuration.
Unfortunately, this wasn’t the case for the release configuration. It seems that Visual Studio (for Mac) has some kind of bug that prevents the IDE from saving the enabled Multi-Dex value in its properties interface in the project file correctly.

Solution? Copy the code…

true

and paste it into your Release configuration (often indicated by ‘Release|AAnyCPU’):

To my surprise, the AndroidEnableMultiDex was removed as soon as I saved it. And thus the multidex error stayed.

This made me think that I needed to include another configuration targeted at Release configuration, but with the same blank indicators as the generic Debug configuration.

I did just that and added the key AndroidEnableMultiDex to it. It was preserved this time (yeey!). The setup looks like this:

As soon as you add in the extra configuration, App Center Build will pick up the setting as desired and will continue to build, even when you’ve integrated the whole bundle of packages from Firebase so you can receive and handle (App Center’s) Push Notifications.

Hope this helps others to prevent wasting hours to get their build going for them.