Skip to content Skip to sidebar Skip to footer

Gradle: Exclude A Dependency's Package

Is it possible to exclude a package from an Android Gradle dependency so it does not end up inside the APK? As: dependencies { compile('com.facebook.android:facebook-android-s

Solution 1:

You can't exclude some specific parts of the artifact because Gradle doesn't know anything about what is inside it. To Gradle it's monolithic: you either include the artifact with whatever is inside it, or not.

It may be possible to achieve what you want using ProGuard. This is a common step when building a release version of the application, you can specify the rules to do shrinking, which will remove unnecessary code. Android documentation has a code shrinking article with more details.

The only problem that could arise is that if the dependency includes a ProGuard configuration file itself, then it may not be possible to force shrinking of what is specified to be kept in there. However, I've just looked into the AAR you asked about, and that doesn't seem to be the case.


Solution 2:

in this way , u can exclude few packages from the library, this is just a example of concept

compile ('com.github.ganfra:material-spinner:1.1.1'){
        exclude group: 'com.nineoldandroids', module: 'library'
        exclude group: 'com.android.support', module: 'appcompat-v7'
}

Solution 3:

You can use sourceSets inside your build.gradle to filter the packages or classes you don't want to bundle in your apk or aab. For example -

android {
  sourceSets {
        main {
            java {
                exclude 'com/a/b/helper/**'
                exclude 'com/a/ext/util/TestUtils.java'
            }
        }
    }
}

Hope it will help :)


Solution 4:

Updated answer for excluding reference libraries from your dependency groups

implementation (group: 'net.sf.jasperreports', name: 'jasperreports', version: '6.1.0'){
    //example : org.olap4j:olap4j:0.9.7.309-JS-3
    exclude group: 'org.olap4j', module: 'olap4j'
}

Post a Comment for "Gradle: Exclude A Dependency's Package"