Android:Dex 无法解析版本 52 字节代码

新手上路,请多包涵

我刚切换到 Android Studio 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.
>
> ```

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

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


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

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 } }


使用 Maven 构建的子模块。在 pom.xml 文件中,我还尝试强制生成 1.7 代码。

我知道我正在使用一个包含从属模块的程序集工件,但我没有更改任何从属模块,并且生成的模块的 .jar 文件在我上次编译时运行良好。

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

”`

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

原文由 Hephaestus 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 821
2 个回答

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

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

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
    }
}

原文由 foolcage 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您的模块带有 非 Android 特定 的 java 库,这应该有效: apply plugin:'java'

把它放在 build.gradle 文件的顶部,然后重建。

     apply plugin: 'java'
    apply plugin: 'jacoco'

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.11'

        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }

原文由 Hesham Fas 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题