您的位置:首页 > 移动开发 > Android开发

Android APK反编译

2016-05-10 00:00 531 查看
摘要: 本文的目的是让开发者去学习他人的技术和设计,而不是将应用换壳后混乱应用市场,以Mac系统为例。

在决定写这篇博客之前,我也查阅了大量的博客,然后进行了总结,希望这篇文章可以清晰明了的阐述我的中心思想。有时候我们想用别的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 <brut.alll@gmail.com>
Updated by Connor Tumbleson <connor.tumbleson@gmail.com>

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[/code] 到此为止,三个工具全部准备好了。

开始反编译

首先创建一个文件夹,里面放入你要反编译的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

到此结束了,欢迎纠正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息