所以,我想在我的项目中添加一个外部库。图书馆本身很小,大约有300种方法。但它配置为非常自由的,它的proguard配置。我在一个准系统项目上运行了一个带/不带库和/无proguard的简单测试,这就是我想出来的
Proguard Lib Method Count
N N 15631
Y N 6370
N Y 15945
Y Y 15573
如您所见,启用proguard后,计数为~6000。但是当我添加lib时,尽管库本身只有大约300种方法,但计数却达到了15000左右。
所以我的问题是,如何忽略此特定库的proguard配置?
更新
现在无法使用android gradle插件。我发现android bug根本没有优先权。请在提及“不可能”时避免回答,并在可能的解决方法或官方决定之前保持问题开启。否则,你将收集一半的赏金而不增加价值。谢谢!
答案 0 :(得分:6)
在这种特定情况下,您有几个选择:
将以下内容添加到build.gradle:
afterEvaluate {
// All proguard tasks shall depend on our filter task
def proguardTasks = tasks.findAll { task ->
task.name.startsWith('transformClassesAndResourcesWithProguardFor') }
proguardTasks.each { task -> task.dependsOn filterConsumerRules }
}
// Let's define our custom task that filters some unwanted
// consumer proguard rules
task(filterConsumerRules) << {
// Collect all consumer rules first
FileTree allConsumerRules = fileTree(dir: 'build/intermediates/exploded-aar',
include: '**/proguard.txt')
// Now filter the ones we want to exclude:
// Change it to fit your needs, replace library with
// the name of the aar you want to filter.
FileTree excludeRules = allConsumerRules.matching {
include '**/library/**'
}
// Print some info and delete the file, so ProGuard
// does not pick it up. We could also just rename it.
excludeRules.each { File file ->
println 'Deleting ProGuard consumer rule ' + file
file.delete()
}
}
使用DexGuard(7.2.02+)时,您可以将以下代码段添加到build.gradle:
dexguard {
// Replace library with the name of the aar you want to filter
// The ** at the end will include every other rule.
consumerRuleFilter '!**/library/**,**'
}
请注意,逻辑与上面的ProGuard示例相反, consumerRuleFilter 仅包含与模式匹配的消费者规则。