天天看點

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

Android反編譯xml檔案

一、前言:

大家好,今天給大家分享一下Android中的拿來主義,我們時常會碰到一個自己覺得很漂亮很帥氣的應用(apk),是以我們會嘗試用WinRAR等之類工具檢視,而一般的應用程式打包後的目錄通常是這樣的如下圖:

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

當然res裡的圖檔是可以拿來就用的(筆者的好多應用的圖檔都是從别人的apk裡扣出來的),而諸如layout裡的布局及權限檔案(AndroidManifest.xml)已經是一堆亂碼了,完全看不懂,想看看别人是怎麼布局的都不容易。還有源代碼都被編譯成了classes.dex,完全看不出什麼線索。基于以上的困惑,筆者給大家分享一下Android中的拿來主義。

二、所需工具(點選各自連接配接進入下載下傳頁面):

1.AXMLPrinter2.jar

2.baksmali.jar

3.smali.jar

三、準備工作

為了友善起見,作者把AXMLPrinter2.jar,還有baksmali.jar,還有smali.jar(下下來為了友善重命名),放在Android SDK tools檔案夾中如下圖所示:

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

為了便于大家更容易程式比對,作者寫了一個簡單的應用(叫APKInstaller)目錄結構如下圖所示:

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

四、開始拿來主義

1.用AXMLPrinter2.jar檢視apk中的布局xml檔案:

将ApkInstaller應用生成的ApkInstaller.apk(為了友善起見放到tools目錄裡)用WinRAR等工具打開,将res/layout/main.xml解壓出來(也還是放在tools目錄裡哦)

打開main.xml檔案,内容如下(一堆天文):

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

這時候AXMLPrinter2.jar派上用場了,打開cmd終端,一直進入到tools目錄下,輸入如下指令:

java -jar AXMLPrinter2.jar main.xml > main.txt. (如下圖所示)

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

打開main.txt代碼如下(是不是有個123了呵呵~):

[html] view plain copy

  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <LinearLayout     
  4.     xmlns:android="http://schemas.android.com/apk/res/android"    
  5.     android:orientation="1"    
  6.     android:layout_width="-1"    
  7.     android:layout_height="-1"    
  8.     >     
  9.     <WebView     
  10.         android:id="@7F050000"    
  11.         android:layout_width="-1"    
  12.         android:layout_height="-2"    
  13.         >     
  14.     </WebView>     
  15. </LinearLayout>    
  16. <?xml version="1.0" encoding="utf-8"?>  
  17. <LinearLayout  
  18.  xmlns:android="http://schemas.android.com/apk/res/android"  
  19.  android:orientation="1"  
  20.  android:layout_width="-1"  
  21.  android:layout_height="-1"  
  22.  >  
  23.  <WebView  
  24.   android:id="@7F050000"  
  25.   android:layout_width="-1"  
  26.   android:layout_height="-2"  
  27.   >  
  28.  </WebView>  
  29. </LinearLayout>  

為了比對打開源程式中的main.xml代碼如下(大家比對一下吧):

[html] view plain copy

  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:orientation="vertical"    
  5.     android:layout_width="fill_parent"    
  6.     android:layout_height="fill_parent"    
  7.     >     
  8. <WebView     
  9.     android:id="@+id/apk_web"    
  10.     android:layout_height="wrap_content"    
  11.     android:layout_width="fill_parent"    
  12. />     
  13. </LinearLayout>   

混淆檔案 proguard.cfg 參數詳解

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案
-optimizationpasses 5                                                           # 指定代碼的壓縮級别
-dontusemixedcaseclassnames                                                     # 是否使用大小寫混合
-dontskipnonpubliclibraryclasses                                                # 是否混淆第三方jar
-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 方法不被混淆
    native <methods>;
}

-keepclasseswithmembers class * {                                               # 保持自定義控件類不被混淆
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);     # 保持自定義控件類不被混淆
}

-keepclassmembers class * extends android.app.Activity {                        # 保持自定義控件類不被混淆
   public void *(android.view.View);
}

-keepclassmembers enum * {                                                      # 保持枚舉 enum 類不被混淆
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {                                # 保持 Parcelable 不被混淆
  public static final android.os.Parcelable$Creator *;
}

-keep class MyClass;                                                            # 保持自己定義的類不被混淆      
Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

代碼混淆的方法

根據 SDK 的版本不同有 2 中不同的代碼混淆方式,以上的 proguard.cfg 參數詳解中所涉及到的資訊是在較低版本SDK 下的混淆腳本,事實上在高版本的SDK 下混淆的原理和參數也與低版本的相差無幾,隻是在不同SDK 版本的環境下引入混淆腳本的方式有所不同。具體方法如下:

  • 低版本 SDK 下,項目中同時包含 proguard.cfg 和 project.properties 檔案,則隻需在project.properties 檔案末尾添加 proguard.config=proguard.cfg再将項目 Export 即可。
  • 高版本SDK 下,項目中同時包含proguard-project.txt 和 project.properties 檔案,這時需要在proguard-project.txt 檔案中進行如下資訊的配置,然後再将項目Export 即可。下面以真實的檔案進行示範說明。
Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案
# 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

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-16      
Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案

以上的配置資訊即是 project.properties 檔案中内容,藍色文字為我們在代碼混淆過程中需要添加的配置資訊,其中:sdk.dir 為你在目前機器上 SDK 的安裝路徑。如果想保留某個包下的檔案不被混淆,可以在 proguard-project.txt 檔案中加入保留對應包名的語句即可。

Android如何反編譯xml檔案 ,和防止代碼反編譯Android反編譯xml檔案
# 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:

-dontwarn com.cnki.android.cnkireader.** 
-keep class com.cnki.android.cnkireader.** { *; }

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