Android應(yīng)用開(kāi)發(fā)之代碼混淆
混淆器(ProGuard)
混淆器通過(guò)刪除從未用過(guò)的代碼和使用晦澀名字重命名類、字段和方法,對(duì)代碼進(jìn)行壓縮,優(yōu)化和混淆。結(jié)果是一個(gè)比較小的.apk文件,該文件比較難進(jìn)行逆向project。因此,當(dāng)你的應(yīng)用程序?qū)Π踩舾校ㄒ蟾撸热绠?dāng)你授權(quán)應(yīng)用程序的時(shí)候,混淆器是一種重要的保護(hù)手段。
混淆器被集成在android 構(gòu)建系統(tǒng)中,所以你不必手動(dòng)調(diào)用它。同一時(shí)候混淆器僅在公布模式下進(jìn)行構(gòu)建應(yīng)用程序的時(shí)候才會(huì)執(zhí)行起來(lái),所以在調(diào)試模式下構(gòu)建程序時(shí),你不必處理混淆代碼。讓混淆器執(zhí)行起來(lái)是可選擇的,可是推薦選上。
1. 改動(dòng)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-19
將proguard.config前面的凝視去掉
2. 改動(dòng)proguard-project.txt
# 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 *; #}
假設(shè)在程序中使用了第三方的`jar`包,在混淆后導(dǎo)致出錯(cuò),這時(shí)我們須要在proguard-project.txt中去進(jìn)行對(duì)應(yīng)的配置,來(lái)讓其在混淆時(shí)不要混淆對(duì)應(yīng)的jar包。對(duì)改配置文件里的相關(guān)配置解釋例如以下:
```java -keep public class * extends android.app.Activity 【不進(jìn)行混淆類名的類,保持其原類名和包名】 -keep public abstract interface com.asqw.android.Listener{ public protected <methods>; 【全部public protected的方法名不進(jìn)行混淆】 } -keep public class com.asqw.android{ public void Start(java.lang.String); 【對(duì)該方法不進(jìn)行混淆】 } -keepclasseswithmembernames class * { 【對(duì)全部類的native方法名不進(jìn)行混淆】 native <methods>; } -keepclasseswithmembers class * { 【對(duì)全部類的指定方法的方法名不進(jìn)行混淆】 public <init>(android.content.Context, android.util.AttributeSet); } -keepclassmembers class * extends android.app.Activity {【對(duì)全部類的指定方法的方法名不進(jìn)行混淆】 public void *(android.view.View); } -keepclassmembers enum * {【對(duì)枚舉類型enum的全部類的下面指定方法的方法名不進(jìn)行混淆】 public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable {【對(duì)實(shí)現(xiàn)了Parcelable接口的全部類的類名不進(jìn)行混淆,對(duì)其成員變量為Parcelable$Creator類型的成員變量的變量名不進(jìn)行混淆】 public static final android.os.Parcelable$Creator *; } -keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue {【對(duì)指定類的指定變量的變量名不進(jìn)行混淆】 volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node head; volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail; volatile transient int sweepVotes; } -keep public class com.unionpay.** {*; }【對(duì)com.unionpay包下全部的類都不進(jìn)行混淆,即不混淆類名,也不混淆方法名和變量名】 ```
經(jīng)過(guò)上面這兩部之后反編譯后就能混淆了,可是四大組件還在,為什么四大組件還在呢,由于四大組件是在清單文件里進(jìn)行配置的,假設(shè)混淆后就不能依據(jù)清單文件的配置去尋找了。
假設(shè)對(duì)于一些自己的代碼中要想提供出來(lái)讓別人通過(guò)反射調(diào)用的方法時(shí),我們不想讓部分代碼被混淆,或者是我們使用別人提供的第三方j(luò)ar包,由于第三方的jar包一般都是已經(jīng)混淆過(guò)的,我們要是再混淆就會(huì)報(bào)錯(cuò)了,所以我們要保證這些內(nèi)容不用混淆,這里我們僅僅需改動(dòng)這個(gè)文件,然后加上后面的一句話,他就不會(huì)混淆我們給出的內(nèi)容
-keepattributes *Annotation* -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.BackupAgent -keep public class * extends android.preference.Preference -keep public class * extends android.support.v4.app.Fragment -keep public class * extends android.app.Fragment -keep public class com.android.vending.licensing.ILicensingService -keep class com.itheima.mobilesafe.engine.AppInfoProvider -keep class net.youmi.android.** { *; }
相關(guān)文章
Android自定義View編寫隨機(jī)驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android自定義View隨機(jī)驗(yàn)證碼實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android開(kāi)發(fā)之SQLite的使用方法
本篇文章介紹了,Android開(kāi)發(fā)之SQLite的使用方法。需要的朋友參考下2013-04-04Android開(kāi)發(fā)獲取短信的內(nèi)容并截取短信
本文給大家介紹android開(kāi)發(fā)獲取短信內(nèi)容并截取短息的相關(guān)內(nèi)容,本文代碼簡(jiǎn)單易懂,感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類
這篇文章主要介紹了Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類,涉及Android針對(duì)日期時(shí)間的相關(guān)運(yùn)算與判斷簡(jiǎn)單操作技巧,需要的朋友可以參考下2018-02-02Android中的Looper對(duì)象詳細(xì)介紹
這篇文章主要介紹了Android中的Looper對(duì)象,需要的朋友可以參考下2014-02-02Android 自定義ProgressDialog進(jìn)度條對(duì)話框用法詳解
ProgressDialog為進(jìn)度對(duì)話框。android手機(jī)自帶的對(duì)話框顯得比較單一,我們可以通過(guò)ProgressDialog來(lái)自己定義對(duì)話框中將要顯示出什么東西2016-01-01Android實(shí)現(xiàn)軟件列表的點(diǎn)擊啟動(dòng)另外一個(gè)程序功能【附demo源碼下載】
這篇文章主要介紹了Android實(shí)現(xiàn)軟件列表的點(diǎn)擊啟動(dòng)另外一個(gè)程序功能,涉及Android針對(duì)應(yīng)用程序的讀取、加載、啟動(dòng)等操作相關(guān)技巧,需要的朋友可以參考下2016-07-07Android中的廣播(BroadCast)詳細(xì)介紹
這篇文章主要介紹了Android中的廣播(BroadCast)詳細(xì)介紹,本文講解了什么是廣播、廣播有什么用、實(shí)現(xiàn)廣播、動(dòng)態(tài)注冊(cè)方式、配置文件方式等內(nèi)容,需要的朋友可以參考下2015-03-03