Android程序加入代码混淆器

版权所有,禁止匿名转载;禁止商业使用。


加入代码混淆器,主要是加入 proguard-project.txt 文件的规则进行混淆,之前新建 Android 程序是 proguard.cfg 文件


可以看一下我采用的通用规则( proguard-project.txt 文件)

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

# 以下两个命令配合让类的路径给删除了
-allowaccessmodification
-repackageclasses ”

# 记录生成的日志数据,在 proguard 目录下
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt

# 异常都可以忽略就打开
#-dontwarn
-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
-keepnames class * implements java.io.Serializable

# 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 ;
#}
#-keepclasseswithmembernames class * {
#	public (android.content.Context, android.util.AttributeSet);
#}
#-keepclasseswithmembernames class * {
#	public (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 ;
#}

# Preserve the special static methods that are required in all enumeration classes.
-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 public class * {
# public protected *;
#}

如果还有规则就加在后面


另外,我们一般需要混淆的是自己写的代码,引入的第三方库文件是不需要混淆的,否则会出现如下错误:

YRj2Mb.png


就是找不到所需要的类。所以第三方库需要指出来不混淆,方法如下:


上面是android.support.v4.×找不到所需类,查看引入库的包名

RRJfUf.png


在proguard-project.txt 后面加上

-dontwarn android.support.v4.** 
-keep class android.support.v4.** { *; }

第一句是忽略这个警告,第二句是保持类不混淆


把你所有引入的第三方库弄完后,在 project.properties 文件里加上一句

proguard.config=proguard-project.txt

就可以编译了(怎么编译? )


编译后,可以在 bin 里看到混淆代码的 apk 文件,是不是文件变小了,多爽!


同时,在 bin/proguard 文件夹里多了几个文件


ZJV3ui.png

其中,mapping.txt 就是所有混淆的类及变量名的前后参照, usage.txt 是你在代码中没用到的方法,都给你列出来了,是优化后的结果。


0 0