Android dex gives a BufferOverflowException when building

AndroidDex

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

No need to downgrade the build tools back to 18.1.11, this issue is fixed with build tools 19.0.1.

If you can't use 19.0.1 for some reason then:

Make sure that the value of android:targetSdkVersion in AndroidManifest.xml matches target=android-<value> in project.properties. If these two values are not the same, building with build tools version 19.0.0 will end in the BufferOverflowException. Source

There is also some indication from comments on this post that you need to target at least 19 (android-19). Please leave a comment if this solution also works if your target is < 19.

This is how the fix looks for my project. The related AOSP issue is #61710.

1 If you really need to downgrade, you don't need to uninstall build tools 19.0.0, simply install 18.1.1 and add sdk.buildtools=18.1.1 to the local.properties file.

Solution 2 - Android

Try what van said:

Right click your projectandroid toolsandroid support library.

Hope this helps :)

Solution 3 - Android

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

Solution 4 - Android

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.

Solution 5 - Android

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.

Solution 6 - Android

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.

Solution 7 - Android

Update

right click your project > android tools > android support library

Clean your project and try to built.

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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'

Solution 11 - Android

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

Solution 12 - Android

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.

Solution 13 - Android

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.

Solution 14 - Android

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"

Solution 15 - Android

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.

Solution 16 - Android

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..

Solution 17 - Android

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

Solution 18 - Android

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)

Solution 19 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbeetstraView Question on Stackoverflow
Solution 1 - AndroidFlowView Answer on Stackoverflow
Solution 2 - AndroidLolo MeView Answer on Stackoverflow
Solution 3 - AndroidStefanoView Answer on Stackoverflow
Solution 4 - AndroidmrrrkView Answer on Stackoverflow
Solution 5 - AndroidlearnerView Answer on Stackoverflow
Solution 6 - AndroidMahendra LiyaView Answer on Stackoverflow
Solution 7 - AndroidHarshidView Answer on Stackoverflow
Solution 8 - AndroidvanView Answer on Stackoverflow
Solution 9 - AndroidZsolt BoldizsárView Answer on Stackoverflow
Solution 10 - AndroidJacob TabakView Answer on Stackoverflow
Solution 11 - AndroidAdíView Answer on Stackoverflow
Solution 12 - AndroidkontinuityView Answer on Stackoverflow
Solution 13 - AndroidJim VitekView Answer on Stackoverflow
Solution 14 - AndroidRKMView Answer on Stackoverflow
Solution 15 - AndroidvpathakView Answer on Stackoverflow
Solution 16 - AndroidharikrishnanView Answer on Stackoverflow
Solution 17 - AndroidChinciusan MarianView Answer on Stackoverflow
Solution 18 - AndroidAl-Kathiri KhalidView Answer on Stackoverflow
Solution 19 - AndroidJunaidView Answer on Stackoverflow