Android:Dex 无法解析版本 52 字节代码 [英] Android: Dex cannot parse version 52 byte code

查看:55
本文介绍了Android:Dex 无法解析版本 52 字节代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚切换到 Android Studio 2.1,在尝试编译以前运行的应用程序时出现此错误:

<块引用>

错误:将字节码转换为 dex 时出错:原因:Dex 无法解析版本 52 字节代码.这是由使用 Java 8 或更高版本编译的库依赖项引起的.如果您在库子模块中使用java"gradle 插件,请添加目标兼容性 = '1.7'源兼容性 = '1.7'到该子模块的 build.gradle 文件.

我已经更新了主项目的 gradle.build 文件以强制生成 Java 1.7 代码:

buildscript {存储库{jcenter()}依赖{类路径 'com.android.tools.build:gradle:2.1.0'应用插件:'java'源兼容性 = 1.7目标兼容性 = 1.7}}

我还更新了模块 gradle.build 如下以设置 java 版本:

android {compileSdkVersion 19构建工具版本23.0.2"默认配置{applicationId "com.abc.def"minSdk 版本 19目标SDK版本19}构建类型{释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'}}编译选项{sourceCompatibility JavaVersion.VERSION_1_7targetCompatibility JavaVersion.VERSION_1_7}}

使用 Maven 构建的子模块.在 pom.xml 文件中,我也尝试强制生成 1.7 代码.
我知道我正在使用包含从属模块的程序集工件,但我没有更改任何从属模块,并且上次编译时该模块的 .jar 文件运行良好.

 <插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><!-- maven-compiler-plugin --><version>2.6</version><配置><source>1.7</source><目标>1.7</目标><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></配置><执行><执行><id>make-assembly</id><!-- 用于继承合并--><phase>包</phase><!-- 绑定到打包阶段--><目标><目标>单</目标></目标></执行></执行></插件></插件></build>

我的问题:1) 这是 Android Studio 2.1 的问题吗?其他人看过吗?2) 假设这是我的错误,并且由于错误消息对查找坏模块没有帮助,是否有任何关于查找 V52 代码的建议?我不能在不破坏大量代码的情况下省略库.可以检查 .jar 文件以查找代码修订版吗?提前致谢.-赫菲斯托斯

解决方案

只需将 java 1.8 与 Android Studio 3.0+ 一起使用,并为我设置以下工作:似乎需要最新的构建工具

classpath 'com.android.tools.build:gradle:3.0.0'

android {compileSdkVersion 26构建工具版本26.0.1"默认配置{...//jackOptions {//已弃用//启用真//}}dexOptions {增量真}编译选项{sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}}

I just switched to Android Studio 2.1 and this error showed up when trying to compile an app the was previously working:

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.

I had already updated the main project's gradle.build file to force Java 1.7 code generation:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        apply plugin: 'java'
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
}

I had also updated the module gradle.build as follows to set the java version:

android {
compileSdkVersion 19
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.abc.def"
    minSdkVersion 19
    targetSdkVersion 19
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

The submodule being built with Maven. In the pom.xml file I have also tried to force 1.7 code generation.
I understand that I am using an assembly artifact, which incorporates subordinate modules, but i have not changed any of the subordinate modules and the resulting .jar file for the module ran fine last time I compiled.

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId> <!-- maven-compiler-plugin -->
            <version>2.6</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target> 
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

My question: 1) Is this an Android Studio 2.1 problem? Have others seen it? 2) Assuming this is my error and since the error message gives no help in finding the bad module, are there any recommendations on finding the V52 code? I can't just omit the libraries without breaking large amount of code. Can one inspect a .jar file to find the code revision? Thanks in advance. -Hephaestus

解决方案

just use java 1.8 with Android Studio 3.0+ and set following works for me: it seems need the latest build tools

classpath 'com.android.tools.build:gradle:3.0.0'

and

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        ...        
        //jackOptions { // DEPRECATED
            //enabled true
        //}
    }
    dexOptions {
        incremental true
    }

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

这篇关于Android:Dex 无法解析版本 52 字节代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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