bakua
bakua

Reputation: 14454

Don't use later library version from transitive dependency in Gradle

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?

Upvotes: 3

Views: 3088

Answers (2)

cmcginty
cmcginty

Reputation: 117018

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'
     }
 }

Upvotes: 3

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363825

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.

Upvotes: 4

Related Questions