android混淆打包經驗分享!

yangxi_001發表於2013-11-15
以下為我此期專案中的關於混淆打包的總結:

(本人第一次混淆打包,呵呵,錯誤很多!列了一些比較頭疼的)

一、專案混淆過程中注意事項:

由於我的sdk版本較高,因此新建android專案下只有proguard-project.txt和project.properties這兩個資料夾,而網上一些所謂混淆的方法我均試驗了下,都有或多或少的問題,以下是一些混淆總結:

1、如果你的專案沒有其他第三方包的話,那麼進行混淆很簡單,只需要將project.properties資料夾下面的註釋解開就行,一點區別在於:如果您是2.3之前的sdk版本,那麼就用這個proguard.config=proguard.cfg

如果是之後的則為:proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt(當然視您生成專案時候該檔案具體生成情況所定)。

2、如果有第三方lib包的話,則混淆時需要注意了,以下是常用的一些lib包的混淆配置:

1)、友盟sdk:

-libraryjars libs/umeng_sdk.jar

-keepclassmembers class * {

public (org.json.JSONObject);

}

-keep public class [您的應用程式名].R$*{

public static final int ;

}

2)、gson

-ibraryjars libs/gson-2.2.2.jar

-keep class sun.misc.Unsafe { *; } 

-keep class com.google.gson.stream.* { ; } 

-keep class com.google.gson.examples.android.model.* { ; } 

-keep class com.google.gson.* { *;}

3)、support-v4包

-libraryjars libs/android-support-v4.jar

-dontwarn android.support.v4.**



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



4)、nineoldandroids動畫lib包

-libraryjars libs/nineoldandroids-2.4.0.jar

-dontwarn com.nineoldandroids.*

-keep class com.nineoldandroids.** { *;}

註解:

-braryjars libs/nineoldandroids-2.4.0.jar----指明lib包的在工程中的路徑

而-dontwarn com.xx.bbb.**和-keep class com.xx.bbb.** { ;}

這兩個引數用來保持第三方庫中的類而不亂,將-dontwarn和-keep 結合使用,意思是保持com.xx.bbb.*這個包裡面的所有類和所有方法而不混淆,接著還叫ProGuard不要警告找不到com.xx.bbb.**這個包裡面的類的相關引用。

具體介紹參考:http://blog.sina.com.cn/s/blog_644c92c90101o3f8.html

3、本人採用的方式為:直接將專案生成的proguard-project.txt刪掉,然後直接找原來2.3版本生成的proguard.cfg複製到專案中來,以下為我兩個混淆檔案的配置:(為了讓大家看的更加清楚,原文貼上

1)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.

proguard.config=proguard.cfg
target=android-11

2)proguard.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 ;

}

-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);

}

-keepclassmembers enum * {

public static **[] values();

public static ** valueOf(java.lang.String);

}

-keep class * implements android.os.Parcelable {

public static final android.os.Parcelable$Creator *;

}

-libraryjars libs/umeng_sdk.jar

-keepclassmembers class * {

public (org.json.JSONObject);

}

-keep public class com.smile.android.R$*{

public static final int *;

}

-libraryjars libs/gson-2.2.2.jar

-keep class sun.misc.Unsafe { ; } 

-keep class com.google.gson.stream.* { ; } 

-keep class com.google.gson.examples.android.model.* { ; } 

-keep class com.google.gson.* { *;}

-libraryjars libs/android-support-v4.jar

-dontwarn android.support.v4.**



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

-libraryjars /libs/nineoldandroids-2.4.0.jar

-dontwarn com.nineoldandroids.**

-keep class com.nineoldandroids.** { *;}

注:(對呀lib包的混淆配置,一般放到此檔案的最後面!注意紅字部分)

二、混淆打包後的錯誤收集:

1、如果專案中含有資料庫並且資料庫的建立方式為通過實現BaseColumns類來依次建立資料庫表的話,那麼在混淆之後會出現no such table這類的錯誤,這個錯誤困擾我一晚上了,實在想不通,後面覺得應該是混淆過程中將包名混淆了導致編譯器找不到該表。

解決辦法:換資料庫的建立方式:直接在繼承自SQLiteOpenHelper中執行表格建立即:db.execSQL(DB_CREAT);這樣一來就不會出錯了!很奇葩的解決方法,還是迴歸原始比較好!

2、專案中libs包中有gson的話,混淆後出現java.lang.IllegalArgumentException: class com.smile.android.open.c.a declares multiple JSON fields named a

at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Unknown Source)

at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(Unknown Source)

經過一番搜尋:此譯文說的是我的實體物件類中有重名的變數名,後面發覺是不是我此物件有太多巢狀的原因,試了下將父類物件的變數全部移植到子類中,再重新混淆打包編譯,發現解決了!哈哈!哎!同樣苦逼的問題!

看來以後實體物件還是不要有太多的巢狀了!

相關文章