使用 Proguard 混淆 ActionBarSherlock

Posted

技术标签:

【中文标题】使用 Proguard 混淆 ActionBarSherlock【英文标题】:Obfuscating ActionBarSherlock with Proguard 【发布时间】:2012-06-24 11:05:56 【问题描述】:

我正在尝试使用 proguardant 混淆我的 android 应用程序(eclipse + proguard 永远无法工作,总是以 error 1 失败)。但是我收到了class file ... unexpectedly contains class ... 警告,并且构建失败。我还尝试使用proguard 中的-ignorewarnings 选项。构建成功,但我得到了一个无法使用的包。

这是蚂蚁的输出:

-post-compile:

optimize:
     [jar] Building jar: /home/binoy/git/Vibhinna/proguard/unoptimized.jar
     [java] ProGuard, version 4.8
     [java] Reading input...
     [java] Warning: class [classes/com/actionbarsherlock/R$array.class] unexpectedly contains class [com.actionbarsherlock.R$array]
     [java] Warning: class [classes/com/actionbarsherlock/R$attr.class] unexpectedly contains class [com.actionbarsherlock.R$attr]
     [java] Warning: class [classes/com/actionbarsherlock/R$bool.class] unexpectedly contains class [com.actionbarsherlock.R$bool]
     [java] Warning: class [classes/com/actionbarsherlock/R$color.class] unexpectedly contains class [com.actionbarsherlock.R$color]
     ....
     [java] Warning: class [classes/com/actionbarsherlock/R$xml.class] unexpectedly contains class [com.actionbarsherlock.R$xml]
     [java] Warning: class [classes/com/actionbarsherlock/R.class] unexpectedly contains class [com.actionbarsherlock.R].
     [java] Warning: class [classes/com/vibhinna/binoy/DetailsDialogFragment$1.class] unexpectedly contains class [com.vibhinna.binoy.DetailsDialogFragment$1]
     [java] Warning: class [classes/com/vibhinna/binoy/DetailsDialogFragment.class] unexpectedly contains class [com.vibhinna.binoy.DetailsDialogFragment]
     [java] Warning: class [classes/com/vibhinna/binoy/HoloAlertDialogBuilder.class] unexpectedly contains class [com.vibhinna.binoy.HoloAlertDialogBuilder]
     [java] Warning: class [classes/com/vibhinna/binoy/MiscMethods.class] unexpectedly contains class [com.vibhinna.binoy.MiscMethods]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$1$1.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$1$1]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$1$2.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$1$2]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$1.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$1]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$10.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$10]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$11.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$11]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$2.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$2]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$3.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$3]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$4.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$4]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$5.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$5]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$6.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$6]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$7.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$7]
     [java] Warning: class [classes/com/vibhinna/binoy/NewDialogFragment$8.class] unexpectedly contains class [com.vibhinna.binoy.NewDialogFragment$8]
     ....
     [java] Warning: class [classes/com/vibhinna/binoy/Preferences.class] unexpectedly contains class [com.vibhinna.binoy.Preferences]
     [java] Warning: class [classes/com/vibhinna/binoy/ProcessManager.class] unexpectedly contains class [com.vibhinna.binoy.ProcessManager]
     [java] Warning: there were 89 classes in incorrectly named files.
     [java]          You should make sure all file names correspond to their class names.
     [java]          The directory hierarchies must correspond to the package hierarchies.
     [java]          If you don't mind the mentioned classes not being written out,
     [java]          you could try your luck using the '-ignorewarnings' option.
     [java] java.io.IOException: Please correct the above warnings first.
     [java]     at proguard.InputReader.execute(InputReader.java:133)
     [java]     at proguard.ProGuard.readInput(ProGuard.java:197)
     [java]     at proguard.ProGuard.execute(ProGuard.java:79)
     [java]     at proguard.ProGuard.main(ProGuard.java:493)

我该如何解决这个问题?

【问题讨论】:

actionbarsherlock.com/faq.html(参见使用 proguard 部分) 【参考方案1】:

确保您的 proguard 配置文件具有 ActionbarSherlock 的 recommended exclusions:

-keep class android.support.v4.app.**  *;  
-keep interface android.support.v4.app.**  *;  
-keep class com.actionbarsherlock.**  *;  
-keep interface com.actionbarsherlock.**  *;  
-keepattributes *Annotation*

也尝试添加

-keepclassmembers class **.R$* 
    public static <fields>;

按照here的建议。

【讨论】:

仍然没有变化,仍然有 89 个类在错误命名的文件中。 也尝试添加-keepclassmembers class **.R$* public static &lt;fields&gt;; 确保将 unoptimized.jar 添加为库 jar,如果它在您的项目外部:-libraryjars /home/binoy/git/Vibhinna/proguard/unoptimized.jar 到配置文件 不是库jar,是ant创建的。【参考方案2】:

参照。 ProGuard 手册 > 疑难解答 > Warning: class file ... unexpectedly contains class ...

您应该在输入类路径中指定正确的基目录:“-injars ....../classes”。

【讨论】:

以上是关于使用 Proguard 混淆 ActionBarSherlock的主要内容,如果未能解决你的问题,请参考以下文章

防止 Proguard 使用注释混淆子类

使用 ProGuard 混淆私有字段

Spring boot使用ProGuard实现代码混淆

使用 proguard 的 Android 混淆应用程序会不断混淆库 jar - 是吗?

为啥 proguard 不混淆方法体?

如何使用 proguard 混淆选择性课程?