Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 512
Elye Avatar asked Aug 19 '19 01:08

Elye


People also ask

What version of Java is needed for Android studio?

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.

Which JDK should I use for Kotlin?

Starting with Kotlin 1.5, Kotlin needs a JDK with version 1.8 (i.e. Java 8) or higher.

Can I use Java 8 in Android Studio?

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.

Do I need Java for Kotlin?

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.


1 Answers

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.

like image 77
ianhanniballake Avatar answered Oct 29 '22 22:10

ianhanniballake