How To Exclude Specific Resources from an AAR Depedency?

gradle exclude multiple dependencies
gradle exclude dependency from project
gradle exclude class from dependency
gradle exclude specific version
android aar exclude dependency
gradle exclude nested dependency
could not find method exclude() for arguments
how to remove unused dependencies in android studio

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 interested in excluding certain resources from an AAR.

LeakCanary is an interesting library for helping to track down memory leaks. However, it has an undocumented requirement of compileSdkVersion of 21 or higher. While most projects should not have a problem with this, it's unseemly for a library to require a certain compileSdkVersion without a good reason. A development team may have frozen their compileSdkVersion as part of a general policy to only change those sorts of settings as part of major version updates of their app or something.

In this case, for v1.3.1 of LeakCanary at least, the only reason compileSdkVersion is required, AFAICT, is because the AAR has a res/values-v21/ directory, containing a theme definition that inherits from Theme.Material. This theme is used by a diagnostic activity. That activity is never seen by end users, only by developers in debug builds. Frankly, what that activity looks like, theme-wise, does not really matter. Forcing a compileSdkVersion of 21 just to have that diagnostic activity have a certain theme is, IMHO, stupid.

It'd be nice if as part of a compile directive we could say "hey, please skip res/values-v21/ from this AAR, m'kay?". Since the -v21 theme is simply providing an alternative definition of a theme defined elsewhere, dropping the -v21 theme will not break the build or break things at runtime, but merely will give us a Holo-themed diagnostic activity.

I fail to see how this answer works with dependencies. I am also uncertain if it is complete, and it certainly does not appear to be supported. It also doesn't really qualify as "simple" — I would not expect somebody to try dropping this in a build.gradle file just to block a single file from a diagnostic library like LeakCanary.

So, is there something simpler than this that works with now-current editions of the Android Plugin for Gradle?


Wrote advanced gradle task for you:

final List<String> exclusions = [];

Dependency.metaClass.exclude = { String[] currentExclusions ->
    currentExclusions.each {
    return thisObject

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ('')
    debugCompile ('com.squareup.leakcanary:leakcanary-android:1.3.1')
    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()) {

    if (^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.

How to exclude libs provided inside of aar dependency?, I have a library, which has libs/android-support-v4.jar inside of its aar, as one of my How can I exclude such bundled library from dependency aar? Standard particular are available as first-class artifacts in their own right. Questions: 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 interested in excluding certain resources from an AAR.

Try compileOnly keyword to mark the resource is used for compile only.

dependencies {
      compileOnly fileTree(include: ['*.jar'], dir: 'libs')

How to exclude dependencies of a particular dependency in Gradle, As you might know, by running $ ./gradlew dependencies gradle will print out the list of all libraries you are having at your build. I've checked lots of posts on stackoverflow, none of them worked so far. So I have an APK that depends on a bunch of AARs, one of the AAR has 10 audio files that I do not need. Since I can't remove the dependency directly because I need some APIs in it, I want to make sure those audio fils don't get built into the APK.

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>'

Add build dependencies, I am trying to create an AAR file for a library Android project using Android Studio and gradle. I want to exclude from this archive specific folders and files but I cannot 'assets' exclude '**/comments.txt' } sourceSets { flavour1 { resources dependencies { compile '' }. Now we will see how to exclude the transitive dependency for any JAR. Exclude Transitive Dependency by Configuration Excluding Transitive Dependency by Configuration is preferable way. Using configuration we can exclude transitive dependency for module and group. Find the separate example of module and group. First find the example which will use module dependency as below.

Yes, you can use Proguard

buildTypes {
    release {
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
debug {
        proguardFiles getDefaultProguardFile('proguard-android.txt'),

Exclude package/classes from #aar, gradle dependency, I am getting httpclient-android- file with one of the aar file that I am adding in my dependencies. I want to exclude this jar from this dependency,  23. You can utilize the dependency management mechanism. If you create entries in the <dependencyManagement> section of your pom for spring-security-web and spring-web with the desired 3.1.0 version set the managed version of the artifact will override those specified in the transitive dependency tree.

Reduce duplicated dependencies from your project, try to remove unused classes and resources to get squeezed into limit. - or you can manage your dependencies, remove dependencies and get new compile (​'com.mapbox.mapboxsdk:mapbox-android-sdk:4.2.0@aar') { transitive = true exclude group: '', module: 'appcompat-v7'  Re: Axis2-aar maven plugin : How to exclude a particular dependency from a AAR file Basically you have to make sure that your maven class path only has the needed set of dependencies. Try a mvn dependency:tree and exclude what is not needed.

How to exclude a file from an .AAR Android library archive with gradle, libraryVariants.all{ variant -> project.task("filter${}Resources", type: Copy) { filterTask -> filterVariant "${}", filterTask filterTask. To address this, Maven allows you to exclude specific dependencies. Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared .

DexProtector User Manual, Our solution allows protecting the content of APK files and AAR The set of classes/resources that should be protected can be specified with the help And we would also recommend to exclude all the open source libraries. buildscript { dependencies { classpath 'com.licel:dexprotector-gradle-plugin:' +  Exclude transitive dependencies. As an app grows in scope, it can contain a number of dependencies including direct dependencies and transitive dependencies (libraries which your app's imported libraries depend on). To exclude transitive dependencies that you no longer need, you can use the exclude keyword as given below:

  • I think, the workaround for LeakCanary ( would be to fork it and compile your own version with proper compileSdkVersion. I'm not sure if it can be counted as an answer to the question though.
  • @KonstantinLoginov: LeakCanary appears to have a fair number of interconnected moving parts, which is why I am skeptical that a fork will be all that easy. I did enough poking around to determine that the only Android 5.0+ feature was Theme.Material, which is what led me to ask this question. While I am framing the question in the context of LeakCanary, the issue transcends that one library.
  • It would be nice if google's current gradle plugin source was public. Tags for the DSL's source [1] are way behind the release tags [2]. [1] [2]
  • I think your LEAKCANARY_ARTIFACT_NOOP definition has a bug. And while this is interesting, it does not answer the actual question: "Is there a reasonably simple way for a module's build.gradle file to indicate that certain files from a dependency should be excluded?" It addresses my example scenario in an alternative fashion (modifying resources). Thanks, though!
  • I don't think that there's any way to do what you want out of the box. But you can write your own task, like this, that will be executed before resource merging and will delete resources that you don't need. All resources of each aar module are stored in build/exploded-aar directory, and you can safely remove them from there and they would not be processed at all. However, about simplicity: you can write your own wrapper for "compile" closure which can take array of files/dirs that will be excluded in task. I will try to make this tomorrow.
  • Edited the answer. Now excluding is much simplier, i think.
  • I tried using this in Android Studio 2.2.2 and it fails with: Error:Could not find method $() for arguments [build_6l34hmliq0djb0q3abscneim0$_run_closure2$_closure16$_closure17@1ee01b51] on project ':gw_lib' of type org.gradle.api.Project.
  • I changed the name from "exclude" to "excludeFiles" and it works.
  • I'll give it a try sometime, but I am skeptical that this will work. First, pacakgingOptions is much too late in the build process -- the build should fail due to the compileSdkVersion before it gets there. Second, AFAIK, packagingOptions is referring to excluding things based on where they are in the APK, and XML resources do not go in the APK as ordinary files. Thanks, though!
  • any chance to do it dynamically?…