删除未使用的类使用ProGuard为AndroidProGuard、Android

2023-09-05 01:19:29 作者:擦干眼泪我还是王

历史/背景 我有一个项目[1],其中大小真的很重要 - 最近我使用的lib感动的东西到一个共享库[2],并认为ProGuard的会照顾和删除未使用的类,因为我有一个配置被大大减少了尺寸,但由我来到魔术100KB标志,所以我调查:类,我不使用肯定是在生成DEX文件 - 甚至与全名(不缩短到单字符) - 例如:我看到DEX的SquareView这是我在应用程序。

History/Context I have a project[1] where size really matters - recently I moved stuff to a shared lib[2] and thought proguard will take care and remove the unused classes because I had a config that was drastically reducing the size but by using the lib i came over the magic 100kb mark so I investigated: classes which I do not use for sure are in the resulting dex file - and even with full name ( not shortened to single-char ) - e.g. I see the SquareView in the dex which I in no way use in the App.

问题 令人惊讶的,我发现ProGuard文档中的以下内容:

Question Surprisingly I found in the proguard documentation the following:

The library jars themselves always remain unchanged. 

我可以以某种方式告诉/招ProGuard的(中)来处理呢?我觉得这真的很奇怪,特别是因为我希望更多的东西是可移动的lib比在应用程序本身。

Can I somehow tell/trick proguard (in)to process them? I find this really strange especially because I expect more stuff to be removeable in the lib than in the App itself..

[1] https://github.com/ligi/FAST [2] https://github.com/ligi/AndroidHelper

[1] https://github.com/ligi/FAST [2] https://github.com/ligi/AndroidHelper

推荐答案

在Eclipse中/ ANT /摇篮建立在Android SDK程序自动从斌/类指定code()和库(由)的选项 -injars 。这意味着完整的应用程序被压缩,优化,混淆(在发布版本,假设ProGuard的启用)。

The Eclipse/Ant/Gradle build processes in the Android SDK automatically specify your code (from bin/classes) and its libraries (from libs) with the option -injars. This means that the complete application is compacted, optimized, and obfuscated (in release builds, assuming ProGuard is enabled).

构建过程只用选项 -libraryjars 指定Android运行的android.jar 。有必要处理code,但它不应该结束在处理APK,因为它已经$设备页上$ psent。

The build processes only specify the Android runtime android.jar with the option -libraryjars. It is necessary to process the code, but it should not end up in the processed apk, since it is already present on the device.

所以应该全部自动工作了。您仍可以看到其原始名称整个图书馆处理的apk,如果你的配置 ProGuard的-project.txt 包含像 -keep类org.mylibrary线** {*; } 。这样的结构通常是保守的解决方案,以占反射。随着一些研究和实验,你经常可以细化的配置,并取得更好的成绩。你可以找出原因班被关押的选项 -whyareyoukeeping

So it should all work out automatically. You may still see entire libraries with their original names in processed apks, if your configuration proguard-project.txt contains lines like -keep class org.mylibrary.** { *; }. Such configuration is typically a conservative solution to account for reflection. With some research and experimentation, you can often refine the configuration and get better results. You can figure out why classes are being kept with the option -whyareyoukeeping.