Android APK反編譯

在決定寫這篇博客之前,我也查閱了大量的博客,然後進行了總結,希望這篇文章可以清晰明瞭的闡述我的中心思想。有時候我們想用別的APK的一些圖片,甚至想參考一下別的APK某個功能用到的技術點,這個時候就需要對這個APK進行反編譯,但是也不是所有的APK都可以完全的反編譯成功。下面我把我在Mac本上反編譯的流程給大家演示一遍:

工具安裝:

這裏用到了三個軟件,分別是dex2jar、jd-gui-osx、apktool。

1.dex2jar安裝:網址爲: https://sourceforge.net/projects/dex2jar/ ,下載之後解壓就可以了。

2.jd-gui-osx安裝:網址爲: http://mac.softpedia.com/get/Development/Java/JD-GUI.shtml,下載之後解壓就可以了。

3.apktool安裝:關於apktool安裝有很多種方法,網上也寫的亂七八糟,我認爲最簡單的安裝方式就是使用HomeBrew(對於HomeBrew不瞭解的請看一下它的官網:http://brew.sh/index_zh-cn.html或者我的一篇博客:http://my.oschina.net/gef/blog/658168),然後打開終端輸入:

brew search apktool

顯示效果如下:

gefufengdeMacBook-Air:/ gefufeng$ brew search apktool
apktool

意思就是搜索到了apktool,然後輸入:

gefufengdeMacBook-Air:/ gefufeng$ brew install apktool
==> Downloading https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.1.
==> Downloading from https://bbuseruploads.s3.amazonaws.com/0becf6a1-1706-4f2e-9
######################################################################## 100.0%
?  /usr/local/Cellar/apktool/2.1.0: 3 files, 6.2M, built in 5 minutes 32 seconds

下載完成後(此過程需要翻牆)輸入:apktool -v,顯示以下效果表示安裝成功:

gefufengdeMacBook-Air:/ gefufeng$ apktool -v
Apktool v2.1.0 - a tool for reengineering Android apk files
with smali v2.1.1 and baksmali v2.1.1
Copyright 2014 Ryszard Wiśniewski <[email protected]>
Updated by Connor Tumbleson <[email protected]>

usage: apktool
 -advance,--advanced   prints advance information.
 -version,--version    prints the version then exits
usage: apktool if|install-framework [options] <framework.apk>
 -p,--frame-path <dir>   Stores framework files into <dir>.
 -t,--tag <tag>          Tag frameworks using <tag>.
usage: apktool d[ecode] [options] <file_apk>
 -f,--force              Force delete destination directory.
 -o,--output <dir>       The name of folder that gets written. Default is apk.out
 -p,--frame-path <dir>   Uses framework files located in <dir>.
 -r,--no-res             Do not decode resources.
 -s,--no-src             Do not decode sources.
 -t,--frame-tag <tag>    Uses framework files tagged by <tag>.
usage: apktool b[uild] [options] <app_path>
 -f,--force-all          Skip changes detection and build all files.
 -o,--output <dir>       The name of apk that gets written. Default is dist/name.apk
 -p,--frame-path <dir>   Uses framework files located in <dir>.

For additional info, see: http://ibotpeaches.github.io/Apktool/ 
For smali/baksmali info, see: https://github.com/JesusFreke/smali

到此爲止,三個工具全部準備好了。

開始反編譯

首先創建一個文件夾,裏面放入你要反編譯的apk,然後在終端上cd到該文件夾目錄,最後執行命令:apktool d XXX.apk,例如:

gefufengdeMacBook-Air:apk2 gefufeng$ apktool d gefufeng.apk 
I: Using Apktool 2.1.0 on gefufeng.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /Users/gefufeng/Library/apktool/framework/1.apk
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...

然後再次查看該文件夾,如圖:

這時候你就可以查看這個APK的一些xml文件和圖片了,但是java代碼還是不能查看,因爲全是亂碼,這時候可以dex2jar和jd-gui-osx這兩個工具了,其思路就是使用dex2jar讓APK文件轉換成JAR文件,然後使用jd-gui查看jar文件:

首先cd到dex2jar文件夾,然後給d2j_invoke.sh和d2j-dex2jar.sh增加可執行權限,如下:

gefufengdeMacBook-Air:dex2jar-2.0 gefufeng$ chmod 777 d2j_invoke.sh
gefufengdeMacBook-Air:dex2jar-2.0 gefufeng$ chmod 777 d2j-dex2jar.sh

然後執行:./d2j-dex2jar.sh ../XXX.apk ,例如:

gefufengdeMacBook-Air:dex2jar-2.0 gefufeng$ ./d2j-dex2jar.sh /Users/gefufeng/work/tool/apk/ProviderApp-online-release.apk 
dex2jar /Users/gefufeng/work/tool/apk/ProviderApp-online-release.apk -> ./ProviderApp-online-release-dex2jar.jar

就會得到jar文件,jar文件的位置就在dex2jar文件夾下。

最後打開jd-gui-osx文件夾下的JD-GUI,將jar文件拖入就可以看到這個APK的java代碼了,如下圖:

修改後重新打包

這裏依然用到apktool,使用命令:apktool b XXX(XXX爲剛纔反編譯時生成的文件夾),例如:

gefufengdeMacBook-Air:apk gefufeng$ apktool b ProviderApp-online-release
I: Using Apktool 2.1.0
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
I: Checking whether resources has changed...
I: Building resources...
I: Copying libs... (/lib)
I: Building apk file...
I: Copying unknown files/dir...

完成之後ProviderApp-online-release文件夾裏會多兩個文件夾build和dist,如圖:

build和dist就是回編譯apk過程中生成的東西,編譯出來的apk在dist目錄下,但是這個APK是沒有經過簽名的,因爲打開build/apk文件夾會發現少了original文件夾下的META-INF文件夾。

重新給APK簽名

要想給APK簽名必須有一個keystore,關於怎麼生成一個keystore?其關鍵是對keytool工具的使用,我的另一篇博客(http://my.oschina.net/gef/blog/670874)中介紹了,生成簽名後就可以爲APK簽名了,例如:

jarsigner -digestalg SHA1 -sigalg MD5withRSA -tsa -verbose -keystore my.keystore -signedjar my_signed.apk my.apk my

到此結束了,歡迎糾正。

發佈了83 篇原創文章 · 獲贊 3 · 訪問量 4565
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章