Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use Java 8 functional interfaces on Android API below 24?

I can use retrolambda to enable lambdas with Android API level <24. So this works

myButton.setOnClickListener(view -> Timber.d("Lambdas work!")); 

This also works

Runnable runLater = () -> Timber.d("Lambdas work!"); runLater.run(); 

But this one does not

Consumer<Integer> runLaterWithInt = (Integer i) -> Timber.d("i = " + i); runLaterWithInt.accept(3); 

The last one works on Android API Level 24, but on other devices this code causes a crash

java.lang.NoClassDefFoundError: com.retrolambdatry.MainActivity$$Lambda$1 

Instead of using retrolambda I tried to enable Java 8. First two code examples still work, although butterknife stopped working. https://developer.android.com/preview/j8-jack.html#configuration here ava.util.function is said to be supported, but I still get a crash when running the third one, this time it is a little different

java.lang.NoClassDefFoundError: com.retrolambdatry.MainActivity$-void_onCreate_android_os_Bundle_savedInstanceState_LambdaImpl1 
like image 967
sasha199568 Avatar asked Jul 27 '16 08:07

sasha199568


People also ask

Can I use Java 8 for Android?

Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still it doesn't have invokedynamic, only the new syntax sugar. If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba.

How many methods should be there in functional interface in Java 8?

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface.

Does Java 8 have functional interface before?

We have been using functional interfaces even before the introduction of Java 8. We used them by creating anonymous inner classes using functional interfaces. The functional interfaces such as Runnable, ActionListener, Comparator, etc. have a single abstract method.

What are the functional interfaces available in Java 8?

It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces. It is a new feature in Java, which helps to achieve functional programming approach.


1 Answers

Not sure if you still need an answer to this question, but others (like myself) might.

As version 3.0, Android Studio natively supports lambda functions and many other Java 8 functions on all API levels, but some (like Functional Interfaces and java.util.function) are still restricted to APIs 24+.

Until that support is expanded, android-retrostreams provides backport support for most of it. This project is an 'upgraded port' of the streamsupport library, which you can also use and has many of the functionalities in android-retrostreams. The streamsupport library supports down to Java 6/7, so you can use it even if you don't have AS 3.0+ or aren't targeting Java 8, but you're probably better off using android-retrostreams in most cases, if you can. You can go through the project's javadocs to see exactly what's offered, but highlights that I've used are java.util.function and java.util.Comparator.

Note that java in the package names is replaced with java9, and some of the class and/or method names may have been changed slightly. For example:

java.util.function becomes java9.util.function,

while

java.util.Comparator becomes java9.util.Comparators (and with slightly different method names and call patterns - but the same functionality).

like image 63
VerumCH Avatar answered Sep 24 '22 15:09

VerumCH