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.

Flow
  • 23,572
  • 15
  • 99
  • 156
beetstra
  • 7,942
  • 5
  • 40
  • 44
  • 1
    Do you use libraries? Check their setup – Sherif elKhatib Nov 01 '13 at 13:43
  • @SherifelKhatib, just the support library and Google Analytics V2 – beetstra Nov 01 '13 at 14:00
  • 10
    Since nobody seemed to have filed an issue on this yet, I have filed one at: http://code.google.com/p/android/issues/detail?id=61710 – CommonsWare Nov 02 '13 at 12:40
  • trying by removing all android sdk , eclipse , and code. – vpathak Nov 06 '13 at 08: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 07 '13 at 15:12

19 Answers19

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.

Flow
  • 23,572
  • 15
  • 99
  • 156
  • 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 '13 at 06:50
  • 3
    For me to make it work, I also had to set the target SDK to the latest version (19). – RolandK Nov 27 '13 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 02 '13 at 03: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 '13 at 15:22
  • 5
    Setting targetSdkVersion in manifest and target in properties, to 19 worked for me – Jaldip Katre Dec 13 '13 at 07:53
  • My target api level was set to 10 in both the manifest and project properties, for me only downgrading to SDK Build-tools 17 (since I didn't have 18 installed) worked. (Im using intellij 13 by the way) – PiersyP Dec 18 '13 at 16:46
  • Thank you. Target sdk in the manifest and the target in the .properties file must be the same. Worked for me. – Ahmed Khalaf Dec 30 '13 at 13:52
  • @JD.com it worked for me, but why should we change that to 19? – twlkyao Jan 06 '14 at 12:22
  • @twlkyao Well the name "target" itself states the requirement, Probably this was not required in SDK tools version less than 22.3 – Jaldip Katre Jan 06 '14 at 12:29
  • @JD.com Does this mean we have to update the **targetSDK** to the latest version? – twlkyao Jan 06 '14 at 13:00
  • @twlkyao No, Only the targetSdkVersion in manifest and target in project.propeeties must be the same value – Jaldip Katre Jan 06 '14 at 13:18
  • @JD.com But why only have I to set them to 16 and above that can solve the issue. – twlkyao Jan 06 '14 at 13:45
  • `target=android-17` is must equals to `android:targetSdkVersion="17"` – Pratik Butani Jan 17 '14 at 06:08
  • Is it possible to get this to work when using `target=android-15`? – AfromanJ Mar 28 '14 at 15:39
83

Try what van said:

Right click your projectandroid toolsandroid support library.

starball
  • 20,030
  • 7
  • 43
  • 238
Lolo Me
  • 831
  • 5
  • 3
  • 19
    Thank you for this solution, just two clicks and the problem was solved!!!!! (although in my menu, it was "add support library") – Christopher Nov 12 '13 at 16:21
57

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

Flow
  • 23,572
  • 15
  • 99
  • 156
Stefano
  • 714
  • 6
  • 3
  • 3
    Yep, that fixed it. Had to manually delete the old 19.0.0 directory though. – beetstra Nov 01 '13 at 16:54
  • This fixed it for me also, though I reverted to 18.0.1 – NickT Nov 02 '13 at 10:56
  • 1
    I used this answer and I didn't have to revert back: http://stackoverflow.com/questions/19727915/android-dex-gives-a-bufferoverflowexception#answer-19952656 – Jon Nov 15 '13 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 '13 at 06: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 '13 at 16:53
  • @Stefano: I am interested in knowing, how did you conclude this is what you had to do? – Swanand Nov 29 '13 at 15:38
  • Just to let others know: no need to downgrade, the answer from Flow is better. – Javier Sedano Dec 06 '13 at 14:36
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.

mrrrk
  • 2,186
  • 20
  • 17
  • 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 '13 at 18:35
  • The used target does not determine the used buildtools version. – Flow Nov 26 '13 at 22:47
  • I added the latest support library and added this line to the project properties and my problem was fixed... thanks – speedynomads Dec 03 '13 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. – Yoni Samlan Dec 12 '13 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
learner
  • 11,490
  • 26
  • 97
  • 169
  • 3
    What about API level below 18 ? How can we run application below API level 18 ? – Jayesh Nov 14 '13 at 06: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 '15 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
5

Update

right click your project > android tools > android support library

Clean your project and try to built.

Harshid
  • 5,701
  • 4
  • 37
  • 50
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
van
  • 178
  • 5
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.

Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37
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'
Jacob Tabak
  • 8,044
  • 3
  • 24
  • 32
2

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

Adí
  • 854
  • 12
  • 27
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.

Arif Amirani
  • 26,265
  • 3
  • 33
  • 30
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.

Jim Vitek
  • 4,174
  • 3
  • 23
  • 32
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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
R_K
  • 803
  • 1
  • 7
  • 18
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vpathak
  • 1,133
  • 12
  • 12
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..

harikrishnan
  • 1,985
  • 4
  • 32
  • 63
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)
alkathirikhalid
  • 887
  • 12
  • 18
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

Junaid
  • 3,477
  • 1
  • 24
  • 24