問題描述
模塊的 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模板網!