Android項目混淆打包


以下為我此期項目中的關於混淆打包的總結:
(本人第一次混淆打包,呵呵,錯誤很多!列了一些比較頭疼的)
一、項目混淆過程中注意事項:
由於我的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)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 yourproject 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.txtProject 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 *;
}
 

-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);這樣一來就不會出錯了!很奇葩的解決方法,還是回歸原始比較好!
 

1.引入默認的打包配置
拷貝${sdk.dir}/tools/proguard/proguard-android.txt文件中的配置到你的工程的proguard-android.txt中
如下所示
#系統配置
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

2.第三方jar包的配置
1)fastjson配置
#fast json相關
-libraryjars libs/json.jar                                 #fastjson的jar包不要混淆
-keep class com.alibaba.fastjson.** { *; }      #fastjson包下的所有類不要混淆,包括類里面的方法
-keepattributes Signature                            #這行一定要加上,否則你的object中含有其他對象的字段的時候會拋出ClassCastException

-dontwarn com.alibaba.fastjson.**              #告訴編譯器fastjson打包過程中不要提示警告
2)xutils配置
#xutils相關
-libraryjars libs/xUtils-2.4.7.jar                      #xutils的jar包不要混淆
-keep class com.lidroid.** { *; }                     #xutils包下所有類不要混淆,包括類里面的方法
-keep class * extends java.lang.annotation.Annotation { *; }#注解包下的所有內容不要混淆,ViewUitls有用到

3)新浪微博配置
-libraryjars libs/weibosdkcore.jar                  #微博jar包不要混淆
-keep class com.sina.weibo.sdk.** { *; }         #微博報下所有類及類里面的內容都不要混淆

4)騰訊開放平台配置
-libraryjars libs/mta-sdk-1.6.2.jar                   #騰訊平台jar包不要混淆
-keep class com.tencent.** { *; }                     #騰訊平台jar包中所有類及類里面的內容不要混淆

5)圖片異步加載組件universal-image-loader配置
#圖片加載
-libraryjars libs/universal-image-loader-1.8.4-with-sources.jar  #imageLoader的jar包不要混淆
-keep class com.nostra13.universalimageloader.** { *; }              #imageLoader包下所有類及類里面的內容不要混淆

6)友盟統計組件配置
#友盟相關
-libraryjars libs/umeng-analytics-v5.2.3.jar     #友盟統計的jar包不要混淆
-keep class com.umeng.** { *; }                        #友盟統計jar包下的所有類及類里面的所有內容不要混淆

7)自定義控件及組件不要打包混淆
如果我們自定了ListView,ScrollView,Gallery這些組件的時候,我們就不能混淆這些自定義的類了,因為在layout里面我們已經引用這個了類,而且已經寫死了。同樣四大組件也是不能打包混淆的
#四大組件不能混淆
-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 * extends android.view.View {*;}

8)數據適配器adapter不要混淆
#adapter也不能混淆
-keep public class * extends android.widget.BaseAdapter {*;}

如果你使用了CusorAdapter,添加下面這行
-keep public class * extends android.widget.CusorAdapter{*;}
同樣如果你覺得麻煩,就直接將BaseAdpater換成Adapter
9)數據模型不要混淆
-keepnames class * implements java.io.Serializable     #比如我們要向activity傳遞對象使用了Serializable接口的時候,這時候這個類及類里面#的所有內容都不能混淆
-keepclassmembers class * implements java.io.Serializable {
        *;
}

-keep class com.xx.xxx.domain.* {*;}
-keep class com.xx.xxx.vo.* {*;}

-keep class com.xx.xxx.model.* {*;}
這里的包名取決你自己定義的model所在包的名稱
10)百度地圖組件配置
#百度地圖相關
-libraryjars libs/baidumapapi_v2_4_0.jar        #地圖相關的jar包不要混淆
-keep class com.baidu.** { *; }                         #地圖組件包括圖層、定位等接口所有的類及類里面的內容都不要混淆
-keep class vi.com.gdi.bgl.android.**{*;}         #交通實況相關的類及類里面的所有內容不要混淆
-libraryjars libs/locSDK_3.1.jar                         #定位jar包不要混淆
-libraryjars libs/armeabi/libBaiduMapSDK_v2_4_0.so  #地圖相關的C++編譯的可執行文件(引擎)不要混淆
-libraryjars libs/armeabi/liblocSDK3.so                         #定位相關的C++編譯的可執行文件(引擎)不要混淆

這里要特別說明的是百度地圖這個apiKey,一定要注意測試的時候與打包發布時候的SHA1是不一樣的,所以需要准備兩個apiKey。
所以打包的時候一定要把apiKey設置成打包所需的apiKey,否則打包后的apk文件運行后圖層無法加載,但是定位功能不受影響
具體的操作是在打包最后一步把你的SHA1碼拷貝一份到應用控制台重新生成一個key,如果你使用的是baidumapapi3.0以上的SDK就沒有這么多麻煩事了
打包所需key生成如下圖所示

# 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 *;
#}


#系統配置
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify

# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

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

-keepclassmembers class **.R$* {
public static <fields>;
}

#四大組件不能混淆
-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 * extends android.view.View {*;}
#adapter也不能混淆
-keep public class * extends android.widget.BaseAdapter {*;}

-keep public class * extends android.widget.CusorAdapter{*;}
-keepnames class * implements java.io.Serializable #比如我們要向activity傳遞對象使用了Serializable接口的時候,這時候這個類及類里面#的所有內容都不能混淆
-keepclassmembers class * implements java.io.Serializable {
*;
}

-keep class com.xx.xxx.domain.* {*;}
-keep class com.xx.xxx.vo.* {*;}
-keep class com.xx.xxx.model.* {*;}
-dontwarn android.support.**
-dontwarn android.support.v4.**

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.

#百度分享相關
-libraryjars libs/armeabi/libbdpush_V2_1.so
-libraryjars libs/mips/libbdpush_V2_1.so
-libraryjars libs/x86
-libraryjars libs/Baidu-Frontia-Android-2.0.7.jar
-keep class com.baidu.** { *; }
-dontwarn com.baidu.**

#微博相關
-libraryjars libs/weibosdk.jar
-libraryjars libs/weibosdkcore.jar
-keep class com.sina.weibo.** { *; }
-keep class com.sina.weibo.** { *; }
-dontwarn com.sina.weibo.**

#jackson相關
-libraryjars libs/jackson-annotations-2.4.0-rc3.jar #jackson的jar包不要混淆
-libraryjars libs/jackson-core-2.4.0-rc3.jar
-libraryjars libs/jackson-databind-2.4.0-rc3.jar
-dontwarn com.fasterxml.jackson.** #告訴編譯器fastjson打包過程中不要提示警告
-keep class com.fasterxml.jackson.** { *; } #jackson包下的所有類不要混淆,包括類里面的方法
-keepattributes Signature #這行一定要加上,否則你的object中含有其他對象的字段的時候會拋出ClassCastException

#http相關

-keep class org.apache.http.entity.mime.** {*;} #http 中所有類及類里面的內容不要混淆


-libraryjars libs/mta-sdk-1.6.2.jar
-libraryjars libs/open_sdk.jar #騰訊平台jar包不要混淆
-keep class com.tencent.** { *; } #騰訊平台jar包中所有類及類里面的內容不要混淆

#圖片加載
-libraryjars libs/universal-image-loader-1.9.3-with-sources.jar #imageLoader的jar包不要混淆
-keep class com.nostra13.universalimageloader.** { *; } #imageLoader包下所有類及類里面的內容不要混淆

最后,如果打包總是出現問題,沒有耐心解決,那么推薦一下http://dev.360.cn/protect/welcome/

還有一款反編譯apk軟件,叫安卓改之理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM