Dx bad class file magic (cafebabe) or version (0033.0000) with ADK14
Since moving to ADK14, I have been unable to build new apks for release on my Windows 7 system.
Building fails with "conversion to dalvik format failed with error 1", while the console is filled with lots of "Dx bad class file magic (cafebabe) or version (0033.0000)".
The full exception text:
com.android.ide.eclipse.adt.internal.build.DexException: Conversion to Dalvik format failed with error 1
at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:740)
at com.android.ide.eclipse.adt.internal.project.ExportHelper.exportReleaseApk(ExportHelper.java:204)
at com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard.doExport(ExportWizard.java:290)
at com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard.access$0(ExportWizard.java:229)
at com.android.ide.eclipse.adt.internal.wizards.export.ExportWizard$1.run(ExportWizard.java:214)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
I am building an old project that uses lots of libraries, so presumably the problem is related to this fact. Already done all the "Fix Properties"/Clean etc that are suggested when moving to ADK14 (as I naturally had all those problems), but they haven't helped with this.
All the libraries are on the same Android SDK and JDK/JRE version, so this wouldn't seem to be the issue.And in fact, the app is easily built in debug and installed on my old version 1.5 HTC Magic - it is only when I need to export a signed application package that this breaks down.
[Edit] This is on a Windows 7x64 PC. I observe that packing the apk on my Linux laptop (Lucid Lynx) for the exact same code has absolutely no problems whatsoever.
Any ideas? Getting very frustrated with this.
NOTE
Apparently, this error message can be triggered by a variety of different problems. My particular problem was not related to Java 6/7, as I never installed Java 7 in the first place, and compiler compliance was set to Java 6 (I checked at the time, as I had seen that solution suggested elsewhere).
Compiling with Java 6 instead of 7 worked for me, but only after I configured Eclipse to "know about" my JRE6 install path. Previously I only had the JRE7 set up in Eclipse. I could set my compiler compliance level to 1.6 or 1.5 but apparently without the corresponding JRE it didn't really have any effect. I don't really understand why that should be--what does the JRE have to do with compiling, and what does it have to do with Android code?
I found the solution to this problem at last.
If you look in Proguard.bat (Android SDK\tools\proguard\bin), you will find the following line:
call %java_exe% -jar "%PROGUARD_HOME%"\lib\proguard.jar %*
Replace it with the following:
call %java_exe% -jar "%PROGUARD_HOME%"\lib\proguard.jar %1 %2 %3 %4 %5 %6 %7 %8 %9
It's a stupid old issue, that I actually realize that I have seen before, now that I have figured it out. Apparently the Android SDK team still haven't fixed this problem, and it was reintroduced when I did a clean install of the Android SDK.
Upon reading the source of the dex tool, the problem here is that the class file is not within a version that it can understand. The version must be between 45-50 (major version) The version you're using here is 0033.0000 (0x33 in hex), which is 51 in decimal (the version number representing Java 7). This is not speculation on my part, I found this directly in the source code.
Following is speculation, and what solved my problem: I haven't figured out what you have to do to solidly confirm what class file version you'll get upon compiling with various options, but for me the problem was that my library had been compiled with Java 7. Since Android only supports Java 5 or 6, I simply recompiled in 6, and boom, solved.
Changing the Eclipse java version to 6 worked for me. In Eclipse, go to Windows->Preferences->Java->Compiler and set "Compiler compliance level" to 1.6.
Strange thing is that I had Eclipse 3.7.0 and it worked fine, while other computers had 3.7.1 and the jar files built there didn't work...
If you are using Android Studio, this can be fixed by using the above advice to target Java 1.6 in your build.gradle by adding the following lines:
apply plugin: 'java'
sourceCompatibility = 1.6
targetCompatibility = 1.6
Discovered this fix from this blog post: http://www.alonsoruibal.com/my-gradle-tips-and-tricks/