如何在 Eclipse 中使用 Proguard 混淆 Android 库(.jar 文件)
我看过很多关于如何在 Eclipse 中使用 ProGuard 混淆 Android 应用程序(.apk 文件)的文章。另请参阅http://developer.android.com/guide/developing/tools/proguard.html:
“当您在发布模式下构建应用程序时,无论是通过运行 ant release 还是使用 Eclipse 中的导出向导,构建系统都会自动检查是否设置了 proguard.config 属性。如果是的,ProGuard 会在之前自动处理应用程序的字节码将所有内容打包到一个 .apk 文件中。”
但是,如果使用 Eclipse 导出向导将 Android 项目导出为 .jar 文件,请按照描述的步骤(创建文件 proguard.cfg、将 proguard.config 属性配置为文件 default.properties 中的 proguard.cfg、使用导出向导)等)似乎不起作用 - 我在生成的 jar 文件中没有看到类名等的混淆。我的 proguard.cfg 文件中也有以下设置,但我在项目目录或 proguard 目录中没有看到任何输出文件(该目录甚至没有创建)。
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
我什至在我的项目目录中创建了一个文件project.properties,其中包含以下行,但这似乎并没有吸引ProGuard采取行动:
proguard.config=proguard.cfg
此项目中没有定义任何活动。我在 Windows 上使用 Android 2.3.1 和 Eclipse Galileo 3.5.2。与 Android 3.0 的结果相同。似乎必须以某种方式在 Eclipse 导出向导中显式插入混淆步骤。我将不胜感激任何帮助或见解。谢谢。
I have seen many posts about how to obfuscate an Android application (.apk file) using ProGuard in Eclipse. Also see http://developer.android.com/guide/developing/tools/proguard.html:
"When you build your application in release mode, either by running ant release or by using the Export Wizard in Eclipse, the build system automatically checks to see if the proguard.config property is set. If it is, ProGuard automatically processes the application's bytecode before packaging everything into an .apk file."
But in case of exporting an Android project in a .jar file using Eclipse Export Wizard, following the described steps (of creating a file proguard.cfg, configuring proguard.config property to proguard.cfg in the file default.properties, using Export Wizard etc.) does not seem to work - I see no obfuscation of class names, etc. in the resulting jar file. I also have the following settings in my proguard.cfg file, but I don't see any output files in my project directory or in the proguard directory (that directory is not even created).
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
I have even created a file project.properties in my project directory with the following line but that did not seem to entice ProGuard into action:
proguard.config=proguard.cfg
There are no activities defined in this project. I am using Android 2.3.1 and Eclipse Galileo 3.5.2 on Windows. Same results with Android 3.0. Seems like the obfuscation step has to be somehow interjected explicitly in the Eclipse Export Wizard. I will appreciate any help or insight. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要混淆您的纯 Java Jar。在生成 Jar 时完全跳过该阶段(无论是在 Eclipse 中手动还是通过命令行的 Ant 构建)。
相反,在 client 项目中设置并执行适当的混淆,即使用 Jar 的项目,您可以在其中将库添加为外部 Jar。 Proguard 也能够混淆 Jar 中的代码。
我偶然发现了这个问题,并最终如我所描述的那样成功。
Do not obfuscated your pure Java Jar. Skip that phase completely while producing the Jar (whether manually in Eclipse or via Ant build from command line).
Instead, setup and perform proper obfuscation in the client project, the one using the Jar, where you add the library as external Jar. Proguard will be able to obfuscate code within the Jar too.
I stumbled upon this issue, and ended up successfully as I described here.
调用 ProGuard 的方法相当简单:
proguard.config=proguard.cfg
添加到 project.properties默认的 proguard.cfg 文件应该已由新项目向导自动创建。
The way to invoke ProGuard is fairly straightforward:
proguard.config=proguard.cfg
to project.propertiesA default proguard.cfg file should have been automatically created by the new project wizard.
我使用间接的方式生成导出的android混淆jar,我的方式是:
使用eclipse导出签名的apk
解压apk,找到classes.dex
使用dex2jar.bat,将classes.dex改为jar
解压jar 并删除不需要的类,然后将其压缩并
将文件名更改为XXX.jar
然后你在其他项目中使用这个jar,或者将其交给客户,它
是混淆的!
我相信这会对您有所帮助!好好享受!
I use an indirect way to generate a exported android obfuscate jar, my way is:
export a signed apk use eclipse
unzip the apk, find the classes.dex
use dex2jar.bat ,change the classes.dex to a jar
unzip the jar and delete the class you do not need,then zip it and
change the file name to XXX.jar
Then you use this jar in other project,or give it to customer, it
is obfuscate!
I am sure this will help you! Enjoy it!
yourconfig.pro(扩展自 http://proguard.sourceforge.net/index.html#manual/examples。 html):
可以使用 jd-gui 验证结果
yourconfig.pro (extended from http://proguard.sourceforge.net/index.html#manual/examples.html):
Result can be verified with jd-gui
正如对上述答案之一的评论中所建议的(但我一开始没有注意到,因为它被埋在其中一个“附加评论”中)……
我们只需在命令行上运行 progruard (请参阅第一个下面的块)在 Eclipse 外部的库上,并使用我们的
proguard.cfg
文件中下面第二个块中的参数(并且绝对不要使用-dontpreverify
,或由于您的project.jar 中的StackMapTable 问题,将您的项目用作android 库的项目将无法正确地进行混淆。命令行:
proguard.cfg:
可能并非所有参数和 -keep 项都是严格必需的(除了前面提到的不使用
-dontpreverify
之外),但其中大多数对我来说作为项目有意义如果您要导出的库中有 Activity 类扩展,则需要在那里。as suggested in the comments to one of the answers above (but which i didn't notice at first because it was buried amongst one of the "additional comments") …
we simply run progruard on the command line (see the first block below) on the library outside of eclipse with the parameters in the second block below in our our
proguard.cfg
file (and definitely do not use-dontpreverify
, or projects that use your project as an android library won't be able to properly be obfuscated due to StackMapTable problems from your project.jar).command line:
proguard.cfg:
it's possible not all of the parameters and -keep items are strictly necessary (other than not using
-dontpreverify
as previously mentioned), but most of these make sense to me as items that need to be there if you have an Activity class extension in the library you are exporting.