174

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.

5
  • 1
    Do you use libraries? Check their setup Nov 1, 2013 at 13:43
  • @SherifelKhatib, just the support library and Google Analytics V2
    – beetstra
    Nov 1, 2013 at 14:00
  • 10
    Since nobody seemed to have filed an issue on this yet, I have filed one at: code.google.com/p/android/issues/detail?id=61710 Nov 2, 2013 at 12:40
  • trying by removing all android sdk , eclipse , and code.
    – vpathak
    Nov 6, 2013 at 8:37
  • getting simiar error on Centos x64: !SESSION 2013-11-07 17:07:35.305 eclipse.buildId=v22.3.0-887826 java.version=1.7.0_40 java.vendor=Oracle Corporation BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=en_US Framework arguments: -product com.android.ide.eclipse.adt.package.product Command-line arguments: -os linux -ws gtk -arch x86_64 -product com.android.ide.eclipse.adt.package.product java.nio.BufferOverflowException at java.nio.Buffer.nextPutIndex(Buffer.java:519)
    – isti_spl
    Nov 7, 2013 at 15:12

19 Answers 19

230

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.

14
  • 5
    This didn't work for me. Verified all project.properties and targetSdk. I am using the latest version of IntelliJ IDEA 13 Cardea. Fortunately, uninstalling build-tools 19.0.0 did fix the build.
    – James Wald
    Nov 22, 2013 at 6:50
  • 3
    For me to make it work, I also had to set the target SDK to the latest version (19).
    – RolandK
    Nov 27, 2013 at 10:20
  • And also make sure the target sdk version is downloaded with your sdk manager (this is my problem's cause).
    – mrmoment
    Dec 2, 2013 at 3:19
  • 1
    I had target 10 in both AndroidManifest.xml and project.properties. Tried restarting Eclipse and cleaning, but did not help. Then I set target to 19 in config.properties and it worked. Have not tried downgrading build tools. (Target 15 did not work)
    – thomasa88
    Dec 11, 2013 at 15:22
  • 5
    Setting targetSdkVersion in manifest and target in properties, to 19 worked for me Dec 13, 2013 at 7:53
83

Try what van said:

Right click your projectandroid toolsandroid support library.

1
  • 19
    Thank you for this solution, just two clicks and the problem was solved!!!!! (although in my menu, it was "add support library") Nov 12, 2013 at 16:21
57

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

7
  • 3
    Yep, that fixed it. Had to manually delete the old 19.0.0 directory though.
    – beetstra
    Nov 1, 2013 at 16:54
  • This fixed it for me also, though I reverted to 18.0.1
    – NickT
    Nov 2, 2013 at 10:56
  • 1
    I used this answer and I didn't have to revert back: stackoverflow.com/questions/19727915/…
    – Jon
    Nov 15, 2013 at 17:18
  • I verified all project.properties and targetSdk. I am using the latest version of IntelliJ IDEA 13 Cardea and played with the project's SDK version as well. Uninstalling build-tools 19.0.0 was the only way to fix the build for my project.
    – James Wald
    Nov 22, 2013 at 6:51
  • 1
    Thanks. Reverting to build tools 18.1.1 and deleting 19 fixed this for me. I thought I was going crazy
    – Darren
    Nov 23, 2013 at 16:53
50

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.

4
  • i think this is also one of the reason behind the problem. I never used version 19 in my target sdk and still the problem was coming. Probably the sdk build tools was set to 19 which had to be reverted back
    – saurav
    Nov 19, 2013 at 18:35
  • The used target does not determine the used buildtools version.
    – Flow
    Nov 26, 2013 at 22:47
  • I added the latest support library and added this line to the project properties and my problem was fixed... thanks Dec 3, 2013 at 15:34
  • I'm surprised this worked; it's in the AOSP source as sdk.buildtools, not sdk.build.tools, and that was the name that worked for me. Dec 12, 2013 at 18:39
7

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.

2
  • 3
    What about API level below 18 ? How can we run application below API level 18 ?
    – Jayesh
    Nov 14, 2013 at 6:18
  • I had to add project->Android Tools->Add Support Library and it install ADT 19.0.1 Support Library. After that it generates apk fine.
    – user914425
    Jul 23, 2015 at 19:26
7

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.

2
  • I noticed that this folder would cause a duplicate JAR issue. But deleting it didn't resolve my problem.
    – Salsero69
    Nov 29, 2013 at 18:06
  • @Salsero69 Are you using a library project as a reference in your actual project? Nov 30, 2013 at 8:30
5

Update

right click your project > android tools > android support library

Clean your project and try to built.

0
4

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.

3

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.

2
  • How did you change the API level to 16? Dec 21, 2013 at 7:23
  • I simply right clicked my project, and selected Android from the list. It should show up a list of installed android API versions, select the one which has 16 as API level(4.1.2). Dec 21, 2013 at 17:56
3

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'
1
  • Yes, this issue seems fixed in 19.0.1. Also worked fine with "ant debug".
    – Robert
    Jan 2, 2014 at 1:43
2

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

2

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.

1

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.

1

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"
0

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

0

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

0

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)
0

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.

1
  • From where I can install adt bundle ?
    – Jayesh
    Nov 14, 2013 at 6:15
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.