Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1315568
  • 博文数量: 478
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4833
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-28 11:12
文章分类

全部博文(478)

文章存档

2019年(1)

2018年(27)

2017年(21)

2016年(171)

2015年(258)

我的朋友

分类: Android平台

2016-05-30 18:07:01

http://blog.csdn.net/huangyabin001/article/details/44407319
一:解压缩(获取图片等资源)

    对于apk中丰富的资源,如果我们在练习的时候需要引用某些apk中的资源文件时,最简单的办法使用解压缩工具对apk进行解压缩,然后在相应的目录下查找需要的资源文件。

二:反编译APK

    我们可以通过解压缩的方式去使用某些apkres/drawable,res/rawassets目录下的相关多媒体资源和字体文件等,但是想要同时临摹动画、布局等xml资源却无能为力,因为res/rawassets文件夹来存放不需要系统编译成二进制的文件,而其他文件在打包的过程会编译成二进制文件。那么此时我们该怎么办呢?

    Google Code上为我们提供了对apk进行反编译的工具包-apktool,当前最新的版本是2.0.0 RC42015-02-12iBotPeaches上传在(由于google将要关闭google code服务,所有版本的apktool将会在Bitbucket上发布),当然我们也可以从Google Code中搜索到该入口。

    我们通过apktoolapk进行反编译操作,从而得到apk应用中的源代码和图片、XML配置、语言资源等文件。那么如何使用apktool呢,下面我们简单的介绍一下(这里参考Google Code上提供的文档,查看原文档请移步):

 1.apktool下载

通过上面的下载连接我们可以得到名为apktool_2.0.0rc4.jar的jar包,那么我们该如何使用它呢?

  1>重命名

    将apktool_2.0.0rc4.jar修改为apktool.jar;

  2>适配不同的操作系统

  windows下

    将下面脚本内容保存在apktool.bat文件中

[html] view plain copy
 print?
  1. @echo off  
  2. set PATH=%CD%;%PATH%;  
  3. java -jar -Duser.language=en "%~dp0\apktool.jar" %1 %2 %3 %4 %5 %6 %7 %8 %9  
  linux下:


    将下面脚本内容保存为apktool文件

[html] view plain copy
 print?
  1. #!/bin/bash  
  2. #  
  3. # Copyright (C) 2007 The Android Open Source Project  
  4. #  
  5. # Licensed under the Apache License, Version 2.0 (the "License");  
  6. # you may not use this file except in compliance with the License.  
  7. # You may obtain a copy of the License at  
  8. #  
  9. #     
  10. #  
  11. # Unless required by applicable law or agreed to in writing, software  
  12. # distributed under the License is distributed on an "AS IS" BASIS,  
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  14. # See the License for the specific language governing permissions and  
  15. # limitations under the License.  
  16.   
  17. # This script is a wrapper for smali.jar, so you can simply call "smali",  
  18. # instead of java -jar smali.jar. It is heavily based on the "dx" script  
  19. # from the Android SDK  
  20.   
  21. # Set up prog to be the path of this script, including following symlinks,  
  22. # and set up progdir to be the fully-qualified pathname of its directory.  
  23. prog="$0"  
  24. while [ -h "${prog}" ]; do  
  25.     newProg=`/bin/ls -ld "${prog}"`  
  26.     echo ${newProg}  
  27.   
  28.   
  29.     newProg=`expr "${newProg}" : ".* -> .?$"`  
  30.     if expr "x${newProg}" : 'x/' >/dev/null; then  
  31.         prog="${newProg}"  
  32.     else  
  33.         progdir=`dirname "${prog}"`  
  34.         prog="${progdir}/${newProg}"  
  35.     fi  
  36. done  
  37. oldwd=`pwd`  
  38. progdir=`dirname "${prog}"`  
  39. cd "${progdir}"  
  40. progdir=`pwd`  
  41. prog="${progdir}"/`basename "${prog}"`  
  42. cd "${oldwd}"  
  43.   
  44.   
  45. jarfile=apktool.jar  
  46. libdir="$progdir"  
  47. if [ ! -r "$libdir/$jarfile" ]  
  48. then  
  49.     echo `basename "$prog"`": can't find $jarfile"  
  50.     exit 1  
  51. fi  
  52.   
  53. javaOpts=""  
  54.   
  55. # If you want DX to have more memory when executing, uncomment the following  
  56. # line and adjust the value accordingly. Use "java -X" for a list of options  
  57. # you can pass here.  
  58. #   
  59. javaOpts="-Xmx512M"  
  60.   
  61. # Alternatively, this will extract any parameter "-Jxxx" from the command line  
  62. # and pass them to Java (instead of to dx). This makes it possible for you to  
  63. # add a command-line parameter such as "-JXmx256M" in your ant scripts, for  
  64. # example.  
  65. while expr "x$1" : 'x-J' >/dev/null; do  
  66.     opt=`expr "$1" : '-J.?'`  
  67.     javaOpts="${javaOpts} -${opt}"  
  68.     shift  
  69. done  
  70.   
  71. if [ "$OSTYPE" = "cygwin" ] ; then  
  72.     jarpath=`cygpath -w  "$libdir/$jarfile"`  
  73. else  
  74.     jarpath="$libdir/$jarfile"  
  75. fi  
  76.   
  77. # add current location to path for aapt  
  78. PATH=$PATH:`pwd`;  
  79. export PATH;  
  80. exec java $javaOpts -jar "$jarpath" "$@"  
  3>设置环境变量


将apktool.jar和apktoo.bat所在的文件夹添加到系统环境变量中或者拷贝到系统文件夹中C://Windows,Linux下拷贝到/usr/local/bin (root needed)中,并且要记得修改文件权限(chmod +x)

[html] view plain copy
 print?
  1. 实用选项  


[html] view plain copy
 print?
  1. -version, --version  


[html] view plain copy
 print?
  1. 输出当前版本。  


[html] view plain copy
 print?
  1. -v, --verbose  


[html] view plain copy
 print?
  1. 详细输出,该命令在所有其他命令之前。  


[html] view plain copy
 print?
  1. -q, --quiet  


[html] view plain copy
 print?
  1. 静态输出. 该命令在所有其他命令之前。  


[html] view plain copy
 print?
  1. -advance, --advanced  


[html] view plain copy
 print?
  1. 打印高级选项。  


[html] view plain copy
 print?
  1. 反编译选项  


[html] view plain copy
 print?
  1. --api <API>  


[html] view plain copy
 print?
  1. 生成smali文件的api版本。(eg 14 for ICS).  


[html] view plain copy
 print?
  1. -b, --no-debug-info  


[html] view plain copy
 print?
  1. 不打印log信息.  


[html] view plain copy
 print?
  1. -d, --debug  


[html] view plain copy
 print?
  1. 启动debug模式  


[html] view plain copy
 print?
  1. --debug-line-prefix <prefix>  


[html] view plain copy
 print?
  1. Smali line prefix when decoding in debug mode. Default "a=0;//"  


[html] view plain copy
 print?
  1. -f, --force  


[html] view plain copy
 print?
  1. 如果反编译后生成的文件目录已经存在,则强制覆盖。  


[html] view plain copy
 print?
  1. --keep-broken-res  


[html] view plain copy
 print?
  1. 如果反编译过程正发生错误,需手动修复。  


[html] view plain copy
 print?
  1. -m, --match-original  


[html] view plain copy
 print?
  1. 最大可能保持文件接近原始文件,防止重建,通常用于分析。  


[html] view plain copy
 print?
  1. -o, --output <dir>  


[html] view plain copy
 print?
  1. 指定输出路径.  


[html] view plain copy
 print?
  1. -p, --frame-path <dir>  


[html] view plain copy
 print?
  1. 指定framework路径.  


[html] view plain copy
 print?
  1. -r, --no-res  


[html] view plain copy
 print?
  1. 防止重新编译资源文件。  


[html] view plain copy
 print?
  1. -s, --no src  


[html] view plain copy
 print?
  1. 防止重新编译源文件.  


[html] view plain copy
 print?
  1. -t, --frame-tag <tag>  


[html] view plain copy
 print?
  1. 使用framework文件标记.  


[html] view plain copy
 print?
  1. 如何反编译?  


[html] view plain copy
 print?
  1. 反编译之前, 必须保证frameworks已经安装。有关Frameworks可以访问。如果已安装了frameworks,可以运行如下命令进行反编译:  


[html] view plain copy
 print?
  1. apktool d name_of_apk.apk  


[html] view plain copy
 print?
  1. 重建选项  


[html] view plain copy
 print?
  1. -a, --aapt <file>  


[html] view plain copy
 print?
  1. 从指定的路径总载入aapt,如果找不到相关目录则会执行回滚操作.  


[html] view plain copy
 print?
  1. -c, --copy-original  


[html] view plain copy
 print?
  1. 拷贝 AndroidManifest.xml 和 META-INF 文件夹到重建的apk中.  


[html] view plain copy
 print?
  1. -d, --debug  


[html] view plain copy
 print?
  1. 启动debug模式。  


[html] view plain copy
 print?
  1. -f, --force-all  


[html] view plain copy
 print?
  1. 重建过程中覆盖已存在的文件.  


[html] view plain copy
 print?
  1. -o, --output <dir>  


[html] view plain copy
 print?
  1. 指定输出路径.  


[html] view plain copy
 print?
  1. -p, --frame-path <dir>  


[html] view plain copy
 print?
  1. 指定framework files的路径.  


[html] view plain copy
 print?
  1. 如何重建一个项目?  


[html] view plain copy
 print?
  1. apktool b folder_of_decoded_apk  


那么通过apktool d xx.apk,我们将apk文件反编译之后我们就可以使用编辑工具查看一些xml配置文件了,但是源文件对于我们来说还是未解之谜。因为apktool将字节码文件转换为smali文件。

smali是将Android字节码用可阅读的字符串形式表现出来的一种语言,可以称之为Android字节码的反汇编语言。使用baksmali或apktool可以将Android应用程序包(apk或jar)反编译为smali代码。

那么我们接下来要做的就是把smali文件反编译为文件。

三:反编译smali

    smali2java是一个将smali代码反编译成java代码的工具,是基于apktool v1.5.0(baksmali v1.3.4)生成的smali文件,依赖于smali文件中的代码行数(.line关键字)和变量别名(.local关键字)等信息,可以最大程度还原原始的java代码。还原出的java代码将具有原始的变量命名,代码的顺序也与原始的java代码保持一致。

    下载地址:

反编译工具(csdn资源)下载地址:

阅读(1137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~