Android代码混淆

 混淆器(ProGuard)
---

混淆器通过删除从未用过的代码和使用晦涩名字重命名类、字段和方法,对代码进行压缩,优化和混淆。结果是一个比較小的.apk文件,该文件比較难进行逆向project。因此,当你的应用程序对安全敏感(要求高),比如当你授权应用程序的时候,混淆器是一种重要的保护手段。    
混淆器被集成在android 构建系统中,所以你不必手动调用它。同一时候混淆器仅在公布模式下进行构建应用程序的时候才会执行起来,所以在调试模式下构建程序时,你不必处理混淆代码。让混淆器执行起来是可选择的,可是推荐选上。

Android项目中的混淆很easy,之所以写这篇总结是由于近期发现公司的代码居然没有混淆,反编译后代码随手可得。很震惊。

1. 改动project.properties
```xml
    # This file is automatically generated by Android Tools.
    # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
    #
    # This file must be checked in Version Control Systems.
    #
    # To customize properties used by the Ant build system edit
    # "ant.properties", and override values to adapt the script to your
    # project structure.
    #
    # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
    #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

    # Project target.
    target=android-19
```
    将proguard.config前面的凝视去掉

2. 改动proguard-project.txt
```xml
    # To enable ProGuard in your project, edit project.properties
    # to define the proguard.config property as described in that file.
    #
    # Add project specific ProGuard rules here.
    # By default, the flags in this file are appended to flags specified
    # in ${sdk.dir}/tools/proguard/proguard-android.txt
    # You can edit the include path and order by changing the ProGuard
    # include property in project.properties.
    #
    # For more details, see
    #   http://developer.android.com/guide/developing/tools/proguard.html

    # Add any project specific keep options here:

    # If your project uses WebView with JS, uncomment the following
    # and specify the fully qualified class name to the JavaScript interface
    # class:
    #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
    #   public *;
    #}
```
    假设在程序中使用了第三方的`jar`包,在混淆后导致出错,这时我们须要在proguard-project.txt中去进行对应的配置,来让其在混淆时不要混淆对应的jar包。对改配置文件里的相关配置解释例如以下:
```java
    -keep public class * extends android.app.Activity  【不进行混淆类名的类,保持其原类名和包名】

    -keep public abstract interface com.asqw.android.Listener{
    public protected <methods>;  【全部public protected的方法名不进行混淆】
    }
    -keep public class com.asqw.android{
    public void Start(java.lang.String); 【对该方法不进行混淆】
    }
    -keepclasseswithmembernames class * { 【对全部类的native方法名不进行混淆】
    native <methods>;
    }
    -keepclasseswithmembers class * { 【对全部类的指定方法的方法名不进行混淆】
    public <init>(android.content.Context, android.util.AttributeSet);
    }
    -keepclassmembers class * extends android.app.Activity {【对全部类的指定方法的方法名不进行混淆】
    public void *(android.view.View);
    }
    -keepclassmembers enum * {【对枚举类型enum的全部类的下面指定方法的方法名不进行混淆】
    public static **[] values();
    public static ** valueOf(java.lang.String);
    }
    -keep class * implements android.os.Parcelable {【对实现了Parcelable接口的全部类的类名不进行混淆,对其成员变量为Parcelable$Creator类型的成员变量的变量名不进行混淆】
    public static final android.os.Parcelable$Creator *;
    }
    -keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue {【对指定类的指定变量的变量名不进行混淆】
        volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node head;
        volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail;
        volatile transient int sweepVotes;

    }
    -keep public class com.unionpay.** {*; }【对com.unionpay包下全部的类都不进行混淆,即不混淆类名,也不混淆方法名和变量名】
```        

经过上面这两部之后反编译后就能混淆了,可是四大组件还在,为什么四大组件还在呢,由于四大组件是在清单文件里进行配置的,假设混淆后就不能依据清单文件的配置去寻找了。     
假设对于一些自己的代码中要想提供出来让别人通过反射调用的方法时,我们不想让部分代码被混淆,或者是我们使用别人提供的第三方jar包,由于第三方的jar包一般都是已经混淆过的,我们要是再混淆就会报错了,所以我们要保证这些内容不用混淆,这里我们仅仅需改动这个文件,然后加上后面的一句话,他就不会混淆我们给出的内容    
```xml
-keepattributes *Annotation*          
-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.BackupAgent
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.itheima.mobilesafe.engine.AppInfoProvider
-keep class net.youmi.android.** {
*;
}

Android代码混淆,布布扣,bubuko.com

Android代码混淆

上一篇:玩转JPA(一)---异常:Repeated column in mapping for entity/should be mapped with insert="false" update="fal


下一篇:android adb 发送keyEvent