Java 将模块包含到项目后得到“不受支持的类文件版本 52.0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37902840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:46:46  来源:igfitidea点击:

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

javaandroidandroid-studioandroid-gradle-plugin

提问by Sébastien

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:

在 Android Studio 中创建一个空项目并包含一个纯 Java 模块后,该模块可以自行编译并完美运行,我在该模块中的每个类上都收到以下错误:

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

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.

我尝试使用嵌入式 JDK 和我系统上的 JDK 8 (1.8.0_91) 运行该项目,结果是一样的。

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

请注意,我没有将模块作为.jar库包含在内,它是使用以下说明导入的源代码:

include ':app', ':my-module'
project(':my-module').projectDir = new File(settingsDir, '../my-module-java')

采纳答案by Sébastien

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.

明白了,错误是因为我没有在模块本身中指定兼容性选项。这意味着如果您已安装并使用 JDK 8 并且您的 android 项目使用 Java 1.7(默认情况下在 Android SDK 23 及以下版本中)并且它包含一个没有任何使用 Java 1.7 规范的模块,那么该模块将被编译使用 Java 1.8 语法的 JDK 8 会出现错误,因为它们不兼容,并且使用 Java 1.7 的编译器无法解析面向 Java 1.8 且版本为 52 的类文件。

build.gradle- this build file is for module level

build.gradle - 此构建文件用于模块级别

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

}

回答by Elliott Frisch

I know, I specify target version 1_7

我知道,我指定目标版本 1_7

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

Oracle兼容性指南为Java 8说(部分)

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.

根据 JVM 规范,Java SE 8 的类文件版本为 52.0。由 Java SE 8 编译器生成的 52.0 版类文件不能在 Java SE 的早期版本中使用。

Target Java 7 and recompile.

以 Java 7 为目标并重新编译。

回答by 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.8to 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.gradlefiles of Java libraries.

我面对这些错误,我从Java切换后1.8去渣1.7。我的项目由几个库、1 个应用程序模块、3 个 Android 库模块和 2 个 Java 库模块组成。我没有更改 Android 库中的任何内容,而是在build.gradleJava 库文件的依赖项中添加了以下几行。

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.

它为我解决了这个问题。解析错误主要是因为 Java 1.8 类无法解析为 Java 1.7 类。

回答by steveputz

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 sourceCompatibilityand targetCompatibilityin the build.gradle for pure java libraries as explained above.

在将 Android Studio 从 2.1 更新到 2.2.2 后,我第一次遇到此错误,并且很困惑,因为我的系统只安装了 Java 1.7,或者我是这么想的。事实证明,Android 2.2.2 安装了自己的 Java 1.8 JRE (C:\Program Files\Android\Android Studio\jre),因此需要在 build.gradle 中为纯 Java 库指定sourceCompatibilitytargetCompatibility如上所述。

apply plugin: 'java'

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}