Avoid packing .jar into Android Library (.aar)

2019-05-25 03:35发布

问题:

My project is an Android Library which depends on Dropbox's android library.

dependencies {
    ...
    provided fileTree(dir: '../Libraries/Dropbox', include: ['*.jar'])
    ...
}

Everything works well excepts Gradle puts all the .jar files from Dropbox into my output .aar file.

MyLib.aar
|-classes.jar
|-AndroidManifest.xml
|-...
|-libs
    |-bcprov-jdk16-146.jar
    |-commons-logging-1.1.1.jar
    |-dropbox-android-sdk-1.6.1
    |-json_simple-1.1.jar

How can I avoid this?

回答1:

something like this might help you:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        def packageLib = output.getPackageLibrary()
        packageLib.exclude('libs/externalLibrary.jar')
    }
}

inside android {} block



回答2:

  1. Why do you want to avoid this? When you give your library to someone, they have all the dependencies already in one file.

  2. You can include the dependencies via

compile 'com.dropbox.core:dropbox-core-sdk:1.7.7' 
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'commons-logging:commons-logging:1.2'
compile 'org.bouncycastle:bcprov-jdk16:1.46'

in your build.gradle file and remove it from the libs folder. Do the same with the other dependencies. This way they will not be packaged into your .aar file.