“sourceCompatibility” vs “targetCompatibility”

What is the relationship/difference between sourceCompatibility and targetCompatibility?
What happens when they are set to different values?
Accoording to Gradle documentation:
sourceCompatibility is "Java version compatibility to use when compiling Java source."
targetCompatibility is "Java version to generate classes for."
My understanding is that targetCompatibiliy will generate java bytecode that is compatible with a specific version of Java, is this a subset of the functionality of sourceCompatibility
?

——From this question of stackoverflow

One of the answers

sourceCompatibility = specifies that version of the Java programming language be used to compile .java files. e.g sourceCompatibility 1.6 =specifies that version 1.6 of the Java programming language be used to compile .java files.
By default sourceCompatibility = "version of the current JVM in use" and targetCompatibility = sourceCompatibility
targetCompatibility = The option ensures that the generated class files will be compatible with VMs specified by targetCompatibility . Note that in most cases, the value of the -target option is the value of the -source option; in that case, you can omit the -target option.
Class files will run on the target specified by targetCompatibility and on later versions, but not on earlier versions of the VM

簡單翻譯就是

sourceCompatibility:指定編譯.java文件的jdk版本
targetCompatibility:確保.class文件與targetCompatibility所指定版本或者更新版本的java虛擬機兼容

Paste_Image.png

但是

    compileOptions {
        sourceCompatibility 1.6
        targetCompatibility 1.7
    }
    //不報錯,編輯的時候用的是JDK1.6(只能用JDK1.6的特性),生成的是1.7的.class文件
    compileOptions {
        sourceCompatibility 1.6
        targetCompatibility 1.8
    }
    //不報錯,編輯的時候用的是JDK1.6(只能用JDK1.6的特性),生成的是1.8的.class文件,需要Build Tools 24.0.0 or later,使用Jack工具鏈
    compileOptions {
        sourceCompatibility 1.7
        targetCompatibility 1.8
    }
    //不報錯,編輯的時候用的是JDK1.7,生成的是1.8的.class文件,需要Build Tools 24.0.0 or later,使用Jack工具鏈
compileOptions {
        sourceCompatibility 1.7
        targetCompatibility 1.6
    }
    //報錯,如下圖(1.6是1.8而已)。但是用Jack工具鏈不報錯。
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.7
    }
    //報錯,如下圖(1.6是1.8而已)。但是用Jack工具鏈不報錯。
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.6
    }
    //報錯,如下圖。但是用Jack工具鏈不報錯。
Paste_Image.png

** 綜上所述 **

  • 使用dx工具,則要求targetCompatibility>=sourceCompatibility,否則報錯,

但是這里報錯的根本原因不在于dx,而在于JDK的javac工具,其不支持targetCompatibility<sourceCompatibility的情況###

  • 使用Jack工具鏈,則沒有上述要求,

而使用Jack工具鏈不報錯的根本原因是Jack (.java → .jack → .dex),壓根就不會生成.class文件,這里的targetCompatibility 也就失效了。###

關(guān)于Jack,請看下篇文章Who is Jack?

由此可見,對于Android程序員而言,這個targetCompatibility我們不太需要關(guān)心,我們程序的向前兼容最直接取決于minSdkVersion,所以Android中建議targetCompatibility=sourceCompatibility

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容