Is it possible to use Java 8 for Android developme

2018-12-31 02:30发布

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.

21条回答
与风俱净
2楼-- · 2018-12-31 03:09

Follow this link for new updates. Use Java 8 language features

Old Answer

As of Android N preview release Android support limited features of Java 8 see Java 8 Language Features

To start using these features, you need to download and set up Android Studio 2.1 and the Android N Preview SDK, which includes the required Jack toolchain and updated Android Plugin for Gradle. If you haven't yet installed the Android N Preview SDK, see Set Up to Develop for Android N.

Supported Java 8 Language Features and APIs

Android does not currently support all Java 8 language features. However, the following features are now available when developing apps targeting the Android N Preview:

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)

There are some additional Java 8 features which Android support, you can see complete detail from Java 8 Language Features

Update

Note: The Android N bases its implementation of lambda expressions on anonymous classes. This approach allows them to be backwards compatible and executable on earlier versions of Android. To test lambda expressions on earlier versions, remember to go to your build.gradle file, and set compileSdkVersion and targetSdkVersion to 23 or lower.

Update 2

Now Android studio 3.0 stable release support Java 8 libraries and Java 8 language features (without the Jack compiler).

查看更多
高级女魔头
3楼-- · 2018-12-31 03:11

Latest news:

Google announce that with Android N and Android Studio 2.1+, platform will support Java 8. Also stable version of studio 2.1 was released.

At last we can use lambda expressions. No more list filter in for loop. Horeeey.

查看更多
笑指拈花
4楼-- · 2018-12-31 03:12

Android Studio 3.0 started to provide built-in support for some of Java 8 language features, which are:

  • Lambda expressions
  • Method references
  • Type Annotations (information is available at compile time, but not at runtime)
  • Repeating annotations
  • Default and static interface methods

Also starting from API level 24 the following Java 8 API are available:

  • java.util.stream
  • java.util.function
  • java.lang.FunctionalInterface
  • java.lang.annotation.Repeatable
  • java.lang.reflect.AnnotatedElement.getAnnotationsByType(Class)
  • java.lang.reflect.Method.isDefault()

Besides that, the try-with-resources support was extended to all Android API levels.

More Java 8 features are promised to be added in the future.

To start using supported Java 8 language features, update the Android plugin to 3.0.0-alpha1 (or higher) and add the following to your module’s build.gradle file:

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

For more details visit:
https://developer.android.com/studio/write/java8-support.html

查看更多
无与为乐者.
5楼-- · 2018-12-31 03:12

When I asked this question almost 2 years ago the answer really was “officially” no, but as pointed out by ekcr1's answer you can get one of the most highly anticipated features (lambdas) to work if you use retrolamba. At the time I was still using eclipse, as Android Studio was in “preview” mode, so I never did pursue this path.

Today, I think the “official” answer is still no, and while retrolamba still seems like a good way to go, there is another option for those willing to go down a somewhat “unofficial” route can take, namely Kotlin.

Today Kotlin reached 1.0.0. For those not familiar with Kotlin, more info can be found at their website found here:

https://kotlinlang.org

or watch this utube video of a talk given by Jake Wharton

https://www.youtube.com/watch?v=A2LukgT2mKc

查看更多
骚的不知所云
6楼-- · 2018-12-31 03:14

We Can Use Java 8 using:

  1. In build.gradle (Project: myProject) add following

    classpath 'me.tatarka:gradle-retrolambda:x.x.x' //x.x.x is recent version
    
  2. In build.gradle (Module: myModule) add following

    apply plugin: 'me.tatarka.retrolambda'
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
查看更多
人间绝色
7楼-- · 2018-12-31 03:17

I asked this question over 3 years ago and obviously the answers have changed over the years. As many above have already answered, as of sometime back, the answer became Yes. I have never updated the accepted answer because it was the correct answer at the time. (I am not sure what the Stack Overflow policy is on that)

I just wanted to add another answer for those who still search for this topic. As of 5/17/2017 Google also announced that Kotlin is also an official language for Android development.

I have not found an official press release, but I did watch some of the Google I/O videos where it was announced. Here is a link to a blog post by the Kotlin team on the announcement.

查看更多
登录 后发表回答