Android 混淆機制

來源:互聯網
上載者:User

一、為什麼要加入混淆機制?

為了防止apk被反編譯後,很容易被其他人看懂。

混淆機制的本質是什嗎?

把原來有具體含義的類名,變數名,方法名,修改成讓人看不懂的名字,例如方法名getUserName編程了方法名a


二、如何混淆代碼

Android工程目錄下有兩個檔案,project.properties,proguard-project.txt

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.target=android-18
# 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

此行指定了混淆代碼的設定檔,是/home/jltxgcy/android-sdk-linux/tools/proguard/proguard-android.txt。如果想最佳化你的代碼,設定檔換成/home/jltxgcy/android-sdk-linux/tools/proguard/proguard-android-optimize.txt。

冒號後面就是自訂混淆規則檔案,如下:

2、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 *;#}
# 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
增加自訂混淆機制,預設情況下,這裡的規則會被附加到/home/jltxgcy/android-sdk-linux/tools/proguard/proguard-android.txt後面。

3、匯出混淆後的apk

預設的debug版本的apk是不包含混淆資訊的,所以要產生release版本的apk,點擊右鍵,選擇Android Tools->Export Signed Application Package,此時匯出的apk是包含混淆資訊的。


三、執行個體分析

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-optimize.txt:proguard-project.txt# Project target.target=android-18

2、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 *;#}-keep public class * extends android.app.Activity #保留繼承Activity類的類名-keep public class * extends android.app.Application #保留繼承Application類的類名#-keepclassmembers class com.jltxgcy.crack.MainActivity$SNChecker {#   public boolean isRegistered();#}-keep class android.support.v4.** { *; }//保留這個第三方jar包不被混淆-keep interface android.support.v4.** { *; }

3、/home/jltxgcy/android-sdk-linux/tools/proguard/proguard-android-optimize.txt如下:

# This is a configuration file for ProGuard.# http://proguard.sourceforge.net/index.html#manual/usage.html# Optimizations: If you don't want to optimize, use the# proguard-android.txt configuration file instead of this one, which# turns off the optimization flags.  Adding optimization introduces# certain risks, since for example not all optimizations performed by# ProGuard works on all versions of Dalvik.  The following flags turn# off various optimizations known to have issues, but the list may not# be complete or up to date. (The "arithmetic" optimization can be# used if you are only targeting Android 2.0 or later.)  Make sure you# test thoroughly if you go this route.-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*-optimizationpasses 5-allowaccessmodification-dontpreverify# The remainder of this file is identical to the non-optimized version# of the Proguard configuration file (except that the other file has# flags to turn off optimization).-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose-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的方法的方法名和包含native方法的類的類名不變    native ;}# 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 {#保留繼承於View的類中set*和get*方法的方法名不變   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 { #保留繼承於Activity的類中以View為參數,傳回值是void的方法的方法名   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 { #保留實現了Parcelable介面的類的類名以及Parcelable$Createor內部類的類名  public static final android.os.Parcelable$Creator *;}-keepclassmembers class **.R$* { #保留R$*類中靜態欄位的欄位名    public static ;}# 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.**

4、匯出簽名的apk,命名為CrackApk01.apk。


5、在proguard-project.txt中去掉注釋

-keepclassmembers class com.jltxgcy.crack.MainActivity$SNChecker {   public boolean isRegistered();}
再次匯出簽名的apk,命名為CrackApk02.apk。


四、proguard-android-optimize.txt和proguard-project.txt說明

-keep class 保留類名

-keepclassmembers 保留類中的方法或者欄位名

-keepclasseswithmembernames 保留類名和類中的方法或者欄位名

此部分詳見:http://proguard.sourceforge.net/index.html#manual/examples.html


五、工程源碼下載

http://download.csdn.net/detail/jltxgcy/7125411

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.