NoClassDefFoundError when i use lambda to traverse String array
According to Android, if you want to use lambdas, you need to set your targe API to something lower than 23 (yours is currently set to 25) and then use the Jacktool chain.
Per the docs:
Android does not support all Java 8 language features. However, the following features are available when developing apps targeting Android 7.0 (API level 24):
- Default and static interface methods
- Lambda expressions (also available on API level 23 and lower)
- Repeatable annotations
- Method References (also available on API level 23 and lower)
- Type Annotations (also available on API level 23 and lower)
Note: Note: Type annotation information is available at compile time, but not at runtime. Also, the platform supports TYPE in API 24 and below, but not ElementType.TYPE_USE or ElementType.TYPE_PARAMETER..
To test lambda expressions, method references, and type annotations on earlier versions of Android, go to your build.gradle file, and set compileSdkVersion and targetSdkVersion to 23 or lower. You will still need to enable the Jack toolchain to use these Java 8 features.
forEach
method is available only in Android N, you can't use any API from Java 8 before SDK 24.
Even if you are using Retrolambda or Jack.
You need to use regular for loop.
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
forEach is default method , it’s only supported by java8.
Android does not support all Java 8 language features. However, the following features are available when developing apps targeting Android 7.0 (API level 24):
Default and static interface methods
Lambda expressions (also available on API level 23 and lower)
Repeatable annotations Method References (also available on API level 23 and lower)
Type Annotations (also available on API level 23 and lower)
Android support default and static interface methods , but it needs API level 24.More details here
defaultConfig {
applicationId "com.twsz.app.ivybox"
minSdkVersion 14 // Your minSdkVersion is less than 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
When you run your app in system less than 24, you will get that exception. so you’d better change another way . Traditional loop or Rxjava2.