Cyrus
Cyrus

Reputation: 9425

NoClassDefFoundError when i use lambda to traverse String array

I got follow error when I use lambda to traverse String array.

java.lang.NoClassDefFoundError: com.twsz.app.ivybox.alarm.CreateOrUpdateAlarmActivity$$Lambda$1
at com.twsz.app.ivybox.alarm.CreateOrUpdateAlarmActivity.initView(CreateOrUpdateAlarmActivity.java:143)
at com.twsz.app.ivybox.alarm.CreateOrUpdateAlarmActivity.onCreate(CreateOrUpdateAlarmActivity.java:73)

This is my code.I know it's ok for traditional way to traverse the String array ,but why this happen when I use lambda.

    String[] days = dayOfWeek.split(",");
    Arrays.asList(days).forEach(day->{
        int index = Integer.valueOf(day) -1;
        checkBoxList.get(index).setChecked(true);
    });//where happens NoClassDefFoundError

My build.gradle file

   android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.twsz.app.ivybox"
            minSdkVersion 14
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        dataBinding {
            enabled = true
        }
    }

Thanks for any help.

Upvotes: 16

Views: 4631

Answers (5)

Mr.ccc
Mr.ccc

Reputation: 349

     *
     * @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.

Upvotes: 15

BlackHatSamurai
BlackHatSamurai

Reputation: 23513

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.

Upvotes: 8

Dimezis
Dimezis

Reputation: 1595

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.

Upvotes: 9

Park
Park

Reputation: 411

Source: https://developer.android.com/guide/platform/j8-jack.html

If you want using lambda in android, you add to gradle option like this enter image description here

Upvotes: 2

Sarvex
Sarvex

Reputation: 785

For using Java 8 Features, you need to enable Jack Toolchain

  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }

See documentation for more information

Upvotes: 3

Related Questions