Android Studio 3.0: Java 8 Language Features Support

developers don’t like standing still and want access to the
latest and greatest features in the tools they use, and that
includes programming languages. With Android Studio
3.0, we’re not only shipping Kotlin as an official
new language for Android, but we’re also
making life easier for those of you who prefer
to code in Java by introducing support for Java 8 language
features in the default toolchain. Starting with the Android
Gradle plugin version 3, you can freely use
lambdas, method references, default and static interface
methods, try-with resources, and repeating annotations
on all Android API levels. If you previously switched to
using the experimental Jack compiler to be able
to use these features, please note that
there are now many benefits when using
the default toolchain. Jack offered more
restricted Java 8 support that worked
only for API 24 and above. It wasn’t as fast
when compiling code. And it created problems for
developers using tools that depend on the class format. That’s why we deprecated Jack
and will no longer support it in Android Studio and tools. The good news is that
switching back is very simple. Just delete these lines
from your build file. If you’re using one of the
popular third-party solutions for Java 8 language
features, you might find that they lack
some of the functionality of the default
toolchain, such as being able to seamlessly process
libraries that use Java 8 code. If you want to make
the switch, you’ll have to remove Retrolambda
or DexGuard from your build. Otherwise, the built-in
support will remain disabled. Other than that, make sure
you have the following lines in build Gradle and that you are
using at least Android Gradle plugin version 3. For important plugin
migration instructions, refer to our other video
or the documentation linked in the description. So now that you have
everything set up, what can you actually do
with the new features? Lambdas are a really
nice shorthand notation for implementing functional
interfaces, or in other words, interfaces that have
only one abstract method. Many times when you pass
a Runnable or a listener to another method, you need
all this boilerplate just to run a few lines of code, when
all you could write is this. Android Studio lets
you easily convert between lambdas and
anonymous classes so you can learn
the syntax quickly. With method references
you can sometimes even avoid creating
a lambda by directly passing a method with a correct
signature as a parameter. Use the double colon
syntax like this. If you want to put utility
methods in your interfaces, you can now do it
with a static keyword. Moreover, you can have methods
with default implementations that will not require you to
change any subclasses when you add them to the base interface. Try-with resources
is a great way to deal with closable
resources, such as files. Previously you had to be
extra careful to catch exceptions and close any open
streams in a finally block. Now when you declare a resource
in that try block opening statement, it will be
automatically cleaned up for you after that block exits. The new language
features are provided by transforming bytecode
into a representation compatible with other
platforms, a step which we called desugaring. This is not a backport of
Java 8 packages, and as such, many SDK restrictions still
apply for using newer APIs, like java.lang.string,
in your code. One last thing– as part of
our continuing investment into the toolchain
with Android Studio 3, we’re introducing
a new dexer called D8 that is available
for you to preview. Add this line in your
Gradle properties file, and the build
process will use D8 for converting class
files to dex bytecode. In our tests, D8
compiles faster, and produces smaller
files, and can even improve runtime performance. As an app developer,
you shouldn’t have to make any changes to your
usual workflow when using D8. However, tool developers
need to be aware that D8 moves that desugar
step after any custom bytecode transforms. As such, your
transformation tools might begin to see Java
8 bytecodes when using D8 and have to be made compatible. So have fun writing
more expressive code. And please let us know
on the bug tracker if you run into any issues
when trying the new dexer. And check out your videos about
Android Studio 3.0 features. [MUSIC PLAYING]

13 Replies to “Android Studio 3.0: Java 8 Language Features Support

  1. 00:12 Kotlin as an official new language for Android – does that mean we are encouraged to write new code in Kotlin and Java is no longer preferred?

  2. Java 8 Release : 2014
    Included in Android Studio : 2017
    👏 blazing fast speed.
    I will be waiting for Java 9 support in 2020!

  3. "We know developers don't like standing still and want access to the latest and greatest tools they use…"
    That's why we made Java 8 support right after Java 9 was released…

  4. Hey, for the first time i am using java 8 for build an AR apps. Since, i am new to AR and java 8, here is some error i am getting. I tried very hard to solve it but didn't get anything. Please help..!!
    java.lang.NoSuchMethodError: No virtual method requireActivity()Landroid/support/v4/app/FragmentActivity; in class Lcom/google/ar/sceneform/ux/BaseArFragment; or its super classes (declaration of '' appears in /data/app/
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)

  5. "Note: When developing apps for Android, using Java 8 language features is optional. You can keep your project's source and target compatibility values set to Java 7, but you still need to compile using JDK 8." why do we need to compile with JDK 8, and can we not compile with JDK 7 ?

Leave a Reply

Your email address will not be published. Required fields are marked *