You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio调试评估窗口Lambda表达式失效问题求助

问题描述

调试Android应用时,无论在何处设置断点并打开评估窗口,Lambda表达式均无法正常工作(应用内运行无任何问题)。

示例代码:

Arrays.asList("1One","1Two","1Three","1Four","2Five").stream()
        .filter(word -> word.charAt(0) == '1')
        .collect(Collectors.toList());

该代码在应用中运行正常,但在评估窗口中抛出如下编译错误:

Compilation failed:
cannot find symbol
  symbol:   method metafactory(java.lang.invoke.MethodHandles.Lookup,java.lang.String,java.lang.invoke.MethodType,java.lang.invoke.MethodType,java.lang.invoke.MethodHandle,java.lang.invoke.MethodType)
  location: interface java.lang.invoke.LambdaMetafactory

已将Android Studio更新至最新版本,问题仍未解决。项目最初使用JDK 1.8,后因添加Kotlin或升级Gradle改为JDK 11,项目配置如下:

项目级build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.gradleVersion = '7.2.2'
    ext.kotlinVersion = '1.7.10'
    ext.rxJavaVersion = '3.0.3'

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$gradleVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块级build.gradle(app)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.myschedule"
        minSdk 27
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    sourceSets {
        main {
            assets {
                srcDirs 'src\\main\\assets'
            }
        }
    }
}

dependencies {

    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.work:work-runtime:2.7.1'
    implementation 'androidx.work:work-multiprocess:2.7.1'
    implementation 'androidx.concurrent:concurrent-futures:1.1.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "io.reactivex.rxjava3:rxjava:$rxJavaVersion"
    implementation 'androidx.preference:preference:1.2.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
解决方案

这个问题源于调试器评估窗口使用的JDK版本与项目编译目标版本不匹配,Lambda元工厂方法在不同JDK版本中存在实现差异,可通过以下步骤修复:

  • 统一调试器JDK版本
    打开Android Studio设置(File > Settings),进入Build, Execution, Deployment > Debugger > Java,将Debugger JDK选择为JDK 1.8,应用设置后重启IDE。

  • 确认项目JDK配置一致性
    进入File > Project Structure > Project,确保Project SDK和Project language level均设置为1.8,保持与模块级配置的compileOptions、kotlinOptions版本一致。

  • 清理重建项目
    执行Build > Clean Project,再执行Build > Rebuild Project,重启调试会话后重新测试评估窗口的Lambda表达式。

说明

项目升级到JDK 11但编译目标仍保留1.8时,调试器评估窗口可能会用JDK 11环境编译Lambda,而Android运行时基于JDK 1.8的Lambda元工厂实现,导致方法找不到的冲突,统一调试器JDK版本即可解决该问题。

内容的提问来源于stack exchange,提问作者Kristian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:45:10