Too many field references to fit in one dex file: 67428; max is 65536. – When Building Xamarin.Android project in VSTS

Objective

When I was trying to build one of my Xamarin.Forms (Android) project in Azure DevOps pipeline, the pipeline build was failed and got this error

trouble writing output: Too many field references to fit in one dex file: 67428; 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:
29 android.app
3066 android.arch.core
8 android.arch.core.executor
14 android.arch.core.internal
3161 android.arch.lifecycle
3066 android.arch.lifecycle.livedata
3066 android.arch.lifecycle.livedata.core
3066 android.arch.lifecycle.viewmodel

etc….

And finally the result was..

“D:\a\3\s\\PathToProject\src\MyProject.Mobile.Droid\MyProject.Mobile.Droid.csproj” (PackageForAndroid target) (1) ->
(_CompileToDalvikWithDx target) ->
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2610,3): error MSB6006: “java.exe” exited with code 2. [D:\a\3\s\PathToProject\src\MyProject.Mobile.Droid\MyProject.Mobile.Droid.csproj]

14 Warning(s)
1 Error(s)

Time Elapsed 00:02:24.15
##[error]Process ‘msbuild.exe’ exited with code ‘1’.

So I looked why it happen and finally I found the reason..

Reason

The reason was according to the libraries I have added, the the number of references are too much for a Default size of the Dex file which is 65536. But my packages had 67428 references.

To Learn more about Multi-Dex visit below link…

https://docs.microsoft.com/en-us/xamarin/android/deploy-test/release-prep/?tabs=windows#multi-dex

Multi-Dex

When the Enable Multi-Dex option is enabled, Android SDK tools are used to bypass the 65K method limit of the .dex file format. The 65K method limitation is based on the number of Java methods that an app references (including those in any libraries that the app depends on) – it is not based on the number of methods that are written in the source code. If an application only defines a few methods but uses many (or large libraries), it is possible that the 65K limit will be exceeded.

It is possible that an app is not using every method in every library that is referenced; therefore, it is possible that a tool such as ProGuard (see above) can remove the unused methods from code. The best practice is to enable Enable Multi-Dex only if absolutely necessary, i.e.the app still references more than 65K Java methods even after using ProGuard.

For more information about Multi-Dex, see Configure Apps with Over 64K Methods.

Solution

So What I had to do is.. Enable the Multi-Dex in my android project for Debug and Release configurations

EnableMultidex

Result

Then Build The project again on pipeline and it will build successfully

References

https://docs.microsoft.com/en-us/xamarin/android/deploy-test/release-prep/?tabs=windows#multi-dex

 

Thanks !

error MSB6006: “java.exe” exited with code 1. – Xamarin.Android – How to solve ?

When I was trying to build an Xamarin Android app with Release configuration in Visual Studio 2017, I got an error saying..

error MSB6006: “java.exe” exited with code 1.

Then I looked for how to solve the error and I found that the issue happen because of Proguard is enabled.

1

Image 1 : Android options for release configuration

Then I found 2 solutions to solve the issue.

  1. Set Java Max Heap Size in Android options.
  2. Change format of Java Max Heap Size in Android options.(Use M/G instead of MB/GB)
  3. Add a  custom Proguard configuration file.

I tried first 2 options but, it didn’t solve the issue.

2

Image 2 : Change max heap size manually (option 1 and 2)

Final Solution.

The issue solved when I add a Proguard configuration file manually.

Steps to add a Proguard configuration file manually.

    1. Go to Android project
    2. Create new file call proguard.cfg
    3. Right Click on proguard.cfg file and go to properties.
    4. Change build action to Proguard Configuration.
    5. Open the file and add below code..
    6. Save and Re-Build the Android Project..
3

Image 3 : Change build action of proguard.cfg

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

 

4

Image 4 : code in proguard.cfg

 

Thanks !

 

Reference

https://forums.xamarin.com/discussion/9305/java-exe-exited-with-code-1-xamarin-android-common-targets

https://stackoverflow.com/questions/38147828/java-exe-exited-with-code-1-proguard-issue

Failed to create directory C:\Program Files(x86)\Android\Android-sdk\temp – How to fix it ?

Objective :

When Trying to install new packages/updates using Android SDK Manager, We can see some errors that “Unable to Install”. So Here are some errors I got when I got when I was trying to Install android API(s) and other package by Right Click and Run the app(Normal way).

Failed to create directory C:\Program Files(x86)\Android\Android-sdk\temp

4 error faild to create directory - Copy

Error 1

URL not found: C:\Program Files (x86)\Android\android-sdk\temp\platform-tools_r25.0.5-windows.zip (Access is denied)

1 error

Error 2

How to Fix it ?

To Fix the error and Install the packages, what we have to do is Run the Android SDK Manager as Administrator..

First, go to C:\Program Files (x86)\Android\android-sdk and select SDK Manager.exe. 

Then Right Click and Run As Administrator.

5 run as administrator

Image 1 : Run as Administrator

6 loading packages

Image 2 : Loading packages

1 install ready

Image 3 : Select Packages to Install/Update

2 click install

Image 4 : Selected Packages

3 select and click install

Image 5 : Accept one by one to Install (Accept radio Button)

3 install sdk s

Image 6 : Accept all selected packages to install using “Accept Licence” radio Button

7 downloading

Image 7 : Downloading

8 finished downloading

Image 8 : Downloaded Successfully

Important

Instead of Running as Administrator by going to Program Files, You can Open Android SDK manager using Visual Studio.

It will run the Android SDK Manager as Administrator itself.

Click the …  Tools -> Android -> Android SDK Manager

9. open in vs

Image 9 : Run SDK Manager using VS as Administrator

Thanks !