如何告诉 ProGuard 将所有内容保存在特定包中?
我的应用程序有很多活动,也使用本机库。使用 Eclipse 生成的默认 ProGuard 配置,ProGuard 会删除很多东西 - OnClick 方法、静态成员、我的本机库使用的回调方法...是否有一种简单的方法来指示 ProGuard 不要从我的包中删除任何内容?删除一些东西只能节省大约 2.5% 的应用程序大小,但会完全破坏我的应用程序。在 ProGuard 配置中逐类配置、测试和维护它会很痛苦。
My application has many activities and uses native library too. With the default ProGuard configuration which Eclipse generates ProGuard removes many things - OnClick methods, static members, callback methods which my native library uses... Is it there a simple way to instruct ProGuard to NOT remove anything from my package? Removing things saves only about 2.5% of the application size, but breaks my application completely. Configuring, testing and maintaining it class by class in ProGuard configuration would be a pain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑 这个答案已经有 10 年历史了 - 它可能不适用于较新的 proguard 版本。
我认为你至少需要添加这些标志(修改你个人的包名称):
另外,添加这些标志:
这是我的整个配置文件:我的 Proguard.cfg:
EDIT This answer is 10 years old - it may not apply to newer proguard versions.
I think you need to add these flags at the very least (modify for you individual package names):
Also, add these flags:
Here's my whole config file: of my Proguard.cfg:
作为最终结果,我发现仅保留所有类成员对于我的应用程序的正确工作来说是不够的,也没有必要。我在设置文件中添加了以下内容:
onClick* 的情况适用于我在 .xml 布局文件中的 android:onClick 属性中处理的所有方法(我以“onClick”开头所有此类方法的名称)。
*Callback 的情况适用于我从本机代码(通过 JNI)调用的所有回调方法。我在每个此类方法的名称中添加后缀“Callback”。
此外,我还为一些特殊情况添加了几行,这些变量是我从本机代码中使用的变量,例如:(
对于来自包 com.myapp.mypackage 中名称为“SomeMyClass”的类中名称为“position”的变量)
所有这些都是因为这些onClick、callback等不仅必须存在,而且要保留原来的名称。其他的东西 ProGuard 可以自由优化。
本机方法的情况也很重要,但在 Eclipse 文件生成的文件中有一个声明:
As final result I found that just keeping all class members is not enough for the correct work of my application, nor necessary. I addded to the settings file this:
The case with onClick* is for all methods which I address in android:onClick atribute in .xml layout files (I begin the names of all such methods with 'onClick').
The case with *Callback is for all callback methods which I call from my native code (through JNI). I place a suffix 'Callback' to the name of every such method.
Also I added few rows for some special cases with variables which I use from native code, like:
(for a varible with name 'position' in a class with name 'SomeMyClass' from package com.myapp.mypackage)
All this is because these onClick, callback etc. must not only be present but also kept with their original names. The other things ProGuard can optimize freely.
The case with the native methods is important also, but for it there was a declaration in the generated from Eclipse file:
我知道这是一个老问题,但我希望以下信息可以帮助其他人。
您可以阻止 ProGuard 删除某个包中的任何内容,如下所示;
使用修饰符
allowoptimization
和allowobfuscation
将确保 ProGuard 仍然对代码进行混淆和优化。收缩当然会按预期被禁用。您可以轻松验证这些
-keep
规则如何影响代码,而无需使用 ProGuard 游乐场。I know this is an old question but I hope that the following information might help other people.
You can prevent ProGuard from removing anything in a certain package as follows;
Using the modifiers
allowoptimization
andallowobfuscation
will make sure that ProGuard still obfuscates and optimizes the code. Shrinking will of course be disabled as intended.You can easily verify how these
-keep
rules affect the code without the need to (re-)build using the ProGuard Playground.