8

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.

Frank
  • 12,010
  • 8
  • 61
  • 78
  • Do yo want to remove some package of your own APK or modules from dependency? – AndiGeeky Nov 30 '16 at 08:59
  • packages from the dependency. The facebook lib example has lots of com.facebook.XXX packages, I'd like to not include one of them. I updated my question's example. – Frank Nov 30 '16 at 09:06
  • Yes you can. Check answer below!! – AndiGeeky Nov 30 '16 at 09:07
  • that answer shows how to remove a dependency of the dependency, not a package of the dependency itself. – Frank Nov 30 '16 at 09:10
  • 1
    Oh Yeah!! I guess we can not remove packages then. Still wait for sometime. If someone know then it will be helpful. – AndiGeeky Nov 30 '16 at 09:12

4 Answers4

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.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • Okay that's clear. Using proguard on debug builds is too build-time consuming. It's not possible I assume, I'll accept this answer. – Frank Nov 30 '16 at 11:04
0

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'
}
Ak9637
  • 990
  • 6
  • 12
  • are these packages or referenced libraries from your dependency? I think the last. If not, please give an example on how to use it on something inside com.github.ganfra, as "com.github.ganfra.somepackage". – Frank Nov 30 '16 at 09:05
  • i dont know abt app, but lets say u r building a library,and u wish to avoid conflict of importing the other libraries and package you are using in it but the user might have already import those,populary libraries like retrofit,etc.... in that case we can use the exclude command to remove those dependencies from your library...so that your library can use the one in the project itself – Ak9637 Nov 30 '16 at 09:08
0

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

Neo
  • 3,546
  • 1
  • 24
  • 31
0

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'
}
DalusC
  • 391
  • 1
  • 12