1

I have the following dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-moshi:2.3.0'
    compile('com.github.eoinsha:JavaPhoenixChannels:0.2') {
        exclude module: 'groovy-all'
    }
}

And the following dependency tree:

+--- com.android.support:appcompat-v7:25.3.1
|    +--- com.android.support:support-annotations:25.3.1
|    +--- com.android.support:support-v4:25.3.1
|    |    +--- com.android.support:support-compat:25.3.1
|    |    |    \--- com.android.support:support-annotations:25.3.1
|    |    +--- com.android.support:support-media-compat:25.3.1
|    |    |    +--- com.android.support:support-annotations:25.3.1
|    |    |    \--- com.android.support:support-compat:25.3.1 (*)
|    |    +--- com.android.support:support-core-utils:25.3.1
|    |    |    +--- com.android.support:support-annotations:25.3.1
|    |    |    \--- com.android.support:support-compat:25.3.1 (*)
|    |    +--- com.android.support:support-core-ui:25.3.1
|    |    |    +--- com.android.support:support-annotations:25.3.1
|    |    |    \--- com.android.support:support-compat:25.3.1 (*)
|    |    \--- com.android.support:support-fragment:25.3.1
|    |         +--- com.android.support:support-compat:25.3.1 (*)
|    |         +--- com.android.support:support-media-compat:25.3.1 (*)
|    |         +--- com.android.support:support-core-ui:25.3.1 (*)
|    |         \--- com.android.support:support-core-utils:25.3.1 (*)
|    +--- com.android.support:support-vector-drawable:25.3.1
|    |    +--- com.android.support:support-annotations:25.3.1
|    |    \--- com.android.support:support-compat:25.3.1 (*)
|    \--- com.android.support:animated-vector-drawable:25.3.1
|         \--- com.android.support:support-vector-drawable:25.3.1 (*)
+--- com.squareup.retrofit2:retrofit:2.3.0
|    \--- com.squareup.okhttp3:okhttp:3.8.0
|         \--- com.squareup.okio:okio:1.13.0
+--- com.squareup.retrofit2:converter-moshi:2.3.0
|    +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
|    \--- com.squareup.moshi:moshi:1.4.0
|         \--- com.squareup.okio:okio:1.11.0 -> 1.13.0
\--- com.github.eoinsha:JavaPhoenixChannels:0.2
     +--- com.fasterxml.jackson.core:jackson-databind:2.8.3
     |    +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0
     |    \--- com.fasterxml.jackson.core:jackson-core:2.8.3
     \--- com.squareup.okhttp3:okhttp-ws:3.4.1
          \--- com.squareup.okhttp3:okhttp:3.4.1 -> 3.8.0 (*)

I get this error while trying to build the project:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lokhttp3/internal/ws/WebSocketReader$FrameCallback;

This is supposedly caused by the presence of two different versions of the okhttp library on the classpath:

  • 3.4.1 by com.squareup.okhttp3:okhttp-ws which is a dependency of JavaPhoenixChannels

  • 3.8.0 by retrofit

Gradle should be able to automatically resolve this, so I wonder what's the probelm. I tried to fix it by excluding okhttp and okhttp-ws from their respective first class dependencies and compile them separately but that doesn't seem to help.

Some explanation on why such errors would pop up is also appreciated.

Paghillect
  • 822
  • 1
  • 11
  • 30
  • did you add defaultConfig { multiDexEnabled true } in build.gradle file in app module ? then clean the project and try to build again. – Rohit Padma Jul 14 '17 at 21:01
  • @RohitPadma That seems irrelevant here. I'm not running into method limit issues. – Paghillect Jul 14 '17 at 21:07
  • https://stackoverflow.com/questions/20989317/multiple-dex-files-define-landroid-support-v4-accessibilityservice-accessibility/21100040 – Nouman Ghaffar Jul 14 '17 at 21:12
  • https://stackoverflow.com/questions/22851103/android-studio-gradle-error-multiple-dex-files-define – Nouman Ghaffar Jul 14 '17 at 21:13
  • 3
    Starting from okhttp 3.5 okhttp-ws is part of okhttp itself... Take a source of this library and compile it by yourself... You may also try to exclude okhttp-ws, but there is a chance that the version in okhttp 3.8.0 has no compatible interface – Selvin Jul 14 '17 at 21:23
  • @NoumanGhaffar Already seen those question but didn't solve my problem. – Paghillect Jul 14 '17 at 21:25
  • @Selvin Thanks! solved the issue. You could consider writing this as an answer for future reference. – Paghillect Jul 14 '17 at 21:35

1 Answers1

1

As pointed out by @Selvin in the comments, the issue is caused by the fact that okhtt-ws has been moved inside the core project since version 3.5 and hence specifying both a newer version of the okhttp and an old version of okhttp-ws would result in two different copies of the okhttp-ws library ending up on the classpath. To gradle, these would be different libraries using the same package hierarchy and hence it won't exclude one automatically.

TL;DR the error is fixed by excluding the okhttp-ws dependency as it is already available in version 3.8 of the okhttp itself:

compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile('com.github.eoinsha:JavaPhoenixChannels:0.2') {
    exclude module: 'okhttp-ws'
}
Paghillect
  • 822
  • 1
  • 11
  • 30