简体   繁体   中英

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-sdk:4.17.0') {
        exclude package 'com.facebook.share'
    }
}

but then different, because "package" is not a valid command.

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

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.

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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM