Excluding Jars Via Gradle In Unit Tests
Solution 1:
By default the testImplementation configuration extends from the implementation one so every dependency added to implementation will be present in testImplementation.
So the best option is to declare these specific dependencies into a different configuration, let's call it extraDeps, which you then add to the compileClasspath configuration:
configurations {
extraDepscompileClasspath.extendsFrom(extraDeps)
}
dependencies {
extraDepsfileTree(dir: 'libs', include: ['*.jar'])
}
This gives you the following advantages:
- Shared dependencies between compile and test can still be in
implementation - Special dependencies are identified clearly as they are in their own configuration
- Compile classpath sees all it needs
- Test classpath does not see the special jars
Solution 2:
In my case I needed to include aar for implementation and replace it with jar for unit tests. Gradle can't exclude jar files, so I found different solution.
Let's say I have Android project in folder MyProject. So there must be files MyProject/build.gradle and MyProject/app/build.gradle.
I put <my-dependency>.aar file and <my-test-dependency>.jar files to MyProject/app/libs directory. Then I add this directory as local repository in MyProject/build.gradle file:
allprojects {
repositories {
...
flatDir {
dirs'libs'
}
}
}
Now I can include and exclude my aar by name:
configurations.testImplementation {
exclude module: '<my-dependency>'
}
dependencies {
implementation(name: '<my-dependency>', ext:'aar')
testImplementation(name: '<my-test-dependency>', ext:'jar')
// fileTree also should work, i.e.:
// testImplementation fileTree(dir: 'libs', include: ['*.jar'])
}
Post a Comment for "Excluding Jars Via Gradle In Unit Tests"