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

Retrofit请求触发NoSuchMethodError崩溃问题求助

Retrofit调用崩溃问题解决方案

问题概述

集成Retrofit进行服务端调用时,触发java.lang.NoSuchMethodError异常导致应用崩溃,API在Postman及其他应用中可正常运行,调用enqueue时直接崩溃,无法进入onResponse回调。

异常栈信息

FATAL EXCEPTION: OkHttp Dispatcher

Process: com.eclerx_services.android.fs_security, PID: 9495

java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.art/javalib/core-oj.jar)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.onResponse(DefaultCallAdapterFactory.java:82)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:140)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)

API调用代码

public LiveData<ClientValidationModel> getEmployeeCompanyExistance(Map<String, Object> params) {
    final MutableLiveData<ClientValidationModel> data = new MutableLiveData<>();
    if (ConnectionManager.Connection(context)) {
        clientValidationModelCall = apiRequest.getEmployeeCompanyExistance(params);
        clientValidationModelCall.enqueue(new Callback<ClientValidationModel>() {
            @Override
            public void onResponse(Call<ClientValidationModel> call, Response<ClientValidationModel> response) {
                if (!clientValidationModelCall.isCanceled()) {
                    ClientValidationModel clientValidationModel = response.body();
                    if (clientValidationModel != null) {
                        data.setValue(clientValidationModel);
                    } else {
                        Toast.makeText(context, context.getResources().getString(R.string.error_something_went_wrong), Toast.LENGTH_SHORT).show();
                        data.setValue(null);
                    }
                }
            }
            @Override
            public void onFailure(Call<ClientValidationModel> call, Throwable t) {
                if (!clientValidationModelCall.isCanceled()) {
                    Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
                    data.setValue(null);
                }
            }
        });
    } else {
        Toast.makeText(context, context.getResources().getString(R.string.error_something_went_wrong), Toast.LENGTH_SHORT).show();
        data.setValue(null);
    }
    return data;
}

依赖配置

// retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.3.1'

解决方案

1. 统一OkHttp依赖版本

Retrofit 2.9.0默认依赖OkHttp 4.9.0,你当前使用的logging-interceptor版本为4.3.1,版本不匹配会导致类加载冲突。统一所有OkHttp相关依赖的版本:

// 统一OkHttp版本,确保与Retrofit兼容
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

2. 启用Core Library Desugaring(针对API 26以下设备)

Android API 26及以下系统缺少JDK 8 Lambda表达式所需的LambdaMetafactory相关方法,需启用Desugaring来兼容:
在App模块的build.gradle中添加配置:

android {
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
    // 其他依赖...
}

3. 降级Retrofit版本(备选方案)

如果上述方案无效,可尝试将Retrofit降级到2.8.0版本,该版本对低版本Android的兼容性更稳定:

implementation 'com.squareup.retrofit2:retrofit:2.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.8.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.3.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

问题原因

该异常本质是JDK 8 Lambda表达式依赖的核心方法在低版本Android系统中缺失,或Retrofit与OkHttp依赖版本不匹配导致类加载冲突,Retrofit内部在处理回调时使用了Lambda表达式,触发了缺失的LambdaMetafactory.metafactory方法调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 03:39:59