Is there a reasonably simple way for a module\'s build.gradle
file to indicate that certain files from a dependency should be excluded? I am specifically intere
EDIT:
Wrote advanced gradle task for you:
final List<String> exclusions = [];
Dependency.metaClass.exclude = { String[] currentExclusions ->
currentExclusions.each {
exclusions.add("${getGroup()}/${getName()}/${getVersion()}/${it}")
}
return thisObject
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile ('com.android.support:appcompat-v7:20.+')
debugCompile ('com.squareup.leakcanary:leakcanary-android:1.3.1')
.exclude("res/values-v21/values-v21.xml")
releaseCompile ('com.squareup.leakcanary:leakcanary-android-no-op:1.3.1')
}
tasks.create("excludeTask") << {
exclusions.each {
File file = file("${buildDir}/intermediates/exploded-aar/${it}")
println("Excluding file " + file)
if (file.exists()) {
file.delete();
}
}
}
tasks.whenTaskAdded({
if (it.name.matches(/^process.*Resources$/)) {
it.dependsOn excludeTask
}
})
Now you can use method .exclude()
on each dependency, providing into list of paths, you want to exclude from specified dependency.
Also, you can stack the .exclude()
method calls.
I believe you can solve this problem more elegantly using the PackagingOptions facility of the Android Gradle Plugin DSL.
I was able to use this myself to exclude some native libraries I didn't need introduced by an AAR in my project.
android {
...
packagingOptions {
exclude '/lib/armeabi-v7a/<file_to_exclude>'
}
}
For the case outlined in the question, I believe this would work:
android {
...
packagingOptions {
exclude '/res/values-v21/<file_to_exclude>'
}
}
Yes, you can use Proguard
buildTypes {
release {
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
debug {
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
Try compileOnly keyword to mark the resource is used for compile only.
dependencies {
compileOnly fileTree(include: ['*.jar'], dir: 'libs')
}