This is caused by library dependencies that have been compiled using Java 8 or above

来源:互联网 发布:网络视频管理服务器 编辑:程序博客网 时间:2024/06/06 20:11

错误信息:

Error: Error converting bytecode to dex:Cause: Dex cannot parse version 52 byte code.This is caused by library dependencies that have been compiled using Java 8 or above.If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7'sourceCompatibility = '1.7'to that submodule's build.gradle file.

上面信息意思是,你使用了Java 8 或更高版本编译,你如果使用’java’ gradle 插件,应该加入

targetCompatibility = '1.7'sourceCompatibility = '1.7'

原因分析:

如果仅仅使用java8 ,要为您的项目启用 Java 8 语言功能和 Jack,请在module的 build.gradle 文件中输入以下内容:

android {  ...  defaultConfig {    ...    jackOptions {      enabled true    }  }  compileOptions {    sourceCompatibility JavaVersion.VERSION_1_8    targetCompatibility JavaVersion.VERSION_1_8  }}

在Android Studio中使用 Java 8时,Jack 仅在 Android Studio 2.1 和更高版本上才受支持。要在 Android 的较早版本中测试 Lambda 表达式、方法引用和类型注解,请前往您的 build.gradle 文件,将 compileSdkVersion 和 targetSdkVersion 设置为 23 或更低。您仍需要启用 Jack 工具链以使用这些 Java 8 功能。

注意:如果没有配置Jack 工具链(如下的代码),就会报上面的错误

jackOptions {      enabled true    }

使用retrolambda插件,可以在 Java 5/6/7中使用lambda,它的配置如下,
注意:这里不需要配置Jack 工具链(如下的代码),只需要启用 Java 8 语言功能

apply plugin: 'me.tatarka.retrolambda'android {    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}

解决:

知道了原因,大家就可以根据各自的情况解决问题,我记录一下,我这边的解决方法:

在一个library的module中build.gradle,只配置了

android {    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}

后来把这个配置删除了,就可以了

参考:
使用 Java 8 语言功能
Dagger 2.10-rc1 won’t compile in Android
Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code.
Android: Dex cannot parse version 52 byte code
Error:Jack is required to support java 8 language features [duplicate]
如何在android module中使用Java 8的新特性,比如Lambda?

阅读全文
0 0
原创粉丝点击