3

in my Android project I use

compile 'com.squareup.okhttp:okhttp:2.2.0'

I need okhttp in version 2.2.0 for my code to work properly. But I have a problem when I add

compile('io.intercom.android:intercom-sdk:1.1.2@aar') {
        transitive = true
}

Because inside intercom-sdk there is okhttp dependency again for later version:

compile 'com.squareup.okhttp:okhttp:2.4.0'

Which results that my code uses that later version 2.4.0 instead of 2.2.0 I want. Is there please any way how in my module I can use 2.2.0 which I specified and let intercom to use its 2.4.0?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
bakua
  • 13,704
  • 7
  • 43
  • 62

2 Answers2

4

You can use something like this:

compile('io.intercom.android:intercom-sdk:1.1.2@aar') {
    exclude group: 'com.squareup.okhttp', module: 'okhttp'
  }

However pay attention. If the library uses methods that are not present in the 2.2.0 release, it will fail.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks. That is what I was worried about, because I already tried that and as you said it fails. Could you please explain what would happen if I remove transitive? – bakua Jul 14 '15 at 10:38
  • As I know, when you add the @aar annotation, to have dependence transitive ( (i.e., dependencies of dependencies), you should add "transitive=true". – Gabriele Mariotti Jul 14 '15 at 11:30
  • Flagging that nowadays you need to use group id: `com.squareup.okhttp3` – Tobrun Jul 18 '18 at 10:30
3

You should define a resolution strategy to set a specific version. This will guarantee you will get the correct version you wish no matter what the transitive dependency versions are:

allProjects {
   configurations.all {
       resolutionStrategy {
           eachDependency { DependencyResolveDetails details ->
               if (details.requested.name == 'okhttp') {
                   details.useTarget('com.squareup.okhttp:okhttp:2.2.0')
               }
            }
        }
     }
  }

In newer versions of Gradle you can use:

allProjects {
   configurations.all {
       resolutionStrategy.force 'com.squareup.okhttp:okhttp:2.2.0'
     }
 }
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • Thank you. But won't that force my intercom dependency to use 2.2.0 as well? Or will intercom continue using 2.4.0? – bakua Jul 15 '15 at 05:19
  • You can only have one version of the jar in a dependency configuration. If you want two or more version, you will need to put them in a separate dependency, combine the dependencies for your output. You will also need to make sure the different libs are loaded in a unique classloade at run time. – cmcginty Jul 21 '15 at 19:45