Bad class file magic or version

╄→尐↘猪︶ㄣ 提交于 2019-11-25 19:35:33

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 

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

and it seems to compile fine.

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

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

Setting JAVA_HOME back to 1.7 worked for me.

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 

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.

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.

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"

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!