Alok Patel
Alok Patel

Reputation: 8032

Exclude package/classes from @aar, gradle dependency

I've following dependency added in build.gradle file.

compile 'com.aerisweather:aeris-maps-lib:2.0.0@aar'

It is from

https://oss.sonatype.org/content/repositories/comaerisweather-1027/com/aerisweather/aeris-maps-lib/2.0.0/

If you the see artifacts from following URL, It has android support v7 library classes.

https://oss.sonatype.org/#nexus-search;quick~aerisweather

I want to exclude that package when running/packaging the application. I'm unable to run/package the app due to duplicate class error.

I've tried adding configurations like this,

configurations {
    all*.exclude group: 'com.android.support', module: 'appcompat-v7'
}

But this excludes it from entire project which leads me to many errors.

I've tried everything but still getting following error.

Error:Execution failed for task ':transformClassesWithJarMergingForDebug'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/v7/appcompat/R$anim.class

Upvotes: 6

Views: 9490

Answers (2)

Jitesh Singh
Jitesh Singh

Reputation: 101

not a direct answer, but an advice.

The exclusion feature provided by gradle (exclude method invocation) doesn't work for contents inside local aar files as those contents aren't defined by dependency management and hence aren't recognised by the same. As far as the dependency resolution is concerned, the aar file is an individual unit (including all the resources/classes within). So the file needs to be built in a way which doesn't include those entries; Or if the file is not built by you, you can unpack and omit the files in question and repack.

While there may be hackish ways to drop certain files using gradle (I couldn't find any reliable one yet), where we could possibly hook into some intermediate build steps and get rid of the files; but the generally advised best practise is to avoid packaging publicly available dependencies into the aar/jar to avoid duplicate entry issues and keep the aar/jar size smaller.

Upvotes: 2

This library has also as dependency support-v4 and mediarouter-v7.

You need to exclude them all from aeris-maps-lib and include as your own dependency.

def supportLibraryVersion = '25.0.1'
dependencies {
    compile "com.android.support:support-v4:${supportLibraryVersion}"
    compile "com.android.support:support-annotations:${supportLibraryVersion}"
    compile "com.android.support:appcompat-v7:${supportLibraryVersion}"

    //... other deps

    compile ('com.aerisweather:aeris-maps-lib:2.0.0@aar', {
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'mediarouter-v7'
    })
}

PS.

aeris-maps-lib has also com.google.android.gms:play-services dependency, which is the whole Play Services package (it's large) and you will need to enable MultiDex or shrink code with proguard.

Upvotes: 3

Related Questions