Is it possible to use Java 8 for Android development?

后端 未结 26 1976
礼貌的吻别
礼貌的吻别 2020-11-21 22:50

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\" d

相关标签:
26条回答
  • 2020-11-21 23:21

    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.

    0 讨论(0)
  • 2020-11-21 23:22

    UPDATE 2019/10/28

    Android Studio 4.0 solves this issue.

    The D8 compiler patches a backport of the Java 8 native APIs into your APK at compile time and your app will use that code, instead of the native APIs, at runtime. The process is called desugaring.

    0 讨论(0)
提交回复
热议问题