Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude module from all dependencies but leave explicit declaration Gradle

I am trying to solve the problem,

I have some dependencies declared in build.gradle file for my android app, but the problem is that a lot of these dependencies use the same compat library, in my case appcompat-v7.
It is possible to exclude this library for each dependency

compile ('com.github......'){
    exclude group: 'com.android.support', module: 'appcompat-v7'
}

But I need to do this in for each dependency
Another way is to use such expression

configurations {
    compile.exclude module: 'appcompat-v7'
}

This works, but even If declare this library explicitly it is ignored compile 'com.android.support:appcompat-v7:+'

All what I need it is to include this library only once for the whole app, because if compile without exclude it will show a lot of errors like has been already defined.
Maybe there is an easier way to get this working. I would be grateful for any help, thanks.

like image 204
CROSP Avatar asked Oct 18 '15 11:10

CROSP


People also ask

How do you override a dependency in build Gradle?

To override the version of a transitive dependency in Gradle, exclude it from the declared dependency that pulls it in, and then explicitly declare the version that you prefer to use in your build.

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.


1 Answers

We use a provided configuration in gradle (so that when we gradle:eclipse, the packages are included, but are not included when compiled into a jar, as these jars are expected to be provided at runtime). This configuration looks like the following:

configurations {
  provided {
    dependencies.all {dep ->
       configurations.default.exclude group: dep.group, module:dep.name
    }
  }
  compile.extendsFrom provided
}

This allows us to include dependencies as follows:

dependencies {
    compile("org.scala-lang:scala-library:2.11.7")
    compile("org.scala-lang:scala-compiler:2.11.7")
    provided("org.apache.spark:spark-core_2_11:2.0.0")
}

Try creating a configuration which contains all dependencies where you want to exclude appcompat-v7, and then extend compile from this new configuration.

like image 194
Nick Avatar answered Oct 04 '22 16:10

Nick