1

My Android app uses Retrofit 2.2.0 with OkHttp3 3.7.0 to communicate with our backend. It works very well. Now I need to use an external library made by another company (is not a public library). This library is provided with an AAR and includes OkHttp 3.3.1. I'm using Gradle to build the project.

As soon I add the library to my project everything stop working and the app crashes at startup. The error is:

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)

To my understanding that method should exists in OkHttp 3.3.1. Maybe the two libraries are not backward compatible?

So, how can I make this project compile and works?

Thanks a lot for your help.

Android Manifest

<?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>

App Gradle Build

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'
}
Nicholas
  • 471
  • 3
  • 13

1 Answers1

2

Instead of checking the backward compatibility with OkHttp 3 3.1, you can exclude it from your ExternalSDK_Android dependency.

// External SDK causing problems
compile(project(':ExternalSDK_Android')) {
    exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}

In this case, you can compile with your own version of OkHttp (3.7.0).

Instead, if you want to use the OkHttp version that is compatible with your dependency ExternalSDK_Android, you have to remove the dependency:

compile('com.squareup.okhttp3:okhttp:+') {
    force = true
    transitive = true
}
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70