如何从 AAR 依赖中排除特定资源?

时间:2023-02-13
本文介绍了如何从 AAR 依赖中排除特定资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模块的 build.gradle 文件是否有一种相当简单的方法来指示应排除依赖项中的某些文件?我对从 AAR 中排除某些资源特别感兴趣.

Is there a reasonably simple way for a module's build.gradle file to indicate that certain files from a dependency should be excluded? I am specifically interested in excluding certain resources from an AAR.

LeakCanary 是一个有趣的库,用于帮助追踪内存泄漏.但是,它对 compileSdkVersion 的未记录要求为 21 或更高.虽然大多数项目不应该有这个问题,但库在没有充分理由的情况下要求某个 compileSdkVersion 是不合时宜的.开发团队可能已经冻结了他们的 compileSdkVersion 作为一般政策的一部分,只在他们的应用程序或其他东西的主要版本更新中更改这些类型的设置.

LeakCanary is an interesting library for helping to track down memory leaks. However, it has an undocumented requirement of compileSdkVersion of 21 or higher. While most projects should not have a problem with this, it's unseemly for a library to require a certain compileSdkVersion without a good reason. A development team may have frozen their compileSdkVersion as part of a general policy to only change those sorts of settings as part of major version updates of their app or something.

在这种情况下,至少对于 v1.3.1 的 LeakCanary,AFAICT 需要 compileSdkVersion 的唯一原因是因为 AAR 有一个 res/values-v21/ 目录,包含从 Theme.Material 继承的主题定义.此主题由诊断活动使用.最终用户永远不会看到该活动,只有在 debug 构建中的开发人员才能看到.坦率地说,那个活动看起来像什么,就主题而言,并不重要.强制 compileSdkVersion 为 21 只是为了让诊断活动具有特定主题,恕我直言,愚蠢.

In this case, for v1.3.1 of LeakCanary at least, the only reason compileSdkVersion is required, AFAICT, is because the AAR has a res/values-v21/ directory, containing a theme definition that inherits from Theme.Material. This theme is used by a diagnostic activity. That activity is never seen by end users, only by developers in debug builds. Frankly, what that activity looks like, theme-wise, does not really matter. Forcing a compileSdkVersion of 21 just to have that diagnostic activity have a certain theme is, IMHO, stupid.

如果作为 compile 指令的一部分,我们可以说嘿,请跳过这个 AAR 中的 res/values-v21/,好吧?".由于 -v21 主题只是提供了在别处定义的主题的替代定义,因此删除 -v21 主题不会在运行时破坏构建或破坏事物,而只会给我们一个以 Holo 为主题的诊断活动.

It'd be nice if as part of a compile directive we could say "hey, please skip res/values-v21/ from this AAR, m'kay?". Since the -v21 theme is simply providing an alternative definition of a theme defined elsewhere, dropping the -v21 theme will not break the build or break things at runtime, but merely will give us a Holo-themed diagnostic activity.

我看不到 this answer 如何处理依赖项.我也不确定是否完整,而且它似乎不被支持.它也并不真正符合简单"的条件—我不希望有人尝试将其放入 build.gradle 文件中,只是为了阻止来自 LeakCanary 等诊断库的单个文件.

I fail to see how this answer works with dependencies. I am also uncertain if it is complete, and it certainly does not appear to be supported. It also doesn't really qualify as "simple" — I would not expect somebody to try dropping this in a build.gradle file just to block a single file from a diagnostic library like LeakCanary.

那么,有没有比这更简单的方法可以与当前版本的 Android Plugin for Gradle 一起使用?

So, is there something simpler than this that works with now-current editions of the Android Plugin for Gradle?

推荐答案

为您编写了高级 gradle 任务:

Wrote advanced gradle task for you:

final List<String> exclusions = [];

Dependency.metaClass.exclude = { String[] currentExclusions ->
    currentExclusions.each {
        exclusions.add("${getGroup()}/${getName()}/${getVersion()}/${it}")
    }
    return thisObject
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ('com.android.support:appcompat-v7:20.+')
    debugCompile ('com.squareup.leakcanary:leakcanary-android:1.3.1')
            .exclude("res/values-v21/values-v21.xml")
    releaseCompile ('com.squareup.leakcanary:leakcanary-android-no-op:1.3.1')
}

tasks.create("excludeTask") << {
    exclusions.each {
        File file = file("${buildDir}/intermediates/exploded-aar/${it}")
        println("Excluding file " + file)
        if (file.exists()) {
            file.delete();
        }
    }
}

tasks.whenTaskAdded({
    if (it.name.matches(/^process.*Resources$/)) {
        it.dependsOn excludeTask
    }
})

现在您可以在每个依赖项上使用方法 .exclude() ,提供到路径列表中,您想从指定的依赖项中排除.此外,您可以堆叠 .exclude() 方法调用.

Now you can use method .exclude() on each dependency, providing into list of paths, you want to exclude from specified dependency. Also, you can stack the .exclude() method calls.

这篇关于如何从 AAR 依赖中排除特定资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:第三方库上的 gradle force build tools 版本? 下一篇:java.lang.IllegalAccessError: 方法 'void android.suppor

相关文章

最新文章