且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-26 09:31:58

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

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中使用下面第二个块中的参数code> 文件(并且绝对不要使用 -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.