Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I "use" Java 8 with Android Development now?

I currently have Java 7 JDK installed. It's been like that for awhile. However, recently, my professor is instructing the class to uninstall Java JDK 7 and install Java JDK 8 instead to be able to work on homework and such.

I've been using Java JDK 7 to write and deploy Android apps without any problems so I'm wondering if it's safe to make the upgrade now the JDK 8? I'm assuming that I won't be needing any Java 8 specific methods or functions in my Android development. So if I stay away from that, will my Android development be at all affected?

On a side note, I haven't been able to find any official documentation that states whether or not Java JDK 8 is safe or not or how to make it safe. Either way, I'm going to have to upgrade to Java 8 soon...

like image 240
waylonion Avatar asked Jan 14 '16 02:01

waylonion


People also ask

Can I use Java 8 for Android?

Java 8 has been supported natively since Android SDK 26. If you wish to use Java 8 language features and your minimal SDK version is lower than 26, . class files produced by the javac compiler need to be converted to bytecode that is supported by these SDK versions.

Can Java still be used for Android development?

Yes. Absolutely. Java is still 100% supported by Google for Android development. The majority of Android apps today have some mix of both Java and Kotlin code.

Which Java version do I need for Android Studio?

Since Android apps are written in Java, you will need the Oracle Java compiler and libraries on your system. These are collectively called the Java Development Kit or "JDK" for short. (If you are certain that you already have JDK 1.8 or higher on your computer, such as from taking CS 106A, you can skip to Step 2.)


1 Answers

In Google I/O 2016, they announced that Android now supports some features of Java 8, but not all the features.

Features like

  • Default and static interface methods
  • Lambda expressions (also available on API level 23 and lower)
  • Repeatable annotations
  • Method References (also available on API level 23 and lower)
  • Type Annotations (also available on API level 23 and lower)

Additionally, the following Java 8 language APIs are also available:

  • Reflection and language-related APIs
    • java.lang.FunctionalInterface
    • java.lang.annotation.Repeatable
    • java.lang.reflect.Method.isDefault()
    • and Reflection APIs associated with repeatable annotations, such as AnnotatedElement.getAnnotationsByType(Class)
  • Utility APIs
    • java.util.function
    • java.util.stream

All what you should do is to use the new jack compiler, to do this you have just to add this to your gradle file build.gradle (Module: app):

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

References:

  • https://developer.android.com/guide/platform/j8-jack.html
  • https://www.youtube.com/watch?v=B08iLAtS3AQ&feature=youtu.be&t=28m14s
like image 69
Zeyad Assem Avatar answered Sep 23 '22 06:09

Zeyad Assem