当前位置:   article > 正文

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

error:error converting bytecode to dex: cause: dex cannot parse version 52 b

最近在做一个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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

根据提示可以看到,让你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
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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.
  • 1
  • 2

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

android {

    ...
    ...

    defaultConfig {

        ...
        ...

        jackOptions {
            enabled true
        }

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

   ...
   ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

最后Rebuild Project,大功告成~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/311468
推荐阅读
相关标签
  

闽ICP备14008679号