简体   繁体   English

从@aar 中排除包/类,gradle 依赖

[英]Exclude package/classes from @aar, gradle dependency

I've following dependency added in build.gradle file.我在build.gradle文件中添加了以下依赖build.gradle

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/ 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.如果您从以下 URL 看到工件,它具有 android 支持 v7 库类。

https://oss.sonatype.org/#nexus-search;quick~aerisweather 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'.错误:任务“:transformClassesWithJarMergingForDebug”的执行失败。 com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/v7/appcompat/R$anim.class com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:android/support/v7/appcompat/R$anim.class

This library has also as dependency support-v4 and mediarouter-v7 .该库还具有作为依赖项support-v4mediarouter-v7

You need to exclude them all from aeris-maps-lib and include as your own dependency.您需要将它们全部从aeris-maps-lib排除并包含为您自己的依赖项。

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. aeris-maps-lib也有com.google.android.gms:play-services依赖项,它是整个 Play 服务包(很大),你需要启用 MultiDex 或使用 proguard 压缩代码。

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. gradle 提供的排除功能(排除方法调用)不适用于本地 aar 文件中的内容,因为这些内容不是由依赖项管理定义的,因此不被相同的识别。 As far as the dependency resolution is concerned, the aar file is an individual unit (including all the resources/classes within).就依赖解析而言,aar 文件是一个单独的单元(包括其中的所有资源/类)。 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;虽然可能有一些使用 gradle 删除某些文件的黑客方法(我还没有找到任何可靠的方法),但我们可以在其中加入一些中间构建步骤并删除这些文件; 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.但通常建议的最佳做法是避免将公开可用的依赖项打包到 aar/jar 中,以避免重复输入问题并保持 aar/jar 的大小更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM