在 build.gradle(Module :app) 檔案,你會看到下列 compileOptions {}
區塊。
build.gradle(Module :app)檔案
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
我自己的經驗是,這部份宣告的版本,將會跟你 CI/CD 的環境變數互相對應。
stackoverflow 上有人提問,這個compileOptions {}
區塊還有使用的必要嗎?
先來看一下這兩個屬性是做什麼用途。
sourceCompatibility: This setting specifies the Java version compatibility for the source code of your project. It determines the version of the Java language features you can use in your code. It ensures that your code is compatible with the specified Java version and prevents the use of language features that are not supported by that version.
sourceCompatibility 指定此專案的 Java 版本相容性。它決定了你在專案中可使用 Java 語言功能的版本。確保你的專案相容於指定的 Java 版本,並避免使用到該版本不支援的功能。
targetCompatibility: This setting specifies the Java version compatibility for the bytecode generated by the Java compiler. It determines the version of the Java runtime environment (JRE) that your compiled code can run on. It allows you to specify a lower Java version than your source code's compatibility level, enabling backward compatibility with older JREs.
targetCompatibility 指定 Java 編譯器生成 bytecode 的 Java 版本相容性。它決定編譯後程式碼可以運行的 Java 運行環境(Java runtime environment)版本。允許你指定比專案程式碼相容版本更低的 Java 版本,進而達到與舊版 JRE 的向後相容性。
資料來源
stackoverflow - Why use sourceCompatibility and targetCompatibility in modern Android development?
stackoverflow - What is the difference between "sourceCompatibility" and "targetCompatibility"?
Stefan M. - sourceCompatibility, targetCompatibility, and JVM toolchains in Gradle explained