我的安卓应用程序使用Retroeft2.2.0和OkHttp3 3.7.0与我们的后端通信。效果很好。现在我需要使用另一家公司制作的外部库(不是公共图书馆)。这个库提供了一个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中。也许这两个库不向后兼容?
那么,如何使这个项目编译和工作呢?
非常感谢你的帮助。
安卓宣言
<?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'
}发布于 2017-04-23 18:37:37
与检查与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
}https://stackoverflow.com/questions/43572791
复制相似问题