If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7'

周六整整一天就被这个奇葩问题困扰了,整整一天闷闷不乐心情烦躁,中午没有照常吃饭没有给狗洗澡晚上就点了个外卖也不跑步遛狗。最后第二天周日终于解决,瞬间突然觉得自己萌萌哒~~~~

首先先让我们看看开头的一个问题

如果只想关注答案的,直接看结尾,但是因为导致这个问题的有很多种,所以建议还是全程看
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.

那么照常google搜索了以下答案
https://www.google.co.jp/search?dcr=0&ei=giL1WdnYEMu88QWQ16roAg&q=Cause%3A+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+%27java%27+gradle+plugin+in+a+library+submodule+add++targetCompatibility+%3D+%271.7%27+sourceCompatibility+%3D+%271.7%27+to+that+submodule%27s+build.gradle+file.&oq=Cause%3A+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+%27java%27+gradle+plugin+in+a+library+submodule+add++targetCompatibility+%3D+%271.7%27+sourceCompatibility+%3D+%271.7%27+to+that+submodule%27s+build.gradle+file.&gs_l=psy-ab.3…5806.5806.0.6354.1.1.0.0.0.0.0.0..0.0….0…1.1.64.psy-ab..1.0.0….0.2BI1_-lJ0gM

那么前面最热门的答案都是:
添加这些配置即可

defaultConfig {
    jackOptions {
        enabled true
    }
}
dexOptions {

}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

或者这个配置

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

运行,报错

Cannot get property 'destinationDir' on null object

又google了一番

如果使用enabled true开启了1.8
注:AS2.1以上时,app的build.gradle里不要加
apply plugin: 'me.tatarka.retrolambda'

好吧好吧,我删掉~
终于可以运行,谁知道……
运行到某个地方,又报错

IncompatibleClassChangeError …
 was expected to be of type direct but instead was found to be of type virtual

于是又google

I see you are building for android, so maybe issue is as follows: build set minSdkVersion 21 - android 5 lollipop AND targetCompatibility for java 8 - which is supported (very limited though) starting from sdk 24, thus causing your build problems. You can address this issue three ways:
1) build for minSdkVersion 24, targeting solid [0.4%](https://developer.android.com/about/dashboards/index.html) of devices
2) get rid of java 8 features and set source/target compatibility to java 7
3) use java 8 features and build using [retrolambda](https://github.com/orfjackal/retrolambda) (with some limitations, present on lib readme)

大致意思还是java8和兼容java7的问题!

那我就纳闷了,我把我以前的项目跟这个项目重新对比,用排除法将配置一个一个的修改复原,终于找到一个问题……

总结

是下面这个包引起的!!!

compile 'org.jetbrains:annotations:15.0'

还是回到这个热门答案
https://stackoverflow.com/questions/37020413/android-dex-cannot-parse-version-52-byte-code
有一个稍微冷门答案提及到

If you use org.jetbrains:annotation:15 and retrolambda plugin then remove line compile org.jetbrains:annotations:15.0 from your build.gradle and the error will disappear. It works for me.

大致意思是如果你使用org.jetbrains:annotation:15包和retrolambda插件,那么删掉org.jetbrains:annotation:15包就可以了…………应该是冲突造成原因了

有时候就是莫名其妙的问题耽搁事件,我的整整一天假期就这么没了。祝大家周末愉快

    原文作者:qq名长是因为你没给我备注
    原文地址: https://www.jianshu.com/p/3a4ade250bc6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞