Exclude jar-library from aar


I have a library project, which has a dependency to cordova.jar which is loaded from the projects libs directory.

library project

  • libs/
    • cordova.jar

This library project I deploy as an aar to a maven nexus.

In an application project, I have a dependency to the library projects aar (compile …). But the application project has the same dependency to the cordova.jar.

So when building the applicaton project, I get this error:

com.android.dex.DexException: Multiple dex files define Lorg/apache/cordova/AuthenticationToken;
	at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
	at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
	at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
	at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
	at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
	at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
	at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
	at com.android.dx.command.dexer.Main.run(Main.java:277)
	at com.android.dx.command.dexer.Main.main(Main.java:245)
	at com.android.dx.command.Main.main(Main.java:106)

To solve this, i tried to exclude the cordova.jar when loading the library aar in my application project:

dependencies {
    compile ('com.my.library:hybridstory:1.0'){
        exclude (group: 'org.apache.cordova')

But this does not work. Am I doing something wrong?

My second question is:

Is it possible to exclude the /libs/cordova.jar when uploading the library project as aar, instead of excluding the cordova.jar when loading the library in an application?

Thanks and Regards

I tried to do this using the provided scope. But the jar is still packaged into the aar. Is the provided scope not working or am I missing something?

apply plugin: 'com.android.library'
apply plugin: 'maven'

archivesBaseName = 'hybridstory'
group = 'com....'
version = '16.3.0-SNAPSHOT'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName version
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

dependencies {
    provided files('libs/cordova-5.1.1.jar')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com....'

task createJavadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    title = archivesBaseName + " " + version
    options.overview = "src/main/doc-files/overview.html"
    doLast {
        copy {
            from "src/main/doc-files"
            into "$buildDir/docs/javadoc"

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'https://maven..../content/repositories/om-releases/')
            snapshotRepository(url: 'https://maven..../content/repositories/om-snapshots/')

task androidJavadocsJar(type: Jar, dependsOn: createJavadoc) {
    classifier = 'javadoc'
    from createJavadoc.destinationDir

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar