Google has recently released a preview version of Android N, and brand new Java 8 features appeared around. They gave us a sneak peak about the future of Android development.
In this talk we are going to showcase all the supported new language features up to date, detailing which ones are retrocompatible and which ones will just be available starting on API 24 (N). We will get our hands on a Clean Architecture sample project which will use java 8 functionalities on its diffent layers, in order to make people think a little bit more about how those new language features can become handy for us to simplify our code and project architecture.
11. Jill
Jayce
Jack
Extract Res
● Translate library dependencies to Jack format (.jack)
● Prepares them to be quickly merged with other .jack files
Select Res Resources
directory
JACK used to generate a
pre-dexed library
Jayce
Pre-dex
Res
.jack
.class
res
.jar
Jill (Jack Intermediate library linker)
13. ● No more .class intermediate step in toolchain:
○ Bytecode manipulation tools based on .class files (like JaCoCo)
are not working anymore.
○ Lint detectors based on .class file analysis not working anymore.
● Java 8 just supported on versions >= N (API 24)*
Limitations
14. Okay but, how do I use the new tools ?
Let’s calm down (vamo a calmarno.)
15. ● For new projects: File -> New project -> pick Android N
● For already started projects: Add this to your root build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha4'
}
}
22. ● They make Java 8 interfaces very similar to Traits or rich interfaces
from other modern languages
● They allow developers to compose class behavior based on multiple
interfaces
● Interfaces cannot store state
Interfaces - Default methods
24. ● I would use them for utility methods tied to the contract
Interfaces - Static methods
25. public interface TimeMachine {
void goToTime(int hour, int minute, int seconds);
void goToDate(int day, int month, int year);
Long getCurrentTime();
Date getCurrentDate();
default ZonedDateTime getZonedDateTime(String zoneString) {
return new ZonedDateTime(getCurrentTime(), getZoneId(zoneString));
}
static Long getZoneId(String zoneString) {
// ...
return 10L;
}
}
27. ● Literal representation of a function
● As in other modern languages: input args -> function body
● Assignable to variables
● Being passed as method arguments and returned as return values
(High order functions)
● First class citizen
● Translated to anonymous classes for lower API versions
Lambdas
33. ● Custom functional interface for lambdas with 3 input arguments
@FunctionalInterface public interface Function3<A, B, C, R> {
R apply(A a, B b, C c);
}
Function3<Long, Integer, Boolean, Boolean> isEven = (l, i, b) -> l % 2 == 0;
● Use it
Function3<Long, Integer, Boolean, Boolean> isEven2 = (l, i, b) -> {
return l % 2 == 0;
};
40. ● Very powerful concept available in many languages
● You can extract the stream from any collection
● flatMap(), reduce(), count(), distinct(), forEach(), max(), min(), sorted(comparator) ...
Streams
users.stream()
.filter(user -> user.getAvatarUrl() != null)
.map(User::getAvatarUrl)
.findFirst()
.orElse("http://anyimage.com/fallback_avatar.png");
String avatar =
List<User> users = new ArrayList<>();
41. ● Jack & Jill
○ Jack documentation https://source.android.com/source/jack.html
○ More details http://android-developers.blogspot.com.es/2014/12/hello-world-meet-our-new-
experimental.html
○ Annotation Processing open issue https://code.google.com/p/android/issues/detail?id=204065
● Java 8 in Android N
○ Supported features with links to documentation http://developer.android.com/preview/j8-jack.html
○ Functional Interfaces out of the box https://docs.oracle.
com/javase/8/docs/api/java/util/function/package-summary.html
Bibliography