Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 759
rickul Avatar asked Jul 29 '16 12:07

rickul


1 Answers

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.

like image 59
Lingkun Kong Avatar answered Sep 18 '22 02:09

Lingkun Kong