Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 混淆機制

Android 混淆機制

編輯:關於Android編程

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

為了防止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如下:

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;"># 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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved