[android] Android dex gives a BufferOverflowException when building

When compiling a specific Android project, and only on my Windows machine, I get a java.nio.BufferOverflowException during from dex. The problem occurs both when using Eclipse and when using Ant.

The output when using Ant is:

...
  [dex] Pre-Dexing C:\MyProject\libs\android-support-v4.jar -> android-support-v4-5f5341d3c1b10a79d7d93f9c1e64421e.jar
  [dex] Converting compiled files and external libraries into C:\MyProject\bin\classes.dex...
   [dx]
   [dx] UNEXPECTED TOP-LEVEL EXCEPTION:
   [dx] java.nio.BufferOverflowException
   [dx]     at java.nio.Buffer.nextPutIndex(Buffer.java:499)
   [dx]     at java.nio.HeapByteBuffer.putShort(HeapByteBuffer.java:296)
   [dx]     at com.android.dex.Dex$Section.writeShort(Dex.java:818)
   [dx]     at com.android.dex.Dex$Section.writeTypeList(Dex.java:870)
   [dx]     at com.android.dx.merge.DexMerger$3.write(DexMerger.java:437)
   [dx]     at com.android.dx.merge.DexMerger$3.write(DexMerger.java:423)
   [dx]     at com.android.dx.merge.DexMerger$IdMerger.mergeUnsorted(DexMerger.java:317)
   [dx]     at com.android.dx.merge.DexMerger.mergeTypeLists(DexMerger.java:423)
   [dx]     at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:163)
   [dx]     at com.android.dx.merge.DexMerger.merge(DexMerger.java:187)
   [dx]     at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
   [dx]     at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
   [dx]     at com.android.dx.command.dexer.Main.run(Main.java:230)
   [dx]     at com.android.dx.command.dexer.Main.main(Main.java:199)
   [dx]     at com.android.dx.command.Main.main(Main.java:103)

BUILD FAILED
C:\Users\Jaap\android-sdks\tools\ant\build.xml:892: The following error occurred while executing this line:
C:\Users\Jaap\android-sdks\tools\ant\build.xml:894: The following error occurred while executing this line:
C:\Users\Jaap\android-sdks\tools\ant\build.xml:906: The following error occurred while executing this line:
C:\Users\Jaap\android-sdks\tools\ant\build.xml:284: null returned: 2

When using Eclipse the message is shorter but similar:

[2013-11-01 14:29:44] APK file is not created for Project: 
[2013-11-01 14:29:46 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
[2013-11-01 14:29:46 - MyProject] Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

Like I said, I don't have this problem on my MacBook, even though they are both upgraded to the latest versions of the Android Build tools: 19.0.0.

This question is related to android dex

The answer is


For the ones facing issue with IntelliJ IDEA 13, uninstall Build Tools 19.


Had the same issue with target version 19 on both project.properties and AndroidManifest.xml with Ant.

Fixed it by:

  • Uninstalled Android SDK Build-Tools 19.0.1
  • Installed Android SDK Build-Tools 19.0.2

I think @Al-Kathiri-Khalid is spot on. The issue is only due to missing support for the API level in Build Tools.


I had the same problem, though my project did not use the support library. Adding libs/android-support-v4.jar to the project worked around the problem without needing to revert the build tools back from v19.


I fixed this problem without downloading the support library or reverting the build tools to 18.1.1. I simply changed the API level to 16+ and the problem vanished. Hope it will help.


Right click on project >> Properties >> Android >> API Level 18 worked for me. But before I did that I did right click on project >> Android Tools >> Add Support Library and restarted Eclipse. You may have to play around with your selected API level.


Try what van said:

Right click your projectandroid toolsandroid support library.

Hope this helps :)


What worked for me was this: I opened the project.properties file from the root of my project and changed target=android-8 to target=android-17


I was able to get my problem project to build by adding this extra line:

sdk.build.tools=18.1.1

...to my project.properties file, which is present in the root of the project folder. All other approaches seemed to fail for me.


I had the same problem after updating to Revision 19. Just do not forget to update ADT, https://dl-ssl.google.com/android/eclipse/. After this, I was able to build project with the latest revision.


Update

right click your project > android tools > android support library

Clean your project and try to built.


Right click on Project>>Properties>>Android and select API Level greater than 15

OR

Add google-play-services_lib to your project by right clicking on project and selecting Project>>Properties>>Android>>Add


java.nio.BufferOverflowException during from dex error This means you don't have the supporting API for that level hence the build fails, there are a number of ways you can fix this.

Check your manifest file uses-sdk android:minSdkVersion="4" and android:targetSdkVersion="14"

Any of the below will fix the problem:-

  • Download the required API level (This might take time) and run your application again
  • Quick dirty fix change your Project target in project.properties to your new target target=android-4
  • Quick clean fix, change the SdkVersion in your manifest and Clean your project to add the changes to your project.properties (My favorite)

I removed the previous Android SDK and Eclipse. I installed the ADT bundle and it works...

This fixed the problem of BufferOverflow on Dex that started after I got API 19. I was previously using Eclipse with Android SDK installed as an add-on package.


After the installation of the new SDK, there is a new folder, "Android Dependencies", under your project file. If you right click and remove it from the build path, you will again be able to build your project.


I solved this problem. Just make this change in the project properties file:

target=android-18
sdk.build.tools=18.1.1

And in the manifest file:

uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="18"

Add the Library file in project.. Project->right click->Properties->android->Library-> click Add and select the Library Project and give apply and ok.. then, clean the project and run again.. if you want restart the eclipse..

And also, sometimes, need to update Android SDK build tools..


None of the other solutions here worked for me after upgrading to Android Studio 0.4.0 and Gradle 1.9.

I resolved the problem by downloading Build Tools 19.0.1 and updating the following line in my build.gradle files:

buildToolsVersion '19.0.0'

to

buildToolsVersion '19.0.1'

Same problem here. Reverted to build tools 18.1.1, restarted Eclipse and that fixed it.