Got “unsupported class file version 52.0” after including a module to a project

為{幸葍}努か 提交于 2019-11-27 15:27:05

Got it, the error was because I didn't specify compatibility options in the module itself. That means if you have installed and using JDK 8 and your android project uses Java 1.7 (which is by default in Android SDK 23 and below) and it has a module included without any specification to use Java 1.7, then that module will be compiled with JDK 8 using Java 1.8 syntax and there will be an error because they are not compatible and compiler that uses Java 1.7 can't parse class files which were targeting Java 1.8 and have the version 52.

build.gradle - this build file is for module level

apply plugin: 'java'

buildscript {
    tasks.withType(JavaCompile) {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    // Your libraries here

}

I know, I specify target version 1_7

The Oracle Compatibility Guide for Java 8 says (in part),

The class file version for Java SE 8 is 52.0 as per the JVM Specification. Version 52.0 class files produced by a Java SE 8 compiler cannot be used in earlier releases of Java SE.

Target Java 7 and recompile.

Nagaraju Gajula

I've faced similar errors while building the project.

Error:PARSE ERROR
Error:unsupported class file version 52.0

I faced these errors after I've switched from Java 1.8 to Java 1.7. My project consists of several libraries, 1 app module, 3 Android library modules and 2 Java library modules. I didn't change anything in Android libraries, but added the following lines in dependencies of build.gradle files of Java libraries.

sourceCompatibility = 1.7
targetCompatibility = 1.7

It solved the problem for me. Parsing error was mainly because the Java 1.8 classes couldn't be parsed into Java 1.7 classes.

I got this error for the first time after updating Android Studio from 2.1 to 2.2.2, and was puzzled because my system only had Java 1.7 installed, or so I thought. It turns out Android 2.2.2 installs its own Java 1.8 JRE (C:\Program Files\Android\Android Studio\jre), thus the need to specify sourceCompatibility and targetCompatibility in the build.gradle for pure java libraries as explained above.

apply plugin: 'java'

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!