如何在 Eclipse 中使用 Proguard 混淆 Android 库(.jar 文件) [英] How to obfuscate an Android library (.jar file) using Proguard in Eclipse

查看:36
本文介绍了如何在 Eclipse 中使用 Proguard 混淆 Android 库(.jar 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于如何在 Eclipse 中使用 ProGuard 混淆 Android 应用程序(.apk 文件)的帖子.另见http://developer.android.com/guide/developing/tools/proguard.html:

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:

当您在发布模式下构建应用程序时,无论是通过运行 ant release 还是使用 Eclipse 中的导出向导,构建系统都会自动检查是否设置了 proguard.config 属性.如果是,ProGuard 会在将所有内容打包到.apk 文件之前自动处理应用程序的字节码."

"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."

但如果使用 Eclipse 导出向导将 Android 项目导出为 .jar 文件,请按照所述步骤(创建文件 proguard.cfg,将 proguard.config 属性配置为文件 default.properties 中的 proguard.cfg,使用导出向导等)似乎不起作用 - 我在生成的 jar 文件中没有看到类名等的混淆.我的 proguard.cfg 文件中也有以下设置,但我在项目目录或 proguard 目录(甚至没有创建该目录)中看不到任何输出文件.

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

我什至在我的项目目录中使用以下行创建了一个文件 project.properties 但这似乎并没有吸引 ProGuard 采取行动:

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

此项目中未定义任何活动.我在 Windows 上使用 Android 2.3.1 和 Eclipse Galileo 3.5.2.与 Android 3.0 的结果相同.似乎混淆步骤必须以某种方式显式插入 Eclipse 导出向导中.我将不胜感激任何帮助或见解.谢谢.

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.

推荐答案

如对上述答案之一的评论中所建议的(但我一开始没有注意到,因为它被埋在其中一个附加评论"中) ...

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") …

我们只需在 eclipse 之外的库中的命令行上运行 progruard(请参阅下面的第一个块),并在我们的 proguard.cfg 文件(并且绝对不要使用 -dontpreverify,或者由于 StackMapTable 问题,将您的项目用作 Android 库的项目将无法正确混淆你的 project.jar).

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).

命令行:

$ java -jar $ANDROID_SDK/tools/proguard/lib/proguard.jar 
  -libraryjars $ANDROID_SDK/platforms/android-18/android.jar @proguard.cfg
  -outjars /tmp/project.jar -verbose

proguard.cfg:

proguard.cfg:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontwarn our.company.project.R*
-injars bin/project.jar
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep class org.apache.3rdparty.stuff.**
-keep public class our.company.project.ProjectAPI
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference

-keepclassmembers public class our.company.project.ProjectAPI {
    public static <fields>;
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

可能并非所有的参数和 -keep 项都是绝对必要的(除了不使用 -dontpreverify 如前所述),但其中大部分对我来说都是有意义的,因为这些项目需要如果您要导出的库中有 Activity 类扩展.

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.

这篇关于如何在 Eclipse 中使用 Proguard 混淆 Android 库(.jar 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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