关于android代码混淆

代码混淆的必要性,想必大家都是心知肚明的。能看到这篇文章,说明对代码混淆的逻辑、方式都是很熟悉的,在这里就不再从0介绍了。

下面只说在使用混淆工具proguard几个常见问题和解决方法:

1、关于反射

  反射所关心的是类和类的字段(field)的名称(Stirng形式),所以使用到反射的实体类,应避免混淆。一一设置字段的方式个人觉得没必要。

  a、保留指定类
    -keep class com.yourpkg.model.yourcls{
    *;
    }

  b、保留指定包下的所有类
    -keep class com.yourpkg.model.**{
    *;
    }

2、关于Gson和TypeToken

  这个问题困扰了我好几天,最终在Stack Overflow找到解决方法,我是个不求甚解的人,至少目前是。求甚解是个成本很高的活,对于我这样没什么成本的人,遇到问题只要能解决就OK了。

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature


# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }


# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }


##---------------End: proguard configuration for Gson  ----------

3、关于第三方jar包

  项目中经常引用第三方jar,这些jar如果参与混淆,则可能导致未知错误,所以第三方jar包要排除。

  -libraryjars libs/第三方.jar

  有些第三方jar包可能并不是直接引用的,这种情况只需要使用jar包的绝对路径即可。

 

最后提供一个Stack Overflow上找到的通用混淆配置,每条代码具体什么意思,相信大家都会找度娘或者API,排版水平太差,多多包涵。

##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt 
-printseeds seeds.txt 
-printusage unused.txt 
-printmapping mapping.txt 
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*


-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''


-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
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService


# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-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();
}


# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
    native <methods>;
}


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


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


# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
  public static <fields>;
}


# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}


-keep public class * {
    public protected *;
}


-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------

posted @ 2015-01-06 15:52  toronto  阅读(288)  评论(0编辑  收藏  举报