元始天尊 发表于 2015-12-27 12:01:50

【Android】如何编译、调试apktool

apktool jd-gui是反编译android程序常用的工具。因此很多反-反编译软件都在他们的bug上下功夫。
因此要想完美的解决反编译问题,就要能编译调试apktool。(好想吧jd-gui做成能跳转、改名的,这样的话jeb就没啥用了^_^)
在成功之前,经历不少挫折,也发现了不少东西,甚至一度以为不能编译成功而想自己建一个工程吧源码加进去编译了,,然而在这个过程中发现了
antlr junit findbugs guava等很好的工具和插件,在自己构建的工程中,有时候是通过脚本生成java文件,此外gradle还有种补丁文件用于编译前动态修改工程文件,后来看到个帖子,这个人编译的时候报缺少类函数他百思不得其解,其实是热补丁的原因^_^

下面是使用android studio(?!没错,android studio不光用来玩android,Itellij idea收费)编译调试apktool的图文教程:
0.参照上个教程配置好git/tortoise git等环境http://www.0xaa55.com/thread-1656-1-1.html,配置好jdk / android studio等软件
1.将apktool从github上下下来(参照上一篇帖子:如何编译gradle工程)
源码位于在:
如图,勾选recursive,开始下载源码(这一步可能需要翻墙什么的,我用的vpn,如果翻墙的话可以自己设置tortoisegit的代理服务器,在settings菜单中),最终得到80+M的apktool文件夹

2.经研究会发现check下来的master,最新源码并不适合编译,build.gradle中可以发现他们正在做2.0.3(),而master代码暂时还无法编译,所以我们需要check到2.0.2

Administrator@WINWORK-UC7OSF4 MINGW64 /e/androidsrc/apktool - 副本/Apktool/Apkto
ol (master)
$ git checkout 2.0.2
warning: unable to rmdir smali: Directory not empty
Note: checking out '2.0.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at ee9d44c... version bump (2.0.2)

Administrator@WINWORK-UC7OSF4 MINGW64 /e/androidsrc/apktool - 副本/Apktool/Apkto
ol ((2.0.2))
$ git checkout -b 2.0.2
Switched to a new branch '2.0.2'

Administrator@WINWORK-UC7OSF4 MINGW64 /e/androidsrc/apktool - 副本/Apktool/Apkto
ol (2.0.2)
$

Administrator@WINWORK-UC7OSF4 MINGW64 /e/androidsrc/apktool - 副本/Apktool/Apkto
ol (2.0.2)
$ git fetch

最终得到源码

3.导入到android studio中,注意根目录要选择有build.gradle文件的那个,选择默认gradle wrapper(如果翻墙的话需要在android studio设置里配好代理)

之后经过一系列插件下载,编译之类的进去以后,会提示一堆(android framework和unregistered vcs root),看情况选即可,吧需要的插件装好:
11:32:08 Gradle sync started
11:32:11 Gradle sync completed
11:32:12 Frameworks detected: Android framework is detected in the project Configure
11:32:12 Unregistered VCS roots detected
         The following directories are roots of VCS repositories, but they are not registered in the Settings:
         E:\androidsrc\apktool - 副本\Apktool\Apktool\smali
         E:\androidsrc\apktool - 副本\Apktool\Apktool
         Add rootsConfigureIgnore
从菜单->view->Tool Windows->gradle打开gradle projects子视图,点下里面的refresh按钮
下方状态栏会频繁下载gradle指定的插件什么的
完事后是这样


4.选择fatjar 双击进行编译(最深层的节点才可以编译,fatjar是生成可运行apitool-cli.jar包的任务,这个位置因环境而异,我的在apktool.Tasks.other.fatJar)
输出:
11:47:55: Executing external task 'fatJar'...
Incremental java compilation is an incubating feature.
Building SNAPSHOT (2.0.2): ee9d44
:brut.apktool.smali:util:compileJava UP-TO-DATE
:brut.apktool.smali:util:processResources UP-TO-DATE
:brut.apktool.smali:util:classes UP-TO-DATE
:brut.apktool.smali:util:jar UP-TO-DATE
:brut.apktool.smali:dexlib2:compileJava UP-TO-DATE
:brut.apktool.smali:dexlib2:processResources UP-TO-DATE
:brut.apktool.smali:dexlib2:classes UP-TO-DATE
:brut.apktool.smali:dexlib2:jar UP-TO-DATE
:brut.apktool.smali:baksmali:compileJava UP-TO-DATE
:brut.apktool.smali:baksmali:processResources UP-TO-DATE
:brut.apktool.smali:baksmali:classes UP-TO-DATE
:brut.apktool.smali:baksmali:fatJar UP-TO-DATE
:brut.apktool.smali:smali:generateGrammarSource UP-TO-DATE
:brut.apktool.smali:smali:generateJFlexSource UP-TO-DATE
:brut.apktool.smali:smali:compileJava UP-TO-DATE
:brut.apktool.smali:smali:processResources UP-TO-DATE
:brut.apktool.smali:smali:classes UP-TO-DATE
:brut.apktool.smali:smali:fatJar
:brut.j.common:compileJava UP-TO-DATE
:brut.j.common:processResources UP-TO-DATE
:brut.j.common:classes UP-TO-DATE
:brut.j.common:jar UP-TO-DATE
:brut.j.util:compileJava UP-TO-DATE
:brut.j.util:processResources UP-TO-DATE
:brut.j.util:classes UP-TO-DATE
:brut.j.util:jar UP-TO-DATE
:brut.j.dir:compileJava UP-TO-DATE
:brut.j.dir:processResources UP-TO-DATE
:brut.j.dir:classes UP-TO-DATE
:brut.j.dir:jar UP-TO-DATE
:brut.apktool.smali:baksmali:jar UP-TO-DATE
:brut.apktool.smali:smali:jar UP-TO-DATE
:brut.apktool:apktool-lib:compileJava UP-TO-DATE
:brut.apktool:apktool-lib:processResources UP-TO-DATE
:brut.apktool:apktool-lib:classes UP-TO-DATE
:brut.apktool:apktool-lib:jar UP-TO-DATE
:brut.apktool:apktool-cli:compileJava UP-TO-DATE
:brut.apktool:apktool-cli:processResources UP-TO-DATE
:brut.apktool:apktool-cli:classes UP-TO-DATE
:brut.apktool:apktool-cli:fatJarPrepareFiles UP-TO-DATE
:brut.apktool:apktool-cli:fatJar

BUILD SUCCESSFUL

Total time: 17.483 secs
11:48:13: External task execution finished 'fatJar'.

会发现编译出来的文件位于E:\androidsrc\apktool - 副本\Apktool\Apktool\brut.apktool\apktool-cli\build\libs\apktool-cli.jar

使用Java -jar 可以运行

5.配置调试,如图,选择edit configuration,加入jar application,命名为debug,path to jar 填写生成jar文件路径,参数根据需要填写一下即可
apply就可以看到debug条目了,在入口点apktool\brut.apktool\apktool-cli\src\main\java\brut\apktool\main.java设置断点,选择右边虫子按钮,开启调试!!!



7KY6 发表于 2018-1-14 14:53:57

可以可以!!
页: [1]
查看完整版本: 【Android】如何编译、调试apktool