忽略外部库的proguard配置 [英] Ignore proguard configuration of an external library

查看:180
本文介绍了忽略外部库的proguard配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想向我的项目添加一个外部库.该库本身很小,大约有300种方法.但是它的proguard配置使其配置非常自由.我在一个准系统项目上使用/不使用库以及使用/不使用proguard进行了一个简单的测试,这就是我想出的

So, I want to add an external library to my project. The library itself is quite small, around 300 methods. But it is configured to be very liberal with it's proguard configuration. I ran a simple test with/without the library and with/without proguard on a barebones project and this is what I came up with

Proguard    Lib     Method Count
N           N       15631
Y           N       6370
N           Y       15945
Y           Y       15573

如您所见,启用了proguard后,计数约为6000.但是,当我添加库的那一刻,尽管库本身只是约300种方法,但计数却高达约15000.

As you can see, with proguard enabled, the count is ~6000. But the moment I add the lib, count shoots up to ~15000 despite the library itself being only ~300 methods.

所以我的问题是,如何忽略该特定库的proguard配置?

So my question is, how do I ignore the proguard configuration of this particular library?

更新:

现在无法使用android gradle插件.我发现了 Android错误,它根本没有优先级.请避免回答提及不可能"的问题,并在解决问题或做出正式决定之前保持开放性.否则,您将获得一半的赏金而不会增加价值.谢谢!

It is not possible with android gradle plugin now. I found android bug which doesn't have priority at all. Please avoid answers with mentioning "it is not possible" and keep question opened until a workaround or an official decision is possible. Otherwise, you will collect half of bounty without adding value. Thanks!

推荐答案

在这种特定情况下,您有几种选择:

In this specific case you have a few options:

  • 从aar提取classes.jar文件,并将其作为常规jar依赖项包含在您的项目中(当aar包含资源时将不起作用)
  • 更改aar并从中删除消费者保护规则
  • 使用 DexGuard ,它可以过滤掉不需要的消费者规则
  • 进行一些gradle黑客攻击,请参见下文
  • extract the classes.jar file from the aar and include it as normal jar dependency in your project (will not work when the aar includes resources)
  • change the aar and remove the consumer proguard rules from it
  • use DexGuard which allows you to filter out unwanted consumer rules
  • do a bit of gradle hacking, see below

将以下内容添加到您的build.gradle中:

Add the following to your 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中:

When using DexGuard (7.2.02+), you can add the following snippet to your 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 将仅包含与模式匹配的使用者规则.

Mind that the logic is inverted to the ProGuard example above, the consumerRuleFilter will only include consumer rules that match the pattern.

这篇关于忽略外部库的proguard配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆