我如何教ProGuard摆脱我一直在使用的东西? [英] How Do I Teach ProGuard to Get Rid of Something It Is Keeping That I Am Not Using?

查看:108
本文介绍了我如何教ProGuard摆脱我一直在使用的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目,其中app模块的proguard-rules.pro文件仅包含以下内容:

I have an Android project with a proguard-rules.pro file for the app module that contains only the following:

# ProGuard rules
-dontobfuscate
-dontwarn android.arch.util.paging.CountedDataSource
-dontwarn android.arch.persistence.room.paging.LimitOffsetDataSource

我自己什么都没保留.所有-keep规则都来自其他内容,无论是getDefaultProguardFile('proguard-android-optimize.txt')提供的规则还是库中打包的规则.

I am not keeping anything myself. All -keep rules are coming from something else, whether that is the rules provided by getDefaultProguardFile('proguard-android-optimize.txt') or from rules packaged in libraries.

但是,保留了我不使用的东西.如果在我的release版本上使用Android Studio APK分析器,虽然ProGuard删除了许多 内容,但还有许多其他我没有引用的内容.

However, stuff is being kept that I am not using. If I use the Android Studio APK Analyzer on my release build, while lots of things are removed by ProGuard, lots of other things are kept that I am not referencing.

例如:通过传递依赖关系,我具有支持库模块,该模块在应用程序的依赖关系树中包含ViewPager.但是,我(当前)不在此应用程序中使用ViewPager.尽管如此,APK分析器显示了android.support.v4.view.ViewPager的107种已定义方法,包括其构造函数,因此仍然保留了它.

For example: through transitive dependencies, I have the Support Library module that contains ViewPager in the app's dependencies tree. However, I am not (presently) using ViewPager in this app. Despite this, something is causing it to be kept, as the APK Analyzer shows 107 defined methods for android.support.v4.view.ViewPager, including its constructor.

我可以使用各种ProGuard选项来追踪为什么要保留它.但是,这并非来自我的规则.我自己没有-keep需要修复—. -keep来自其他人,大概是一位Google工程师.

I could use various ProGuard options to track down why this is being kept. However, it is not coming from my rules. There is no -keep of my own that needs fixing — the -keep is coming from somebody else, presumably a Google engineer.

那么,如何摆脱ViewPager?有没有办法我可以覆盖导致保留该规则的-keep规则(例如,使用allowshrinking)?如果是这样,由Android Studio调用的ProGuard如何确定谁的-keep规则获胜?

So, how do I get rid of ViewPager? Is there a way that I can override the -keep rule that is causing it to be kept (e.g., using allowshrinking)? If so, how does ProGuard, as invoked by Android Studio, determine whose -keep rule wins?

推荐答案

ViewPager类没有保存在我刚刚检查过的小应用程序中,因此它确实必须是项目中的其他代码或其他规则.

The ViewPager class isn't kept in a small app that I just checked, so it must be other code or other rules in your project indeed.

您可以首先让ProGuard打印出触发ViewPager保留的链:

You can start with letting ProGuard print out the chain that triggers ViewPager to be kept:

-whyareyoukeeping class android.support.v4.view.ViewPager

对于各种类和方法,您可能需要重复此操作多次才能找到根本原因. ProGuard不会打印出确切负责哪个规则-诚然,这将是一个有用的功能.

You may need to repeat this a number of times for various classes and methods to get to the root cause. ProGuard doesn't print out which rule exactly is responsible -- admittedly, this would be a useful feature.

然后您可以在build/intermediates/exploded-aar中查找包含匹配规则的proguard.txt文件.

You can then look for the proguard.txt file in build/intermediates/exploded-aar that contains a matching rule.

关于此时的解决方案:

  • 无法覆盖-keep规则;他们只是积累.
  • 据我所知,Android Gradle插件也不支持在库中禁用过于保守的proguard.txt文件,因此您需要使用更新后的规则创建自定义.aar文件,或向其发送建议库的开发人员.
  • It is not possible to override -keep rules; they only accumulate.
  • As far as I know, the Android Gradle plugin also doesn't support disabling overly conservative proguard.txt files in libraries, so you'd need to create a custom .aar file with the updated rule, or send a suggestion to the developers of the library.

这篇关于我如何教ProGuard摆脱我一直在使用的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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