32

In my Android project, I use a library that comes as a jar. I include it in the dependencies section like so:

dependencies {
    ...

    compile files('libs/thethirdpartylibrary.jar')
    ...
}

I also want to use the okhttp library, which I include like this:

compile ('com.squareup.okhttp:okhttp:2.7.5')

(This particular version of okhttp depends on okio 1.6.0.)

The problem is that the thirdparty jar library depends on okio v0.9.0 and what's worse, bundles it.

As a result, I get a dex conflict error at build time.

I was able to resolve this by manually removing okio from the jar file and this seems to work. But I'm wondering if there's a way to do this in gradle.

My question: Can I remove bundled, transitive ( <- I hope I'm using this word the right way) dependencies from an included jar during build-time with gradle?

4
  • 1
    Any reason why you can't simply recreate the third party jar to remove the classes in it that you don't want? I don't think gradle will allow you to select specific classes to exclude out of a file dependency. Mar 21, 2016 at 1:18
  • In fact, that's what I'm doing right now. I just was wondering if there's a (simple) way to do this with gradle, so that I don't have to do this manually every time the vendor library changes. Mar 21, 2016 at 12:46
  • 1
    Hi, have you resolved this? Can you share your solution? Jul 18, 2016 at 2:45
  • have you got the answer to this?
    – Sagar
    Mar 17, 2022 at 20:37

2 Answers 2

46

Exclude the Group in the dependencies by using the below lines.

1.

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

2.

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
    compile ("com.xxx:xxx-commons:1.+") {
        exclude group: 'junit', module: 'junit'
    }
}

3.

configurations {
    runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}

Try this one.

For more detail

6
  • 2
    2. this syntax does not work with file-based jar files (AFAIK...) Mar 19, 2016 at 17:08
  • 3. I don't think I want to do this since I actually do want to include one verson of the library -- I just don't want to use the one from the JAR Mar 19, 2016 at 17:09
  • There are three options to exclude the dependencies, Mar 19, 2016 at 17:20
  • 1
    It doesn't really answer the core question though, which is: How to exclude an dependency from a JAR. Mar 19, 2016 at 18:03
  • 2
    but it seems to remove it from everything, so that doesn't help me unfortunately ;-| Thanks for your efforts, btw! Mar 19, 2016 at 18:26
1

According to this discussion here https://groups.google.com/forum/#!topic/adt-dev/g1AiJM7PeVs, what you want to do is not possible.

The syntax suggested in the other answers is for "normal" Maven dependencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.