Is it possible to use Java 8 for Android development?

nPn picture nPn · Apr 27, 2014 · Viewed 279.3k times · Source

Searching the web, it is not clear if Java 8 is supported for Android development or not.

Before I download/setup Java 8, can some one point me at any "official" documentation that says Java 8 is or is not supported for Android development.

Answer

ekcr1 picture ekcr1 · Apr 27, 2014

UPDATE 2017/11/04 - Android Studio 3.0 now has native support for Java 8. gradle-retrolambda is now no longer needed. See https://developer.android.com/studio/write/java8-support.html

The above link also includes migration instructions if you are using gradle-retrolambda. Original answer below:


Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still it doesn't have invokedynamic, only the new syntax sugar.

If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba. It's a gradle build dependency that integrates retrolambda, a tool that converts Java 8 bytecode back to Java 6/7. Basically, if you set the compiler in Android Studio to compile Java 8 bytecode, thus allowing lambdas, it'll convert it back to Java 6/7 bytecode which then in turn gets converted to dalvik bytecode. It's a hack for if you want to try out some JDK 8 features in Android in lieu of official support.