Do I need to upgrade to Java 8 for my Android Studio if I'm just writing in Kotlin?

Elye :

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?

ianhanniballake :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=138820&siteId=1