[英]java.lang.NoClassDefFoundError: Failed resolution of: Lretrofit2/Retrofit$Builder;
I'm working on writing very simple Retrofit2 based Instrumented tests in Android (connectedTestAndroid task).我正在努力在 Android 中编写非常简单的基于 Retrofit2 的 Instrumented 测试(connectedTestAndroid 任务)。
My tests fail however due to the above short error message.然而,由于上述简短的错误消息,我的测试失败了。
Here's an example of the code I'm using:这是我正在使用的代码示例:
package com.example.app.android;
import okhttp3.ResponseBody;
import okhttp3.OkHttpClient;
import java.util.concurrent.TimeUnit;
import java.io.IOException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import android.util.Log;
public class Retrofit2Requests extends BasicRequest
{
private Call<ResponseBody> call;
private OkHttpClient _client;
private String _baseUrl;
private Retrofit2Adapter _adapter;
private static final String LOG_TAG = "RetroFit2Requests";
public Retrofit2Requests (String baseUrl) throws Exception
{
_client = new OkHttpClient
.Builder()
.connectTimeout(TIME_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(TIME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
.build();
_adapter = (Retrofit2Adapter) new Retrofit.Builder()
.client(_client)
.baseUrl(baseUrl)
.build()
.create(Retrofit2Adapter.class);
_baseUrl = baseUrl;
}
public void getResponse(String data) throws IOException
{
Call<ResponseBody> call = _adapter.getDelayResponse(50, data);
call.enqueue(getCallback());
}
private Callback getCallback()
{
return new Callback<ResponseBody>()
{
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
{
ResponseBody responseBody = (ResponseBody) response.body();
Log.d(LOG_TAG, "ResponseBody: " + responseBody.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t)
{
Log.e(LOG_TAG, "An exception occured fetchign response...", t);
}
};
}
}
The goal of this code is not to test any business logic or anything special in my app.此代码的目标不是测试任何业务逻辑或我的应用程序中的任何特殊内容。 All it is supposed to do is attempt a request and that's it.
它应该做的就是尝试一个请求,就是这样。
Here's also my Gradle dependencies.这也是我的 Gradle 依赖项。 Based on the output of
./gradlew :app:dependencies
also cleared me off any potential issues regarding different versions of retrofit in the same place.基于
./gradlew :app:dependencies
的输出,我也清除了关于同一地方不同版本改造的任何潜在问题。 The app does not use obfuscation or minification as it, again, is only supposed to do a network request.该应用程序不使用混淆或缩小,因为它再次只应该执行网络请求。
dependencies {
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.apache.commons:commons-lang3:3.7'
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.android.support:multidex:1.0.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'junit:junit:4.12'
}
If you're curious, yes, I'm doing the same thing with multiple network libraries.如果你很好奇,是的,我正在对多个网络库做同样的事情。 Their existence unless creating this builds NoClassDefFoundException should not matter.
它们的存在除非创建这个构建 NoClassDefFoundException 应该无关紧要。 Previously, I also explicitly depended on OkHttp directly but since retrofit2 depends on it I skipped that dependency, naturally.
以前,我也明确地直接依赖 OkHttp,但由于 Retrofit2 依赖于它,我自然而然地跳过了该依赖项。
Here's the defaultConfig for my android application:这是我的 android 应用程序的 defaultConfig:
defaultConfig {
applicationId 'com.example.app.android'
minSdkVersion 16
targetSdkVersion 24
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
dexOptions {
preDexLibraries true
javaMaxHeapSize '6g'
incremental true
}
}
I was able, by decompiling the debug APK, to see that retrofit2/Retrofit.class wasn't available.通过反编译调试APK,我能够看到retrofit2/Retrofit.class 不可用。 This is confusing, as from my understanding Dex'ing shouldn't affect it.
这令人困惑,因为根据我的理解,Dex'ing 不应该影响它。
Any ideas what could be the source of this issue and resolve in this context?任何想法可能是这个问题的根源并在这种情况下解决?
NoClassDefFoundError
error means no definition of the class could be found. NoClassDefFoundError
错误意味着找不到类的定义。 It is different from NoClassFoundException
.它与
NoClassFoundException
不同。
NoClassDefFoundError
: JVM can find the class but can't load it success.(something wrong when init the class) NoClassDefFoundError
: JVM 可以找到类,但无法成功加载它。(初始化类时出错)
NoClassFoundException
: JVM can not find the class in the classpath. NoClassFoundException
: JVM 在类路径中找不到类。
NoClassDefFoundError
always happens during class init. NoClassDefFoundError
总是在类初始化期间发生。 I suggest you clean the project and check the init process of Lretrofit2/Retrofit$Builder
我建议你清理项目并检查
Lretrofit2/Retrofit$Builder
的初始化过程
Hope this post can help you Java understand NoClassDefFoundError希望这篇文章可以帮助你了解 Java 的 NoClassDefFoundError
For my case, I just clean and rebuild project and this made me this issues solved.就我而言,我只是清理并重建项目,这让我解决了这个问题。 this exception arises when you change any class name but the compiler still keeps the previous class name in cache and in runtime compiler can not find previously stored class name as you changed the name later.
当您更改任何类名但编译器仍将先前的类名保留在缓存中并且在运行时编译器无法找到先前存储的类名时,会出现此异常,因为您稍后更改了名称。
Add this to your app`s build.gradle, inside android {...}:将此添加到您的应用程序的 build.gradle 中,在 android {...} 中:
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
Then clean and rebuild your project.然后清理并重建您的项目。 It worked for me.
它对我有用。
The issue was solved in Retrofit 2.8.1 just upgrade retrofit dependency to 2.8.1 if it's below that.这个问题在 Retrofit 2.8.1 中得到解决,如果它低于 2.8.1,只需将改造依赖升级到 2.8.1。
From retrofit changelog :从改造变更日志:
Version 2.8.1 (2020-03-25) Fix: Do not access MethodHandles.
版本 2.8.1 (2020-03-25) 修复:不访问 MethodHandles。 Lookup on Android API 24 and 25. The class is only available on Android API 26 and higher.
在 Android API 24 和 25 上查找。该类仅在 Android API 26 及更高版本上可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.