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.
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.
Yes, Android Supports Java 8 Now (24.1.17)
Now it is possible
But you will need to have your device rom run on java 1.8 and enable "jackOptions" to run it. Jack is the name for the new Android compiler that runs Java 8
https://developer.android.com/guide/platform/j8-jack.html
add these lines to build_gradle
Java 8 seem to be the running java engine of Android studio 2.0, But it still does not accept the syntax of java 8 after I checked, and you cannot chose a compiler from android studio now. However, you can use the scala plugin if you need functional programming mechanism in your android client.
Native Java 8 arrives on android! Finally!
To start using supported Java 8 language features, update the Android plugin to 3.0.0 (or higher)
Starting with Android Studio 3.0 , Java 8 language features are now natively supported by android:
Also from min API level 24 the following Java 8 API are available:
Add these lines to your application module’s build.gradle to inform the project of the language level:
Disable Support for Java 8 Language Features by adding the following to your gradle.properties file:
You’re done! You can now use native java8!
Android uses a Java that branches off of Java 6.
As of Android SDK version 19, you can use Java 7 features by doing this. No full support for Java 8 (yet).
java 8
Android supports all Java 7 language features and a subset of Java 8 language features that vary by platform version.
To check which features of java 8 are supported
Use Java 8 language features
Future of Java 8 Language Feature Support on Android
Eclipse Users:
For old developers who prefer Eclipse, google stops support Eclipse Android Developer tools
if you installed Java 8 JDK, then give it a try, if any problems appears try to set the compiler as 1.6 in Eclipse from window menu → Preferences → Java → Compiler. Java 7 will works too:
install multiple JDK and try.
You can indeed use
gradle-retrolamba
gradle build dependency to use Java 8 for Android Development.Below is the complete guide that I have recently followed to run lambda expressions for Android development. The original source of this guide is mentioned at the end.
Preparation
This guide assumes that the reader has a basic understanding of Android Development and it is based on ADT version 22.6.2 because recent ADT version 23.0.2 seems to have problems like layout folder creation. Details about this issue can be found under the following link:
http://code.google.com/p/android/issues/detail?id=72591
Steps in this guide will be given for a Windows 8.1, 64-bit development machine but they can easily be adapted to other platforms. The new build system
Gradle
will be used for build/clean processes and its installation procedure will also be provided. Also, bothJDK 8
andJDK 7
must coexist on the development machine. Steps given below must be followed to install them:bin
folder to your%PATH%
variableJAVA_HOME
with the value of the path of JDK 8 home folderJAVA8_HOME
again with the value of the path of JDK 8 home folderJAVA7_HOME
with the value of the path of JDK 7 home folderjava -version
command and verify that Java 8 is up and runningjavac -version
command in the same window and verify that JDK 8 Java compiler is also up and runningNow, ADT-22.6.2 must be downloaded from the following link:
http://dl.google.com/android/adt/22.6.2/adt-bundle-windows-x86_64-20140321.zip
D:\adt
ANDROID_HOME
with the value of the path of your ADT installation folder, e.g.D:\adt\sdk
Andoid SDK Platform Tools
andAndoid SDK Tools
folders, e.g.D:\adt\sdk\tools
andD:\adt\sdk\platform-tools
, to your%PATH%
variableD:\adt\eclipse
D:\adt\workspace
Android SDK Manager
button which is located on the toolbarAndroid SDK Build tools Rev. 19.1
andAndroid Support Library
only. Un-select everything else and install these two packages.If everything goes well, ADT will be up and running.
The installation of the following tools is also highly recommended:
Eclipse Kepler Java 8 Support: It makes Eclipse recognize new Java 8 syntax extensions and makes you get rid of annoying
red dots
in your Java code editor. It might be installed throughHelp -> Install New Software
in Eclipse. Enter http://download.eclipse.org/eclipse/updates/4.3-P-builds/ into theWork with
field and continue to install it.Nodeclipse/Enide Gradle: It is mainly used to highlight Groovy language keywords. Groovy is used as the DSL for Gradle build scripts. This plugin can be installed through
Eclipse Marketplace
. However, Eclipse within ADT-22.6.2 does not come along withEclipse Marketplace Client
. Therefore, you will first need to installEclipse Marketplace Client
by means ofInstall New Software
tool in Eclipse. Enter http//:download.eclipse.org/mpc/kepler/ into theWork with
field and continue to install it. After installingEclipse Marketplace Client
, you may search forNodeclipse/Enide Gradle
in theEclipse Marketplace Client
and install it.Genymotion Virtual Device: It is a great replacement of the default
Android Virtual Device
which comes along with ADT. AVD is annoyingly cumbersome and it keeps on crashing for no reason. Genymotion makes you prepare Android VD's usingCyanogenMod
images which are executed by Oracle VirtualBox. Its single user license is for free and it can be downloaded from http://www.genymotion.com. Only a login is required and it can also be integrated into Eclipse. Details can be found under:https://cloud.genymotion.com/page/doc/#collapse8
Below is a screenshot of an Android 4.3 based CyanogenMod virtual device,
It might be considered as a fully-fledge Android device running on a x86 or x64 based personal computer. In order to use Google services like
Google PlayStore
on this virtual device, agapps
image for the Android version that it uses must be flashed onto the device. A propergapps
image for the device might be downloaded from CyanogenMod website:http://wiki.cyanogenmod.org/w/Google_Apps
Gradle installation is optional since it is also provided by Android SDK itself but its separate installation is highly recommended. Installation of it might be conducted by following these steps:
Go to Gradle web site: http://www.gradle.org/
Click
Downloads
Previous Releases
choose version 1.10 and download either gradle-1.10-all.zip or gradle-1.10-bin.zipD:\adt\gradle
GRADLE_HOME
with the value of the path of your Gradle installation folder, e.g.D:\adt\gradle
D:\adt\gradle\bin
, to your%PATH%
variablegradle -v
command and verify that it`s up and running If you have come up to this point successfully then it means that you are ready to create your first Android App using Java 8 features.Demo App
A simple app will be created to demonstrate the usage of the tools which were described in the previous section. You may simply follow the steps given below to get an insight on using lambda expressions in Android Developer Tools:
File -> New -> Other -> Android -> Android Application Project
Next
button on the following forms and click theFinish
button on the last one. Wait till ADT finishes loading up the projectNew -> Folder
and name itbuilders
gen (Generated Java Files)
folder and delete it. Gradle will generate the same files for us soon and we will add them into the projects build path. The
gen` folder created by the default Ant builder is no longer needed and the artifacts under that folder will be obsoleteCreate following batch files under the
builders
folder:Fill in these batch files as follows:
gradle_build.cmd:
gradle_post_build.cmd:
gradle_clean.cmd:
Project -> Build Automatically
menu optionProperties -> Builders
and un-select all default builders provided by ADTNew
button in the same window and selectProgram
and clickOK
Main
Tab of the new Builder ConfigurationRefresh
Tab of the new Builder ConfigurationEnvironment
Tab of the new Builder ConfigurationBuild Options
Tab of the new Builder ConfigurationGradle_Post_Build
that usesgradle_post_build.cmd
as its program. All other settings of this builder must exactly be the same with the previously created builder. This builder will be responsible for copying the artifacts created by the build process into thebin
folder.Gradle_Cleaner
that usesgradle_clean.cmd
as its program. OnlyRun the builder
setting in the final tab must be set asDuring a Clean
. All other settings of this builder must exactly be the same with the first builder. This builder will be responsible for cleaning the artifacts created by the build process as the name suggests.New Builders of the
HelloLambda
ProjectExport
Android -> Generate Gradle Build Files
and clickNext
Finish
gradlew
andgradlew.bat
. Also deletegradle
folderProject -> Clean
menu option. Fill in the form that shows up as follows:Clean Project
WindowOK
and wait till the cleaning process completessetContentView
function in yourMainActivity
class:build.gradle
file till thesourceCompatibility
section as follows:Properties -> Java Compiler
option and set all compliance levels to Java 8. This will make Eclipse recognize new Java 8 constructs like lambda expressions.No
in the notification windowBuild project
. Eclipse will start building the project.Build Process
Right-click on the project and go to
Properties -> Java Build Path
. Add the following folders to the build path (also shown in below image):build\source\buildConfig\debug
build\source\r\debug
Eclipse will now be able to recognize
R.java
andbuildConfig.java
files and it will not display anyred dots
which denote errors related to the resource files of the project.Run Configuration
for your Android target platform by right-clicking on the project and then selectingRun As -> Run Configurations
. For instance, this demo application looks like shown below on the Genymotion VD:HelloLambda Application
You may observe in the
LogCat
window that the code snippet with a simple lambda expression works properlySource: Using Java 8 Lambda Expressions in Android Developer Tools
I wrote a similar answer to a similar question on Stack Overflow, but here is part of that answer.
Android Studio 2.1:
The new version of Android Studio (2.1) has support for Java 8 features. Here is an extract from the Android Developers blogspot post:
Prior to Android Studio 2.1:
Android does not support Java 1.8 yet (it only supports up to 1.7), so you cannot use Java 8 features like lambdas.
This answer gives more detail on Android Studio's compatibility; it states:
If you want to know more about using
gradle-retrolambda
, this answer gives a lot of detail on doing that.