Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an Android Gradle build, how to exclude dependencies from an included jar file?

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?

like image 651
treesAreEverywhere Avatar asked Mar 19 '16 15:03

treesAreEverywhere


People also ask

How do I exclude in dependencies build Gradle?

Option 1) per-dependency exclude rules. When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com.

How do I exclude tasks in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

How do I clear all Gradle dependencies?

Clean Gradle cache or single dependency Gradle dependencies and metadata stores under your project . gradle/caches/ folder. on Windows, You can manually delete the . gradle/caches/ folder and do a fresh Gradle build and it downloads dependencies from scratch.

Where are Gradle dependencies jars?

Gradle caches artifacts in USER_HOME/. gradle folder. The compiled scripts are usually in the . gradle folder in your project folder.


1 Answers

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

like image 135
Maheshwar Ligade Avatar answered Sep 21 '22 13:09

Maheshwar Ligade