得到“不支持的类文件版本52.0”将模块包含到项目中之后 [英] Got "unsupported class file version 52.0" after including a module to a project

查看:131
本文介绍了得到“不支持的类文件版本52.0”将模块包含到项目中之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Studio中创建一个空项目并包含一个纯java模块,它自己编译并完美运行后,我在该模块中的每个类上都会出现以下错误:

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:

错误:PARSE错误:错误:不支持的类文件版本52.0

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

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.

注意这一点,我不知道不要将模块包含在 .jar 库中,它是使用以下指令导入的源代码:

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


推荐答案

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

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 - 此构建文件适用于模块级

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

}

这篇关于得到“不支持的类文件版本52.0”将模块包含到项目中之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆