Home > OS >  How to use lambda expression in android?
How to use lambda expression in android?

Time:11-02

I have tried everything I could find from StackOverflow to make this problem right but none of them succeeded.

The strange thing is : I can use lambda such as

 activity.runOnUiThread(() -> {
                    activity.startActivity(intent);
                });

and stream like this

activity.getPackageManager().getInstalledPackages(0).stream().collect(Collectors.toList())

but this will fail :

activity.getPackageManager()
.getInstalledPackages(0)
.stream()
.filter(e -> e.packageName.contains("com"))
.collect(Collectors.toList())


my build.gradle file:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.mylearn.simplesender"
        minSdk 25
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.2'
    implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.78'
    implementation group: 'com.google.guava', name: 'guava', version: '31.0.1-android'
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
    testImplementation 'junit:junit:4. '
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

and also, here is my Gradle setting in Android Studio


enter image description here


here is my compile setting in Android Studio


enter image description here

It shows an unreadable error, and when I debug this expression in the watcher shows :

Compilation failed:
-source 1.6 中不支持 lambda 表达式
  (请使用 -source 8 或更高版本以启用 lambda 表达式)

which means I'm using source version 1.6, and it does not support lambda expression and told me to use source 8 or higher to enable lambda expression.

But I'm already in java 11, how could this happen?

CodePudding user response:

go to App level build.gradle>

then put this code in android {} block

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

after this your code look like this

android {
compileSdkVersion 31
buildToolsVersion "30.0.2"

defaultConfig {
    applicationId "in.chikuai.example"
    minSdkVersion 19
    targetSdkVersion 31
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

CodePudding user response:

Recently, I have the problem solved by change my build.gradle file according to the enter image description here

  • Related