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 *;
}

# 假设你的project是对外提供方法调用就打开
#-keep public class * {
# public protected *;
#}

假设还有规则就加在后面

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


就是找不到所须要的类。

所以第三方库须要指出来不混淆,方法例如以下:

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


在proguard-project.txt后面加上

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

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

把你全部引入的第三方库弄完后,在project.properties文件中加上一句

proguard.config=proguard-project.txt

就能够编译了(怎么编译?

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

同一时候。在bin/proguard目录里多了几个文件



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



原文地址:https://www.cnblogs.com/blfbuaa/p/6700356.html