104

I already know that question has been already asked very often and answers, but no one of the answers i found fixed my problem.

It's the error:

Error:Gradle: Execution failed for task ':ffcommunity:preDexDebug'.
 com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    D:\Android SDK\sdk\build-tools\20.0.0\dx.bat --dex --output D:\Users\ReVo\Documents\IntelliJ IDEA\FFCommunity\ffcommunity\build\intermediates\pre-dexed\debug\bananaquery-2ee85432877a057e7414910b8127805535139d5d.jar D:\Users\ReVo\Documents\IntelliJ IDEA\FFCommunity\ffcommunity\libs\bananaquery.jar
Error Code:
    1
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
        at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
        at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
        at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
        at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
        at com.android.dx.command.dexer.Main.processClass(Main.java:665)
        at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
        at com.android.dx.command.dexer.Main.access$600(Main.java:78)
        at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
        at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
        at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
        at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
        at com.android.dx.command.dexer.Main.processOne(Main.java:596)
        at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
        at com.android.dx.command.dexer.Main.run(Main.java:230)
        at com.android.dx.command.dexer.Main.main(Main.java:199)
        at com.android.dx.command.Main.main(Main.java:103)
    ...while parsing com/comuf/revonline/bananaquery/BananaInsert.class
    1 error; aborting

The error is bad class file magic (cafebabe) or version (0034.0000).

I builded and execute the application without problems many times in the same day but now this fails everytime with this message.


Project SDK is Android API 19 Platform and project level is 1.7.

CompileSDK is 19 and buildToolsVersion is '20.0.0'.

Compile options:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

and in dependencies i have:

compile files('libs/bananaquery.jar')

The library is build with SDK Android API 19 Platform with project level 1.7.

My .jar file is inside libs/ folder.

I use IntelliJ IDEA 14 Preview, it could be the reason? With IntellIJ IDEA 13 it remains stuck at "Gradle sync"

compile files could be useless since i have compile fileTree(dir: 'libs', include: ['*.jar']) too

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • Hi I have similar problem but to put project compiler to 1.7 does not help, please can you take a look? http://stackoverflow.com/questions/29098038/errorexecution-failed-for-task-appdexdebug-error-code-1-com-android-dx-cf – Csabi Mar 17 '15 at 13:40
  • The problem it could be a lot of things, I used 1.7 because I was using Java 1.7, try changing it to 1.6 – Marco Acierno Mar 17 '15 at 17:40
  • Hi I tryed the whole project put to 1.6 it does not help, I was using that library before, i just updated the .jar file whit a newer version, It could be a problem that it is compiled with higher java? – Csabi Mar 17 '15 at 21:19
  • I went down the long path of fixing this issue and realized the compilation error was occurring on a library that I was importing. This is an important distinction because no local IDE compiler settings changes will fix something that is being imported. – Shadoninja Jan 11 '16 at 19:19

10 Answers10

96

my JAVA_HOME variable changed to Java 1.8 and I got this error message when compiling a pure java module as a dependency of my android project.

build.gradle of the java module

apply plugin: 'java'

Solution #1: Quick an dirty

I fixed it by setting my JAVA_HOME back to 1.7:

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

Solution #2: change compiler version:

change back to 1.7 for this specific module in its build.gradle

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
passsy
  • 5,162
  • 4
  • 39
  • 65
  • 4
    In the experimental version of Gradle the syntax is: `compileOptions.with { sourceCompatibility=JavaVersion.VERSION_1_7 targetCompatibility=JavaVersion.VERSION_1_7 }` – Stephen Kaiser Sep 24 '15 at 05:06
  • Solution 2 worked for me, even though Android Studio gray-underlined the sourceCompatibility and targetCompatibility saying that "Assignment is not used" – CodyF May 06 '16 at 18:57
39

Ok, my bad.

In the Project SDK section, when you add an Android SDK you should provide the Java SDK and all my Android SDKs uses Java 8 as SDK so it create the class files with the wrong version even if the Project level is 1.7 (i don't know why, i supposed that everything was choosed by Project level).

Now i changed the SDK (the java version "1.x.0" part.)

enter image description here

and it seems to compile fine.

The reason that worked before today was because my SDK was 1.8 and not Android API x

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • i have the same issue. where should i supposed to change this. Thanks – rupesh Jan 19 '15 at 06:58
  • 1
    When you add an Android SDK you should specify at which SDK you refer... (In my case I selected Java 7 because I was using this version) But it depends in your case too. Search online for other errors with the same name, you'll find a lot of information – Marco Acierno Jan 19 '15 at 07:33
  • are you talking about preferences screen ?? – rupesh Jan 19 '15 at 07:35
  • I have searched a lot but didn't get any success – rupesh Jan 19 '15 at 07:37
  • 3
    Open Project structure window > Where is the select with the "Project SDK" message select the SDK you need (your code or the version of the SDK used to compile the external lib). If you have my same problem, check your Android SDK line and click "Edit". You will be moved to another panel, where you have Java SDK, select 1.7 (or the version you need to compile) – Marco Acierno Jan 19 '15 at 15:00
  • 2
    Fixed the same situation by going to Module Settings and editing the JDK location to the home directory where a Java 7 jre was installed – zztonedefzz Feb 13 '15 at 20:24
  • Greetings I'm having same problem but I'm trying to build application from command line. Kindly can you suggest me solution I'm using this command: dx --dex --verbose --output=C:\Users\Muzammil-Husnain\testing\Soothing-snow-fall\MHbin\classes.dex C:\Users\Muzammil-Husnain\testing\Soothing-snow-fall\obj C:\Users\Muzammil-Husnain\testing\Soothing-snow-fall\libs C:\Users\Muzammil-Husnain\testing\UtilityFunctionLib\bin\classes C:\Users\Muzammil-Husnain\testing\PixelResolver\bin\classes C:\Users\Muzammil-Husnain\testing\NextInShop\bin\classes – Muzammil Hussnain Mar 12 '15 at 08:06
  • Greetings I'm having same problem but I'm trying to build application from command line. Kindly can you suggest me solution I'm using this command: `dx --dex --verbose --output=C:\Users\Muzammil-Husnain\testing\Soothing-snow-fall\MHbin\classes.dex C:\Users\Muzammil-Husnain\testing\Soothing-snow-fall\obj C:\Users\Muzammil-Husnain\testing\Soothing-snow-fall\libs C:\Users\Muzammil-Husnain\testing`\UtilityFunctionLib\bin\classes C:\Users\Muzammil-Husnain\testing\PixelResolver\bin\classes C:\Users\Muzammil-Husnain\testing\NextInShop\bin\classes – Muzammil Hussnain Mar 12 '15 at 08:32
  • Man, you just ended my 3h frustration here. Am tho working with AIR - Android - ANE but your solution fixed my issues – Deko Nov 12 '15 at 15:22
27

In case people find @Marco Acierno's answer to be a bit unclear, the solution is to ensure you're building with Java 7 and not a higher version.

For Android Studio, change File -> Project Structure -> SDK Location -> JDK Location to jdk1.7.x. For the command line, ensure java -version outputs java version "1.7.x".

Chris Lacy
  • 4,222
  • 3
  • 35
  • 33
  • I was using IntelliJ IDEA so in my case I had to put the correct Java SDK when I added the Android SDK... (you cannot just select Java 1.x in an Android Module otherwise it won't see Android classes) – Marco Acierno Mar 10 '15 at 06:35
  • 16
    Why cant we use java 8? – Sujay Feb 03 '16 at 09:08
5

Setting JAVA_HOME back to 1.7 worked for me.

Vishal Singh
  • 199
  • 1
  • 3
  • 11
3

change all your module java version to java 1.7 in every build.grade file.

in plugin which is library and application

compileOptions{
    sourceCompatibility=JavaVersion.VERSION_1_7
    targetCompatibility=JavaVersion.VERSION_1_7
}

and in java

sourceCompatibility= 1.7
targetCompatibility= 1.7
baiiu
  • 77
  • 5
2

This problem occurs when you use a .jar file that is not using any feature of Java 6 or higher but was built using Java 6 or higher.

If you built that .jar file then you don't need to change anything in Gradle or ProGuardor Compiler Version. The solution is very simple, just build that .jar file again but using Java 5 or less.

More details.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
1

I had a similar issue, i solved it by upgrading my proguard. get your proguard version by this command

java -jar ~/android-sdks/tools/proguard/lib/proguard.jar 

get the latest progaurd.jar file from here (http://proguard.sourceforge.net)

replace the existing android-sdks/tools/proguard/lib/proguard.jar with new .jar file.

Hopefully this should help you. If u using java 8 then you should upgrade to proguard 5.x coz proguard 4.x does not support java 8.

spaceMonkey
  • 4,475
  • 4
  • 26
  • 34
  • @RookieGuy try setting the java version back to 1.7, that also worked for me. – spaceMonkey Dec 09 '15 at 06:23
  • Thanks for the hint, I had to recompile libraries that were included with transitive dependencies, wrote it up here http://www.uc-mobileapps.com/index.html%3Fp=509.html for reference, since versions have changed quite a bit – RookieGuy Dec 12 '15 at 11:49
1

had a similar issue when i tried to add a self made library from netbeans to android studio. setting the source- and target compatibility in android studio and source/binary format in netbeans (both!) to java 1.7 solved the problem.

in android studio:

Project Structure -> Modules/App -> Propreties -> Source- and Target to 1.7

in netbeans:

File -> Project Properties -> Sources -> Source/Binary Format to 1.7

then clean and build your netbeans projekt and copy the .jar from "NBProj/dist" to "app/libs"

RickPat
  • 487
  • 6
  • 6
0

share the solution of the case if only Java8 installed, just set Java compiler level to 1.7 and then rebuild the project should be OK.

suiwenfeng
  • 1,865
  • 1
  • 25
  • 32
0

I got the error when using a third party library. In accordance with the above-mentioned question, it was necessary to replace the library according to the above stack trace message:

        ...
    ...while parsing com/comuf/revonline/bananaquery/BananaInsert.class
    ...

with the appropriate version.

Corresponding to the question, bananaquery.jar is to be replaced by the Java-compatible version.

gotwo
  • 663
  • 8
  • 16