轉載 點擊打開鏈接
1.引入默認的打包配置 在project.properties中添加語句 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt 新建proguard-android.txt 拷貝${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生成如下圖所示 |