Exclude jar-library from aar

rickul picture rickul · Jul 29, 2016 · Viewed 9.7k times · Source

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

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:

:transformClassesWithDexForDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
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? How could I achieve this with this gradle:

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
}

### UPDATE ###

I tried the gradle scope "provided" as suggested by Xaver Kapeller (thanks!). But there is still the cordova.jar packaged into the aar. Does the provided scope simply not work or am I missing something?

Answer

Lingkun Kong picture Lingkun Kong · Sep 27, 2016

You can simply move the library file from dir yourmodule/libs to dir yourmoudle/ and modify " provided files('libs/yourlibrary.jar') " to "provided files('yourlibrary.jar')" in dependency.

then clean the project and rebuild.