Android& Proguard - 如何混淆,但不优化任何代码?

时间:2012-05-11 15:52:19

标签: android proguard

我想使用Proguard来混淆应用程序的代码。我不需要优化,我也不需要Proguard去除任何类或方法。我想要的只是混淆。该应用程序使用多个库项目。

我一直在撞墙试图让它发生,而且它不按我想要的方式工作。我在应用程序中看到NoSuchMethodExceptions在执行时被抛出,即使我认为我已经关闭了Proguard的缩小选项。

有什么神奇的设置让Proguard JUST混淆,而不是优化掉任何代码?

更新。我通过反复试验证实,导致NoSuchMethodExceptions的是混淆过程(不是优化或收缩)。

Proguard.cfg

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*
-dontshrink
-keep public class * extends Object
-keep class com.myapp.** { *; }
-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 com.myapp.activity.Splash
-keep public class com.myapp.alarm.AlarmsViewer
-keep public class com.myapp.activity.About
-keep public class com.myapp.activity.Base
-keep public class com.myapp.activity.BaseWithMenu
-keep public class com.myapp.alarm.Alarm
-keep public class com.myapp.alarm.AlarmFragment
-keep public class com.myapp.alarm.AlarmPagerAdapter
-keep public class com.myapp.alarm.AlarmStore
-keep public class com.myapp.app.App
-keep public class com.myapp.preferences.Preferences
-keep public class com.myapp.preferences.PreferencesStore

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

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

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

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

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

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }

-keepattributes *Annotation*

-keep public interface com.android.vending.licensing.ILicensingService

-dontwarn android.support.**

2 个答案:

答案 0 :(得分:6)

我有一段时间遇到过类似的问题,并通过蛮力和运气为我解决了这个问题。我的proguard.cfg很相似,但我有一行:

-dontshrink
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

我不记得我从哪里得到了这些优化选项的想法,但它们似乎对我有用。

始终有catch all switch

-dontoptimize

(指定不优化输入类文件。默认情况下,启用优化;所有方法都在字节码级别进行优化。)

可能更合适。

最后,我的方法仅在xml文件(点击处理程序)中引用,需要明确保存

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

答案 1 :(得分:0)

**#Retrofit**
-keep class retrofit.** { ; }

-keepattributes Signature

-keepattributes Exceptions

-keepclasseswithmembers class * {
@retrofit2.http. ;
}

-keepclasseswithmembers interface * {
@retrofit2.http.* ;
}

-dontwarn javax.annotation.Nullable

-dontwarn javax.annotation.ParametersAreNonnullByDefault

-dontobfuscate  - only in debug mode production mode dont use this

-dontoptimize

-dontshrink

Please use in this lines Dexguard-project.txt file. that's all. Enjoy your coding...