Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android混淆打包

Android混淆打包

編輯:關於Android編程

在這之前,看了下proguard官網介紹,並搜了下相關資料。


ProGuard簡介

ProGuard是一個SourceForge上非常知名的開源項目。官網網址是:http://proguard.sourceforge.net/。

Java的字節碼一般是非常容易反編譯的。為了很好的保護Java源代碼,我們往往會對編譯好的class文件進行混淆處理。ProGuard的主要作用就是混淆。當然它還能對字節碼進行縮減體積、優化等,但那些對於我們來說都算是次要的功能。


引用ProGuard官方的一段話來介紹就是:

ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.


Android Eclipse開發環境與ProGuard

在Android 2.3以前,混淆Android代碼只能手動添加proguard來實現代碼混淆,非常不方便。而2.3以後,Google已經將這個工具加入到了SDK的工具集裡。具體路徑:SDK\tools\proguard。當創建一個新的Android工程時,在工程目錄的根路徑下,會出現一個proguard的配置文件proguard.cfg。也就是說,我們可以通過簡單的配置,在我們的elipse工程中直接使用ProGuard混淆Android工程。 集成的ADT現在創建一個新的Android工程時,在工程目錄的根路徑下、會有一個proguard-project.txt。其實這個文件跟proguard.cfg是一樣的。混淆打包的一些配置寫在這個文件裡就行了。

搜了下Proguard混淆打包相關信息,搜來搜去都說的都大同小異,只要懂得了基本的一些使用方法,哪些混淆,哪些不混淆,最常見的就是過濾掉一些android需要注冊的一些組件不混淆,第三方包也不需要混淆,因為有的第三方包已經混淆過了,大致的按照自己需要混淆的需求,寫個基本配置就行了。在這裡我做了個基本的綜合、proguard配置如下:

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

-keepclasseswithmembers class * {
    public (android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public (android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity { 
    public void *(android.view.View);
}

-keep public class * extends android.view.View {
    public (android.content.Context);
    public (android.content.Context, android.util.AttributeSet);
    public (android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient ;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

-keepattributes Signature

-keepattributes *Annotation*

-keep class **.R$* { *; }


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



它主要保留了繼承自Activity、Application、Service、BroadcastReceiver、ContentProvider、BackupAgentHelper、Preference和ILicensingService的子類。因為這些子類,都是可能被外部調用的。

另外,它還保留了含有native方法的類、構造函數從xml構造的類(一般為View的子類)、枚舉類型中的values和valueOf靜態方法、繼承Parcelable的跨進程數據類和實現Serializable對象序列化。

若有其他第三方包,可依依添加不混淆配置就行了。可根據要求,去個性配置proguard混淆就行了。


最後記住把根目錄路徑下的project.properties文件:

# 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-17


proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

紅色的那句注釋(去掉“#”號)的打開放在最下面,就可以簽名混淆打包apk去了。




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved