Bad class file magic or version

Setting JAVA_HOME back to 1.7 worked for me.


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

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


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