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

38,149

Solution 1

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

}

Solution 2

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.

Solution 3

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.

Share:
38,149
Sébastien
Author by

Sébastien

Updated on July 09, 2022

Comments

  • Sébastien
    Sébastien almost 2 years

    After creating an empty project within Android Studio and including a pure java module, which compiles and works perfectly on its own, I get the following error on every single class within that module:

    Error:PARSE ERROR: Error:unsupported class file version 52.0

    I tried to run the project using the embedded JDK and the one that I have on my system - JDK 8 (1.8.0_91), the result is the same.

    Note this, that I don't include the module as .jar library, it is source code which is importing with following instruction:

    include ':app', ':my-module'
    project(':my-module').projectDir = new File(settingsDir, '../my-module-java')
    
    • Elliott Frisch
      Elliott Frisch almost 8 years
      I don't think android supports Java 8 yet.
    • Sébastien
      Sébastien almost 8 years
      @ElliottFrisch I know, I specify target version 1_7.
    • Doug Stevenson
      Doug Stevenson almost 8 years
      @Sébastien Maybe you could post your build.gradle files for both modules and let us check that for you.
  • Evgeniy Mishustin
    Evgeniy Mishustin almost 8 years
    I don't apply 'java' plugin but still have same exception
  • gkondati
    gkondati over 7 years
    you cannot apply java plugin for android project know?
  • Jaime Montoya
    Jaime Montoya about 7 years
    It fixed the problem for me! I switched from jre1.8.0_121 to jre7 and my App finally compiled.
  • steveputz
    steveputz about 7 years
    this is a pure java library used by my android project
  • OneWorld
    OneWorld over 6 years
    I had to omit apply plugin: 'java' in order to avoid stackoverflow.com/questions/26861011/…
  • annoying_squid
    annoying_squid over 5 years
    @JaimeMontoya You shouldn't have to switch to an older jre as newer jre's are backwards compatible. Just recompile the source with the -target option for 1.7.
  • Jaime Montoya
    Jaime Montoya over 5 years
    @annoying_squid Interesting. I did not know that.
  • Tom
    Tom over 5 years
    for the one using maven of course the same apply : <configuration> <source>1.7</source> <target>1.7</target> </configuration>