Proguard进行Android代码混淆

jopen 9年前

在实际的开发成产品apk的发布过程中,我们经常会使用到代码混淆技术。不过在讲解如何进行代码混淆之前,我先跟大家演示一下反编译的过程,这样才能让大家明白为什么需要进行代码混淆。 

 

一、代码反编译 

1.准备工作 

首先我们准备好一个项目的apk文件(未进行混淆打包)、dex2jar工具(下载地址:http://code.google.com/p/dex2jar/)、jd-gui工具(下载地址http://jd-gui.softpedia.com/)。 

Proguard进行Android代码混淆

Proguard进行Android代码混淆

 

2.获取CodeMixTest.apk中的classes.dex文件 

将CodeMixTest.apk解压,这里我们为了方便就直接解压到当前文件夹下。在解压出的文件中,我们可以看见有一个classes.dex文件,这就是我们的源代码打包生成的文件。 

Proguard进行Android代码混淆

 

3.使用dex2jar工具还原jar文件 

dex2jar工具的作用就是将class.dex文件还原成.jar文件。先将dex2jar-0.0.9.15.zip解压,然后将上面获得的classes.dex文件拷贝至解压的目录中。 

Proguard进行Android代码混淆

查看解压目录中,有dex2jar.bat文件。 

Proguard进行Android代码混淆

启动cmd,进入到dex2jar工具解压的目录中,运行命令:dex2jar.bat classes.dex,有如下结果 

Proguard进行Android代码混淆

查看dex2jar工具解压的目录中,生成了我们想要得到的classes_dex2jar.jar文件。 

Proguard进行Android代码混淆

 

4.使用jd-gui工具查看jar文件 

jd-gui-0.3.5.windows.zip工具解压,为了方便我们直接解压到当前文件夹,我们会看见一个jd-gui.exe文件。 

Proguard进行Android代码混淆

双击jd-gui.exe,使用工具打开前面我们获得的classes_dex2jar.jar文件。可以清楚的看见源代码。 

Proguard进行Android代码混淆

 

是企业真实的项目中,如果不经过代码混淆,你的源码就如此赤裸裸的暴露着。这样对于公司来说是非常不安全的,并且不想发生的情况。既然我们已经看见了反编译如此的威力,那么我们有什么办法可以解决这个问题呢?看下面讲解。 

 

二、代码混淆 

1.准备工作 

首先,我们得将我们的项目源码、proguard工具(放心这个工具Google已经在sdk中为我们提供了,在sdk\tools目录下可以看到)、 proguard.cfg文件(该文件定义了混淆的规则,创建2.3以上项目自动生成,或者在网上可以很容易下载到)。 

Proguard进行Android代码混淆

 

2.在你的项目中添加proguard.cfg文件 

该文件Google公司已经为我们准备好了,创建项目时,在项目的根目录下就有了,但是是从2.3开始的。不过我们基于2.2的项目只需要将该文件拷贝到项目根目录下即可。 

Proguard进行Android代码混淆

 

3.修改项目的default.properties配置文件 

Android项目默认在打包的时候是不混淆代码的,所以我们必须得在default.properties文件中,添加该配置:proguard.config=proguard.cfg 

Proguard进行Android代码混淆


4.进行apk打包 

好了,一切就绪,接下来我们就按着平常的打包步骤,进行打包操作。打完包后,我们使用上面讲述的反编译方法,查看现在的jar文件 

Proguard进行Android代码混淆

会发现,混淆了许多aa、bb、cc的包、aa、bb、cc的类和方法,这样的代码根本无法阅读,也就达到了我们保护源代码的目的了 

 

三、produard.cfg文件 

我们在看看produard.cfg文件,该文件中描述了混淆代码的规则,你如果想根据自己的需求来混淆代码,就可以在本文件中编写你的混淆规则。一般情况下,该文件的内容如下: 

    -optimizationpasses 5           -dontusemixedcaseclassnames//混淆时不会产生形形色色的类名           -dontskipnonpubliclibraryclasses//指定不去忽略非公共的类库           -dontpreverify//不预校验           -verbose           -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*//优化                      //不进行混淆保持原样           -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                      //保护指定的类和类的成员的名称,如果所有指定的类成员出席           -keepclasseswithmembernames class * {               native <methods>;           }                      //保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在           -keepclasseswithmembers class * {               public <init>(android.content.Context, android.util.AttributeSet);           }                                 -keepclasseswithmembers class * {               public <init>(android.content.Context, android.util.AttributeSet, int);           }                      //保护指定类的成员,如果此类受到保护他们会保护的更好           -keepclassmembers class * extends android.app.Activity {              public void *(android.view.View);           }                      -keepclassmembers enum * {               public static **[] values();               public static ** valueOf(java.lang.String);           }                      //保护指定的类文件和类成员           -keep class * implements android.os.Parcelable {             public static final android.os.Parcelable$Creator *;           }   

四、可能出现的问题 

1.因为使用了升级后使用android-support-v4而发生混淆错误,无法打包出现如下错误: 

Unexpected error while evaluating instruction:      Class       = [android/support/v4/view/AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl]      Method      = [newAccessiblityDelegateBridge(Landroid/support/v4/view/AccessibilityDelegateCompat;)Ljava/lang/Object;]   Instruction = [18] areturn    Exception   = 1 (Can't find any super classes of [android/support/v4/view/AccessibilityDelegateCompatIcs$1] (not even immediate super class [android/view/View$AccessibilityDelegate]))    Unexpected error while performing partial evaluation:      Class       = [android/support/v4/view/AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl]     Method      = [newAccessiblityDelegateBridge(Landroid/support/v4/view/AccessibilityDelegateCompat;)Ljava/lang/Object;]      Exception   = 1 (Can't find any super classes of [android/support/v4/view/AccessibilityDelegateCompatIcs$1] (not even immediate super class [android/view/View$AccessibilityDelegate]))   java.lang.IllegalArgumentException: Can't find any super classes of [android/support/v4/view/AccessibilityDelegateCompatIcs$1] (not even immediate super class [android/view/View$AccessibilityDelegate])      at proguard.evaluation.value.ReferenceValue.generalize(ReferenceValue.java:287) 

你需要在proguard.cfg文件中添加如下代码 

-libraryjars   libs/android-support-v4.jar 
-dontwarn android.support.v4.**     
-keep class android.support.v4.** { *; }   
-keep interface android.support.v4.app.** { *; }   
-keep public class * extends android.support.v4.**   
-keep public class * extends android.app.Fragment 

 

2.如果你使用了第三方的包,你需要使用一下配置,让ProGuard知道库中的一些类并不是在所有的API中可用: 

-libraryjars   libs/roboguice-2.0.jar 

-dontwarn roboguice.**  

 

3.在打包的过程中,出现如下错误: 

Proguard returned with error code 1. See console 
[2012-09-25 09:50:39 - estore3601] Error: @C :\Users\hp\AppData\Local\Temp\android_5192919586673521494.pro (文件名、目录名或卷标语法不正确。) 

你需要系在ProGuard最新版本,如v4.8。然后解压produard4.8.zip文件,将bin和lib两个文件夹覆盖sdk目录\tool\proguard目录中同名的文件夹