Android 有没有办法从 APK 文件中获取源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3593420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:53:54  来源:igfitidea点击:

Is there a way to get the source code from an APK file?

androidandroid-resourcesdecompilingapk

提问by Frank Bozzo

The hard drive on my laptop just crashed and I lost all the source code for an app that I have been working on for the past two months. All I have is the APK file that is stored in my email from when I sent it to a friend.

我笔记本电脑上的硬盘刚刚崩溃,我丢失了过去两个月一直在开发的应用程序的所有源代码。我所拥有的只是我发送给朋友时存储在我的电子邮件中的 APK 文件。

Is there any way to extract my source code from this APK file?

有没有办法从这个 APK 文件中提取我的源代码?

回答by Prankul Garg

Simple way: use online tool http://www.javadecompilers.com/apk, upload apk and get source code.

简单方法:使用在线工具http://www.javadecompilers.com/apk,上传apk,获取源码。



Procedure for decoding .apk files, step-by-step method:

解码.apk文件的步骤,分步方法:

Step 1:

第1步:

  1. Make a new folder and copy over the .apk file that you want to decode.

  2. Now rename the extension of this .apk file to .zip (e.g. rename from filename.apk to filename.zip) and save it. Now you can access the classes.dex files, etc. At this stage you are able to see drawables but not xml and java files, so continue.

  1. 创建一个新文件夹并复制要解码的 .apk 文件。

  2. 现在将这个 .apk 文件的扩展名重命名为 .zip(例如从 filename.apk 重命名为 filename.zip)并保存它。现在你可以访问 classes.dex 文件等。在这个阶段你可以看到 drawables 但不能看到 xml 和 java 文件,所以继续。

Step 2:

第2步:

  1. Now extract this .zip file in the same folder (or NEW FOLDER).

  2. Download dex2jarand extract it to the same folder (or NEW FOLDER).

  3. Move the classes.dex file into the dex2jar folder.

  4. Now open command prompt and change directory to that folder (or NEW FOLDER). Then write d2j-dex2jar classes.dex(for mac terminal or ubuntu write ./d2j-dex2jar.sh classes.dex) and press enter. You now have the classes.dex.dex2jar file in the same folder.

  5. Download java decompiler, double click on jd-gui, click on open file, and open classes.dex.dex2jar file from that folder: now you get class files.

  6. Save all of these class files (In jd-gui, click File -> Save All Sources) by src name. At this stage you get the java source but the .xml files are still unreadable, so continue.

  1. 现在将此 .zip 文件解压缩到同一文件夹(或新文件夹)中。

  2. 下载 dex2jar并将其压缩到同一文件夹(或新文件夹)。

  3. 将 classes.dex 文件移动到 dex2jar 文件夹中。

  4. 现在打开命令提示符并将目录更改为该文件夹(或新文件夹)。然后写入d2j-dex2jar classes.dex(对于 mac 终端或 ubuntu 写入./d2j-dex2jar.sh classes.dex)并按 Enter。您现在在同一文件夹中有 classes.dex.dex2jar 文件。

  5. 下载 java decompiler,双击 jd-gui,点击打开文件,然后从那个文件夹打开 classes.dex.dex2jar 文件:现在你得到了类文件。

  6. 按 src 名称保存所有这些类文件(在 jd-gui 中,单击 File -> Save All Sources)。在此阶段,您获得了 java 源代码,但 .xml 文件仍然无法读取,因此请继续。

Step 3:

第 3 步:

Now open another new folder

现在打开另一个新文件夹

  1. Put in the .apk file which you want to decode

  2. Download the latest version of apktool ANDapktool install window(both can be downloaded from the same link) and place them in the same folder

  3. Open a command window

  4. Now run command like apktool if framework-res.apk(if you don't have it get it here)and next

  5. apktool d myApp.apk(where myApp.apk denotes the filename that you want to decode)

  1. 放入要解码的 .apk 文件

  2. 下载最新版本的apktoolapktool 安装窗口(都可以从同一个链接下载)并将它们放在同一个文件夹中

  3. 打开命令窗口

  4. 现在运行命令apktool if framework-res.apk(如果你没有它在这里得到它)然后下一步

  5. apktool d myApp.apk(其中 myApp.apk 表示您要解码的文件名)

now you get a file folder in that folder and can easily read the apk's xml files.

现在您在该文件夹中获得了一个文件夹,并且可以轻松读取 apk 的 xml 文件。

Step 4:

第四步:

It's not any step, just copy contents of both folders(in this case, both new folders) to the single one

这不是任何步骤,只需将两个文件夹(在这种情况下,两个新文件夹)的内容复制到一个

and enjoy the source code...

并享受源代码...

回答by Trinimon

This is an alternative description - just in case someone got stuck with the description above. Follow the steps:

这是另一种描述 - 以防万一有人被上面的描述卡住了。按照步骤:

  1. download apktool.bat(or apktoolfor Linux) and apktool_<version>.jarfrom http://ibotpeaches.github.io/Apktool/install/
  2. rename the jar file from above to apktool.jarand put both files in the same folder

  3. open a dos box (cmd.exe) and change into that folder; verify that a Java Environment is installed (for Linux check the notes regarding required libraries as well)
  4. Start: apktool decode [apk file]

    Intermediate result: resource files, AndroidManifest.xml

  5. unzip APK file with an unpacker of your choice

    Intermediate result: classes.dex

  6. download and extract dex2jar-0.0.9.15.zipfrom http://code.google.com/p/dex2jar/downloads/detail?name=dex2jar-0.0.9.15.zip&can=2&q=
  7. drag and drop classes.dexonto dex2jar.bat(or enter <path_to>\dex2jar.bat classes.dexin a DOS box; for Linux use dex2jar.sh)

    Intermediate result: classes_dex2jar.jar

  8. unpack classes_dex2jar.jar(might be optional depending on used decompiler)
  9. decompile your class files (e.g. with JD-GUIor DJ Decompiler)

    Result: source code

  1. 下载apktool.bat(或apktool用于 Linux)和apktool_<version>.jar来自 http://ibotpeaches.github.io/Apktool/install/
  2. 将 jar 文件从上面重命名为apktool.jar并将两个文件放在同一文件夹中

  3. 打开一个 dos 框 ( cmd.exe) 并切换到该文件夹​​;验证是否安装了 Java 环境(对于 Linux,请查看有关所需库的注释)
  4. 开始: apktool decode [apk file]

    中间结果:资源文件,AndroidManifest.xml

  5. 使用您选择的解压器解压 APK 文件

    中间结果classes.dex

  6. dex2jar-0.0.9.15.ziphttp://code.google.com/p/dex2jar/downloads/detail?name=dex2jar-0.0.9.15.zip&can=2&q=下载并提取
  7. 拖放classes.dexdex2jar.bat(或<path_to>\dex2jar.bat classes.dex在 DOS 框中输入;对于 Linux 使用dex2jar.sh

    中间结果classes_dex2jar.jar

  8. 解包classes_dex2jar.jar(根据使用的反编译器可能是可选的)
  9. 反编译您的类文件(例如使用JD-GUIDJ Decompiler

    结果:源代码

Note: it is not allowed to decompile third party packages; this guide is intended to recover personal source code from an APK file only; finally, the resulting code will most likely be obfuscated

注意:不允许反编译第三方包;本指南仅用于从 APK 文件中恢复个人源代码;最后,生成的代码很可能会被混淆

回答by Ryan Gooler

While you may be able to decompile your APKfile, you will likely hit one big issue:

虽然您可能能够反编译您的APK文件,但您可能会遇到一个大问题:

it's not going to return the code you wrote. It is instead going to return whatever the compiler inlined, with variables given random names, as well as functions given random names. It could take significantly more time to try to decompile and restore it into the code you had, than it will be to start over.

它不会返回您编写的代码。相反,它将返回编译器内联的任何内容,变量给出随机名称,函数给出随机名称。与重新开始相比,尝试反编译并将其恢复为您拥有的代码可能需要更多的时间。

Sadly, things like this have killed many projects.
For the future, I highly recommend learning a Version Control System, like CVS, SVNand gitetc.

可悲的是,这样的事情已经扼杀了许多项目。
对于未来,我强烈建议学习版本控制系统,如CVSSVNgit等。

and how to back it up.

以及如何备份它。

回答by Cilenco

There is also a new application on the Play Storewith which it is possible to decompile an apk (system applications too) and view the source code right on your smartphone. It saves the files to your SD card so you can view it on your computer too. It does not require root or something else.

Play Store上还有一个新应用程序,可以使用它反编译 apk(也包括系统应用程序)并直接在您的智能手机上查看源代码。它将文件保存到您的 SD 卡,以便您也可以在您的计算机上查看它。它不需要root或其他东西。

Just install and have fun. I think this is the easiest way to decompile an app.

只需安装并玩得开心。我认为这是反编译应用程序的最简单方法。

回答by Macarse

apktoolis the best thing you can try. I have saved some xml with it, but honestly I don't know how it will work with the .java code.

apktool是您可以尝试的最佳工具。我用它保存了一些 xml,但老实说我不知道​​它如何与 .java 代码一起工作。

I would recommend you to have a code repository even if your are the only coder. I've been using Project Lockerfor my own projects. It gives you free svn and git repos.

即使您是唯一的编码员,我也建议您拥有一个代码存储库。我一直在为我自己的项目使用Project Locker。它为您提供免费的 svn 和 git 存储库。

回答by Tom Hennen

These two articles describe how to combine the use of apktooland dex2jarto take an APKfile and create an Eclipse project that can build and run it.

这两篇文章描述了如何结合使用apktooldex2jar获取APK文件并创建可以构建和运行它的 Eclipse 项目。

http://blog.inyourbits.com/2012/11/extending-existing-android-applications.html

http://blog.inyourbits.com/2012/12/extending-existing-android-applications.html

http://blog.inyourbits.com/2012/11/extending-existing-android-applications.html

http://blog.inyourbits.com/2012/12/extending-existing-android-applications.html

Basically you:

基本上你:

  1. Use apktoolto get the resource files out of the apk
  2. Use dex2jarto get a jar file that contains the classes in a format that Eclipse will like.
  3. Create an Eclipse project point it at the resource files and the new jar file
  4. Open the jar file with a zip utility and delete the existing resources
  5. Open the jar file with JDGuito view the source code
  6. Take whatever source code you need from JDGui, stick it in a class inside Eclipse and modify it
  7. Delete that class from the jar file (so you don't have the same class defined multiple times)
  8. Run it.
  1. 用于apktool从apk中取出资源文件
  2. 使用dex2jar获取包含在Eclipse将喜欢的格式类的jar文件。
  3. 创建一个Eclipse项目指向它的资源文件和新的jar文件
  4. 使用 zip 实用程序打开 jar 文件并删除现有资源
  5. 打开jar文件JDGui查看源码
  6. 获取您需要的任何源代码JDGui,将其粘贴到 Eclipse 中的一个类中并对其进行修改
  7. 从 jar 文件中删除该类(这样您就不会多次定义相同的类)
  8. 运行。

回答by Ryan Berger

apktool will work. You don't even need to know the keystore to extract the source code (which is a bit scary). The main downside is that the source is presented in Smali format instead of Java. Other files such as the icon and main.xml come through perfectly fine though and it may be worth your time to at least recover those. Ultimately, you will most likely need to re-write your Java code from scratch.

apktool 将工作。您甚至不需要知道密钥库来提取源代码(这有点吓人)。主要的缺点是源代码以 Smali 格式而不是 Java 格式呈现。其他文件,例如 icon 和 main.xml,虽然可以正常运行,但至少值得花时间恢复这些文件。最终,您很可能需要从头开始重新编写 Java 代码。

You can find apktool here. Simply just download apktool and the appropriate helper (for Windows, Linux, or Mac OS). I recommend using a tool such as 7-zip to unpack them.

你可以在这里找到 apktool 。只需下载 apktool 和相应的帮助程序(适用于 Windows、Linux 或 Mac OS)。我建议使用诸如 7-zip 之类的工具来解压缩它们。

回答by SKK

May be the easy one to see the source:

可能是最容易看到的来源:

In Android studio 2.3, Build -> Analyze APK -> Select the apk that you want to decompile.
You will see it's source code.

在 Android Studio 2.3 中,Build -> Analyze APK -> Select the apk that you want to decompile.
你会看到它的源代码。

Link for reference:
https://medium.com/google-developers/making-the-most-of-the-apk-analyzer-c066cb871ea2

参考链接:https:
//medium.com/google-developers/making-the-most-of-the-apk-analyzer-c066cb871ea2

回答by 0xcaff

There are a few ways to do this:

有几种方法可以做到这一点:

  1. Use the "Profile or Debug APK" feature in Android Studio 3.0.

    It allows you to open and explore APKs in Android Studio. Classes are decompiled into smali. Resources are not extracted and things like "Go to Definition", "Find All References" and debugging don't work without the source code (android studio 3.0 canary 9). Some additional smali features might work with smalidea.

    android-studio

    select-file

    code

  2. Use jadx.

    Jadx decompiles the code in a given APK to java source files.

    jdax-img

  3. Use apktool.

    Apktool is a command line tool which extracts resources and decompiles code into smali for a given apk. You can recompile using apktool also. Here's an example of it in action:

    $ apktool d test.apk
    I: Using Apktool 2.2.4 on test.apk
    I: Loading resource table...
    I: Decoding AndroidManifest.xml with resources...
    I: Loading resource table from file: 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...
    $ apktool b test
    I: Using Apktool 2.2.4 on test
    I: Checking whether sources has changed...
    I: Smaling smali folder into classes.dex...
    I: Checking whether resources has changed...
    I: Building resources...
    I: Building apk file...
    I: Copying unknown files/dir...
    
  1. 使用 Android Studio 3.0 中的“配置文件或调试 APK”功能。

    它允许您在 Android Studio 中打开和探索 APK。类被反编译成 smali。资源不会被提取,如果没有源代码(android studio 3.0 canary 9),“转到定义”、“查找所有引用”和调试等内容将无法工作。一些额外的 smali 功能可能适用于smalidea

    安卓工作室

    选择文件

    代码

  2. 使用jadx

    Jadx 将给定 APK 中的代码反编译为 Java 源文件。

    jdax-img

  3. 使用apktool

    Apktool 是一个命令行工具,它为给定的 apk 提取资源并将代码反编译为 smali。您也可以使用 apktool 重新编译。这是它的一个例子:

    $ apktool d test.apk
    I: Using Apktool 2.2.4 on test.apk
    I: Loading resource table...
    I: Decoding AndroidManifest.xml with resources...
    I: Loading resource table from file: 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...
    $ apktool b test
    I: Using Apktool 2.2.4 on test
    I: Checking whether sources has changed...
    I: Smaling smali folder into classes.dex...
    I: Checking whether resources has changed...
    I: Building resources...
    I: Building apk file...
    I: Copying unknown files/dir...
    

回答by Kamil Kie?czewski

Below ONLINEtool:

在线工具下方:

http://www.javadecompilers.com/apk

http://www.javadecompilers.com/apk

it do ALL by one click: decompiled .java files + resources + xml(in one .zip file) with very good decompiler (jadx return java code in places/functions where other compiles return comments inside function like "unable to decompile" or some android assembler/machine code)

它通过一键完成所有操作:使用非常好的反编译器反编译.java 文件 + 资源 + xml(在一个 .zip 文件中)(jadx 在其他编译的地方/函数中返回 java 代码,在函数内返回注释,例如“无法反编译”或某些android汇编程序/机器代码)