Android混淆打包

來源:互聯網
上載者:User

在這之前,看了下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去了。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.