Android Studio添加开源库和Jar包报错: Error:Error converting bytecode to dex

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34873338/article/details/79314497

最近在做一个Android小项目的时候,添加Github上一个开源库,官方文档只提供了Maven的引入方法,最后自己在Maven Central中找到了Gradle的依赖方式。之后项目可以正常Gradle Build,但是在点击“Run”,发布Apk到模拟器时,报了如下错误:

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.

根据提示可以看到,让你build.gradle中添加几行代码,设置资源和目标JDK版本。但是还是不知道具体是在哪儿,经过一番搜索,找到了答案,网上很多都没说全,还少了一步操作,如下所示:

1.在build.gradle(Module app)中添加如下内容,VERSION_*为你当前使用的JDK版本。

android {

    ...
    ...
    ...

    compileOptions{
        sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_8
        targetCompatibility org.gradle.api.JavaVersion.VERSION_1_8
    }

}

Rebuild Project后,如果报错,请继续往下看,反之即大功告成啦。
报错信息如下:

Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompatibility JavaVersion.VERSION_1_8.
Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompatibility JavaVersion.VERSION_1_8.

这个时候我们还需要一步操作,开启“jack”

android {

    ...
    ...

    defaultConfig {

        ...
        ...

        jackOptions {
            enabled true
        }

    }
    compileOptions{
        sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_8
        targetCompatibility org.gradle.api.JavaVersion.VERSION_1_8
    }

   ...
   ...
}

最后Rebuild Project,大功告成~~

猜你喜欢

转载自blog.csdn.net/qq_34873338/article/details/79314497
今日推荐