Retrofit2 NoSuchMethodError与使用OkHttp3的外部AAR 3.3.1
问题描述:
我的Android应用程序使用Retrofit 2.2.0和OkHttp3 3.7.0与我们的后端进行通信。它工作得很好。现在我需要使用另一家公司制作的外部库(不是公共图书馆)。这个库提供了一个AAR,并包含OkHttp 3.3.1。我正在使用Gradle构建项目。Retrofit2 NoSuchMethodError与使用OkHttp3的外部AAR 3.3.1
只要我将库添加到我的项目中,一切都停止工作,应用程序在启动时崩溃。错误是:
java.lang.NoSuchMethodError: No static method
parse(Ljava/lang/String;)Lokhttp3/HttpUrl; in class Lokhttp3/HttpUrl;
or its super classes (declaration of 'okhttp3.HttpUrl' appears in
/data/app/mycompany.myapp-
1/split_lib_dependencies_apk.apk:classes8.dex)
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:450)
at mycompany.myapp.rest.APIManager.<init>(APIManager.java:82)
...
at mycompany.myapp.Login.onCreate(Login.java:46)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我的理解,该方法应该存在OkHttp 3.3.1。也许这两个库不向后兼容?
那么,我怎样才能使这个项目编译和工作?
非常感谢您的帮助。
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mycompany.myapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Login" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginOk" />
</application>
</manifest>
应用摇篮构建
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "mycompany.myapp"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.code.gson:gson:2.7'
compile('com.squareup.okhttp3:okhttp:+') {
force = true
transitive = true
}
compile('com.squareup.retrofit2:converter-gson:+') {
force = true
transitive = true
}
compile('com.squareup.retrofit2:retrofit:+') {
force = true
transitive = true
}
// External SDK causing problems
compile(project(':ExternalSDK_Android')) {
transitive = false
}
testCompile 'junit:junit:4.12'
}
答
而不是检查与OkHttp
3 3.1的向后兼容的,你可以从你ExternalSDK_Android
依赖排除。
// External SDK causing problems
compile(project(':ExternalSDK_Android')) {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
在这种情况下,你可以用你自己的版本的OkHttp
(3.7.0)编译。
相反,如果你想使用OkHttp
版本与你的依赖ExternalSDK_Android
兼容,你需要删除的依赖:
compile('com.squareup.okhttp3:okhttp:+') {
force = true
transitive = true
}
感谢您的建议。我尝试都没有成功。总是得到相同的错误。 – Nicholas