有没有办法在24以下的Android API上使用Java 8功能接口?
我可以使用retrolambda来启用Android API级别所以这个工作
I can use retrolambda to enable lambdas with Android API level <24. So this works
myButton.setOnClickListener(view -> Timber.d("Lambdas work!"));
这也有效
Runnable runLater = () -> Timber.d("Lambdas work!");
runLater.run();
但这个不是
Consumer<Integer> runLaterWithInt = (Integer i) -> Timber.d("i = " + i);
runLaterWithInt.accept(3);
最后一个适用于Android API Level 24,但在其他设备上此代码会导致崩溃
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
我试图启用Java 8.而不是使用retrolambda。前两个代码示例仍然有效,尽管butterknife停止工作。 https://developer.android.com/preview/j8-jack.html#configuration 这里 ava.util.function
据说是支持的,但是在运行第三个时我仍然会崩溃,这次它有点不同
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
不确定你是否还需要这个问题的答案,但其他人(比如我自己)可能。
Not sure if you still need an answer to this question, but others (like myself) might.
作为3.0版,Android Studio 原生支持lambda函数和所有API级别上的许多其他Java 8函数,但有些(如功能接口和 java.util.function
)仍然仅限于API 24+。
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+.
直到支持扩大ded, android-retrostreams 为大多数提供了后端支持。该项目是 streamsupport库的升级端口,您也可以使用它,并且有许多android-retrostreams中的功能。 streamsupport库支持Java 6/7,所以你可以使用它,即使你没有AS 3.0+或者没有针对Java 8,但是在大多数情况下你最好使用android-retrostreams,如果您可以。您可以浏览项目的javadoc以查看所提供的内容,但我使用的重点是 java.util.function
和 java.util。比较器
。
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
.
请注意,包名称中的 java
将替换为 java9
,并且某些类和/或方法名称可能略有改变。例如:
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
变为 java9.util.function
,
while
java.util.Comparator
变为 java9.util.Comparators
(并且方法名称和调用模式略有不同 - 但功能相同)。
java.util.Comparator
becomes java9.util.Comparators
(and with slightly different method names and call patterns - but the same functionality).