Using Java 8 features in Android
Retrolambda
https://github.com/evant/gradle-
Gradle Retrolambda Plugin
https://github.com/orfjackal/retrolambda
StreamSupport Library
http://sourceforge.net/projects/streamsupport/
Slide 5
Slide 5 text
Lambdas
&
Streams
inside
Android
Slide 6
Slide 6 text
What are lambdas?
Slide 7
Slide 7 text
No content
Slide 8
Slide 8 text
Classic Java 7 Interface Implementation
ErrorDialog.build().onOk(new DialogListener()
{
@Override
public void onPress(Button b)
{
router.displayScene(b,TopicsScene.class);
}
}).onCancel(new DialogListener()
{
@Override
public void onPress(Button b)
{
router.displayScene(b,TopicsScene.class);
}
});
Slide 9
Slide 9 text
Classy Java 8 Lambda implementation
ErrorDialog.build()
.onOK( b -> router.displayScene(b,TopicsScene.class))
.onCancel( b -> router.displayScene(b,TopicsScene.class));
Slide 10
Slide 10 text
Java 7 with Observables
reloadAndStore(key).subscribe(new Action1()
{
@Override
public void call(V v)
{
log.debug("Reloaded item with key={}", key);
}
}, new Action1()
{
@Override
public void call(Throwable e)
{
log.error("Error reloading key={} to store={}", key,
DataStore.this.getClass().getName(), e);
}
});
Slide 11
Slide 11 text
Java 8 with Observables
reloadAndStore(key)
.subscribe(v -> log.debug("Reloaded item with key={}", key),
e -> log.error("Error reloading key={} to store={}",
key,
DataStore.this.getClass().getName(), e));
Slide 12
Slide 12 text
Lambdas implement the interface behind the scene
reloadAndStore(key)
.subscribe(v -> log.debug …
reloadAndStore(key).subscribe(new Action1()
{
@Override
public void call(V v)
{
log.debug("Reloaded item with key={}", key);
}
}
Slide 13
Slide 13 text
Java 8 Streams
Slide 14
Slide 14 text
Using Java 8 Streams
import java8.util.stream.StreamSupport;
public class MyClass {
public void tryMe() {
List aList = new ArrayList<>();
aList.add(new Random().nextInt());
aList.add(new Random().nextInt());
aList.add(new Random().nextInt());
StreamSupport.stream(aList)
.map(el -> el.hashCode())
.forEach(el-> Log.d("SERVICE", String.valueOf(el)));
}
}
Using Groovy in Android
Groovy Android Gradle Plugin
https://github.com/groovy/groovy-android-gradle-
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
Groovy in Android - Why?
• Pure functional language, perfect for writing
compact code in logical workflows.
• Hides even more boilerplate code then Java 8.
• Toolbox with built in support for lambdas, streams,
json-parsing and much much more ..
Slide 21
Slide 21 text
Groovy in Android - Why?
• You can mix both Java and Groovy
classes in the code tree (live side by side).
• Java can access Groovy classes and vice
versa.
• You already using it. Gradle is written in
Groovy
Slide 22
Slide 22 text
Classic Java 7 Interface Implementation
ErrorDialog.build().onOk(new DialogListener()
{
@Override
public void onPress(Button b)
{
router.displayScene(b,TopicsScene.class);
}
}).onCancel(new DialogListener()
{
@Override
public void onPress(Button b)
{
router.displayScene(b,TopicsScene.class);
}
});
Slide 23
Slide 23 text
A Groovy Interface Implementation
ErrorDialog.build()
.onOK({ b -> router.displayScene(b,TopicsScene.class)})
.onCancel({ b -> router.displayScene(b,TopicsScene.class)});