GuilhE
GuilhE

Reputation: 11891

Gradle dependency exclusion

I've a project A and a project B. Project A is a core project used by other projects too. Here's his build.gradle dependencies:

dependencies {
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile('org.simpleframework:simple-xml:2.7.1') {
        //http://stackoverflow.com/a/19455878/1423773
        exclude module: 'stax'
        exclude module: 'stax-api'
        exclude module: 'xpp3'
    }

    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'org.apache.httpcomponents:httpmime:4.5'
}

Since it's a core project having the latest dependencies versions makes sense. Project B uses a dependency that uses httpcore-4.2.4.jar and it will throw a duplicate dependency error if I don't modify my core project build.gradle to:

dependencies {
    ...    
//    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'org.apache.httpcomponents:httpcore:4.2.4'
    compile('org.apache.httpcomponents:httpmime:4.5') {
        exclude module: 'httpcore'
    }
}

And this is something I would like to avoid because I don't want to change my core project build.gradle.
What's the best solution for this situation? If I removed/exclude the .jar from my other dependency would work? If so, how can I remove it? I've tried exclude module/group but without any luck. Something I was thinking of was Chapter 56. Multi-project Builds (I haven't read yet) but I would like to avoid having different gradle.build files in my core project for every non-core project.

My project B conflict dependency: co.realtime:messaging-android:2.1.40

Thanks.

Upvotes: 2

Views: 3442

Answers (1)

GuilhE
GuilhE

Reputation: 11891

I found it, It was in front of me all the time...

So inside my project B I just have to follow the same approach I used in my core project:

compile (project(':submodules:MyCoreProject')){
        exclude module: 'httpcore'
        //co.realtime:messaging-android:2.1.40
        //uses 'org.apache.httpcomponents:httpcore:4.2.4'
}

and now my core project:

dependencies {
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile('org.simpleframework:simple-xml:2.7.1') {
        //http://stackoverflow.com/a/19455878/1423773
        exclude module: 'stax'
        exclude module: 'stax-api'
        exclude module: 'xpp3'
    }

    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'org.apache.httpcomponents:httpmime:4.5'
}

Upvotes: 2

Related Questions