Lotus@
2010-08-29 03:40
采纳率: 50%
浏览 1.1k

有没有从 APK 文件获取源代码的方法?

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.

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

转载于:https://stackoverflow.com/questions/3593420/is-there-a-way-to-get-the-source-code-from-an-apk-file

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

16条回答 默认 最新

  • 叼花硬汉 2013-07-05 14:37
    已采纳

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


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

    Step 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.

    Step 2:

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

    2. Download dex2jar and 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.

    Step 3:

    Now open another new folder

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

    2. Download the latest version of apktool AND apktool 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 and next

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

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

    Step 4:

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

    and enjoy the source code...

    已采纳该答案
    打赏 评论
  • derek5. 2010-08-29 03:58

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

    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, SVN and git etc.

    and how to back it up.

    打赏 评论
  • 喵-见缝插针 2010-08-29 04:02

    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.

    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.

    打赏 评论
  • 北城已荒凉 2010-08-29 12:17

    apktool is 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.

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

    打赏 评论
  • bug^君 2012-12-06 01:38

    These two articles describe how to combine the use of apktool and dex2jar to take an APK file and create an Eclipse project that can build and run it.

    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 apktool to get the resource files out of the apk
    2. Use dex2jar to 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 JDGui to 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.
    打赏 评论
  • ℡Wang Yan 2013-02-25 12:22

    I'll show you other way to decompile the .apk files.

    You can follow the first 2 steps from "prankul garg". So you have another opportunities:

    Step 3':

    Download the "JD-GUI", thats easy to found this one. Open your .jar file in "jd-gui.exe". (File > Open file > 'found your .jar file'). After this procedure, you can save all resources in a .zip file.

    So,

    1st - You have to rename the .apk file to .zip

    2nd - You have to decode .dex file (if you want, decode the .apk file to dex2jar, that's possible)

    3rd - You have to decode .jar file with JD-GUI

    打赏 评论
  • python小菜 2013-03-13 20:09

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

    1. download apktool.bat (or apktool for Linux) and apktool_<version>.jar from http://ibotpeaches.github.io/Apktool/install/
    2. rename the jar file from above to apktool.jar and 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.zip from http://code.google.com/p/dex2jar/downloads/detail?name=dex2jar-0.0.9.15.zip&can=2&q=
    7. drag and drop classes.dex onto dex2jar.bat (or enter <path_to>\dex2jar.bat classes.dex in 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-GUI or DJ Decompiler)

      Result: source code

    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

    打赏 评论
  • 斗士狗 2014-01-11 12:49

    There is also a new application on the Play Store with 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.

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

    打赏 评论
  • from.. 2016-05-15 08:21

    Apktool for reverse engineering 3rd party, closed, binary Android apps.

    It can decode resources to nearly original form and rebuild them after making some modifications.

    It makes possible to debug smali code step by step. Also it makes working with an app easier because of project-like file structure and automation of some repetitive tasks like building apk, etc.

    http://ibotpeaches.github.io/Apktool/

    打赏 评论
  • 妄徒之命 2017-05-25 14:06

    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.

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

    打赏 评论
  • 衫裤跑路 2017-06-10 14:36

    Below ONLINE tool:

    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)

    打赏 评论
  • elliott.david 2017-07-11 05:51

    apktool is THE way to go. Online apktool service exists as well: http://www.javadecompilers.com/apktool

    Some limitations, obviously, exist due to the service ‘online nature’: you may extract and research assets and the manifest file, but it is impossible to recompile the application at the moment.

    Still, this is a no-hassle way to 'open' the android application.

    打赏 评论
  • 斗士狗 2017-08-11 03:27

    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...
      
    打赏 评论
  • lrony* 2017-09-11 06:20

    This site https://www.apkdecompilers.com/ did it automatically.

    I tried the site mentioned in the accepted answer first but that didn't work for me.

    打赏 评论
  • 10.24 2017-10-04 06:33

    I personally recommend Show Java Android App to get the source code. You can download it from play store or from here

    打赏 评论
  • 零零乙 2017-11-16 10:53

    You can try DexPatcher. It even integrates with Android Studio. It uses Apktool and Dex2Jar internally.
    You can use those tools independently as well. Apktool decompiles apk, and extracts .dex files, which can further be converted to jar using Dex2Jar. Jar can be decompiled by using JD-GUI. You can see the Java code with the help of that tool. Although the similarity of decompiled code to the actual code cannot be guaranteed. There are some advanced code obfuscation tools available in the market, which mess up the code to make it difficult to decompile / understand. eg. Proguard

    打赏 评论

相关推荐 更多相似问题