java Android Studio 2.1 将字节码转换为 dex 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37084912/
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-11-03 02:11:22  来源:igfitidea点击:

Android Studio 2.1 Error converting bytecode to dex

javaandroidandroid-studioandroid-gradle-plugin

提问by Suresh Kumar

I am getting the following error since I updated the android studio from 2.0 to 2.1.

自从我将 android studio 从 2.0 更新到 2.1 以来,我收到以下错误。

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.

错误:将字节码转换为 dex 时出错:

原因:Dex 无法解析版本 52 字节代码。这是由使用 Java 8 或更高版本编译的库依赖项引起的。如果您在库子模块中使用 'java' gradle 插件,请将 targetCompatibility = '1.7' sourceCompatibility = '1.7' 添加到该子模块的 build.gradle 文件中。

I tried adding the following snippet in build.gradle, but still the issue persists

我尝试在 build.gradle 中添加以下代码段,但问题仍然存在

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

I saw some of the questions similar to this, but neither of the questions answered. Can anyone help me to solve this? Thanks in advance.

我看到了一些与此类似的问题,但都没有回答。谁能帮我解决这个问题?提前致谢。

回答by sbeliakov

Try

尝试

allprojects {
    tasks.withType(JavaCompile) {
        sourceCompatibility = "1.7"
        targetCompatibility = "1.7"
    }
}

in the main build.gradlefile

在主build.gradle文件中

回答by AndroidMechanic - Viral Patel

Remove the dependencies from build.gradle, comment relevant code and then compile and clean your project.

从 build.gradle 中删除依赖项,注释相关代码,然后编译和清理您的项目。

After a successful clean add the dependencies again and uncomment what you commented after removing the dependencies earlier.

成功清理后再次添加依赖项并取消注释您之前删除依赖项后评论的内容。

回答by Matteo Milesi

Possibile duplicate of Android: Dex cannot parse version 52 byte code. The most quoted answer solved the problem for me.
I added compileOptions and HymanOptions as shown below to my build.gradle to use JDK 1.8.

Android 的可能重复:Dex 无法解析版本 52 字节代码。引用最多的答案为我解决了这个问题。
我将 compileOptions 和 HymanOptions 添加到我的 build.gradle 中,如下所示,以使用 JDK 1.8。

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