如何在 Eclipse 中使用 Proguard 混淆 Android 库(.jar 文件)

发布于 2024-12-14 17:42:12 字数 1054 浏览 0 评论 0原文

我看过很多关于如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

智商已欠费 2024-12-21 17:42:27

不要混淆您的纯 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.

帅哥哥的热头脑 2024-12-21 17:42:24

调用 ProGuard 的方法相当简单:

  1. 将行 proguard.config=proguard.cfg 添加到 project.properties
  2. 导出应用程序包

默认的 proguard.cfg 文件应该已由新项目向导自动创建。

The way to invoke ProGuard is fairly straightforward:

  1. Add the line proguard.config=proguard.cfg to project.properties
  2. Export the application package

A default proguard.cfg file should have been automatically created by the new project wizard.

枯寂 2024-12-21 17:42:19

我使用间接的方式生成导出的android混淆jar,我的方式是:

  1. 使用eclipse导出签名的apk

  2. 解压apk,找到classes.dex

  3. 使用dex2jar.bat,将classes.dex改为jar

  4. 解压jar 并删除不需要的类,然后将其压缩并
    将文件名更改为XXX.jar

  5. 然后你在其他项目中使用这个jar,或者将其交给客户,它
    是混淆的!

我相信这会对您有所帮助!好好享受!

I use an indirect way to generate a exported android obfuscate jar, my way is:

  1. export a signed apk use eclipse

  2. unzip the apk, find the classes.dex

  3. use dex2jar.bat ,change the classes.dex to a jar

  4. unzip the jar and delete the class you do not need,then zip it and
    change the file name to XXX.jar

  5. 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!

心如狂蝶 2024-12-21 17:42:19
java -jar proguard.jar @yourconfig.pro

yourconfig.pro(扩展自 http://proguard.sourceforge.net/index.html#manual/examples。 html):

-injars yourjar.jar
-outjars yourjar_out.jar

-libraryjars 'C:\android\sdk\platforms\android-10\android.jar'

-printmapping mapping.txt
-verbose
-dontoptimize
-dontpreverify
-dontshrink
-dontskipnonpubliclibraryclassmembers
-dontusemixedcaseclassnames
-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * {
    public protected *;
}

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

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

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

可以使用 jd-gui 验证结果

java -jar proguard.jar @yourconfig.pro

yourconfig.pro (extended from http://proguard.sourceforge.net/index.html#manual/examples.html):

-injars yourjar.jar
-outjars yourjar_out.jar

-libraryjars 'C:\android\sdk\platforms\android-10\android.jar'

-printmapping mapping.txt
-verbose
-dontoptimize
-dontpreverify
-dontshrink
-dontskipnonpubliclibraryclassmembers
-dontusemixedcaseclassnames
-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * {
    public protected *;
}

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

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

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

Result can be verified with jd-gui

残月升风 2024-12-21 17:42:17

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

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

命令行:

$ 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:

-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 类扩展,则需要在那里。

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:

$ 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:

-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 *;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文