I'm working on an old project, which uses Java. It is based on Java 7. But it's now all Kotlin classes for new code.
In our gradle it is still
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
Just curious, is there a need to upgrade to Java 8, if moving forward I'll be writing in Kotlin?
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
From https://developer.android.com/studio/write/java8-support, it looks like the only reason to do the above is if we intend to write in Java 8. But I'm curious and want to avoid not upgrading and missing out some neat new Java optimization that could benefit to Kotlin project in Android?
A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.
Starting with Kotlin 1.5, Kotlin needs a JDK with version 1.8 (i.e. Java 8) or higher.
Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.
Knowledge of Java is not a must. As I see, Kotlin borrowed something from Python and other languages, so you will not feel yourself altogether lost trying to get acquainted with it. Many things in it are sensibly different.
Yes, you should still upgrade to Java 8. This is primarily because many modern versions of key libraries, such as okhttp 3.13 and higher now require Java 8. In addition, all recent AndroidX -ktx
artifacts, such as Fragment's fragment-ktx
and WorkManager's work-runtime-ktx
all require Java 8.
In the Android's Java 8 Support blog post:
Android’s development toolchain and VM support of newer Java language features is still important even if you are writing 100% Kotlin code. New versions of Java bring more efficient constructs in both bytecode and in the VM that Kotlin can then take advantage of.
So as the general toolchain support for Java 8+ continues to improve, the bytecode generated by your Kotlin code will also continue to improve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With