skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    Recently, I have the problem solved by change my build.gradle file according to the official doc. now my build.gradle file is :

    plugins {
        id 'com.android.application'
    }
    
    android {
        compileSdk 31
    
        defaultConfig {
            applicationId "com.infiai.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 {
            coreLibraryDesugaringEnabled true
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    dependencies {
        coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
        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'
    }
    

    I can now using both stream and lambda together, but I didn't figure out how to use this in debug mode when the expression is in watches window, it will still throw exception even the same expression in my code was running succeed.

    enter image description here


  2. 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
    }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search