带有使用 OkHttp3 3.3.1 的外部 AAR 的 Retrofit2 NoSuchMethodError
Retrofit2 NoSuchMethodError with external AAR that uses OkHttp3 3.3.1
我的 Android 应用程序使用 Retrofit 2.2.0 和 OkHttp3 3.7.0 与我们的后端通信。它工作得很好。现在我需要使用另一家公司制作的外部库(不是 public 库)。该库随 AAR 一起提供,并包含 OkHttp 3.3.1。我正在使用 Gradle 构建项目。
一旦我将库添加到我的项目中,一切都停止工作并且应用程序在启动时崩溃。错误是:
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>
应用程序Gradle构建
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) 版本进行编译。
相反,如果您想使用与您的依赖项 ExternalSDK_Android
兼容的 OkHttp
版本,您必须删除依赖项:
compile('com.squareup.okhttp3:okhttp:+') {
force = true
transitive = true
}
我的 Android 应用程序使用 Retrofit 2.2.0 和 OkHttp3 3.7.0 与我们的后端通信。它工作得很好。现在我需要使用另一家公司制作的外部库(不是 public 库)。该库随 AAR 一起提供,并包含 OkHttp 3.3.1。我正在使用 Gradle 构建项目。
一旦我将库添加到我的项目中,一切都停止工作并且应用程序在启动时崩溃。错误是:
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>
应用程序Gradle构建
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) 版本进行编译。
相反,如果您想使用与您的依赖项 ExternalSDK_Android
兼容的 OkHttp
版本,您必须删除依赖项:
compile('com.squareup.okhttp3:okhttp:+') {
force = true
transitive = true
}