1465

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?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Frank Bozzo
  • 15,033
  • 6
  • 25
  • 29
  • 36
    FYI There is online decompiler check [here](http://stackoverflow.com/a/21670560/1450401) – edwin Feb 10 '14 at 06:44
  • http://stackoverflow.com/questions/12259460/decompling-an-android-apk – claws Mar 12 '14 at 19:12
  • apk extractor app https://play.google.com/store/apps/details?id=com.ext.ui&hl=en – Meher May 31 '14 at 07:08
  • 17
    Does this means we can get source code of million dollar worth apps like Whatsapp or Facebook? Is this all open source? – Faizan Jan 10 '15 at 20:15
  • 6
    @Faizan have you heard about proguard?? – Vishwajit Palankar Jun 09 '15 at 08:58
  • @SwiftArchitect - it is not deleted, it is still here for reference. What's your problem with closing it? – dtech Feb 12 '16 at 13:34
  • 3
    The phrasing of the question does contain irrelevant life story tidbits and as such may be interpreted as *recommend a tool*. Yet the answer could have potentially been `javac -decompile`, which would then fit SO guidelines. It appears to me that the answers are *off-topic*, not the question. – SwiftArchitect Feb 12 '16 at 15:04
  • Try the online decompilers.. for more info - http://stackoverflow.com/a/27981520/3879847 – Ranjithkumar Mar 11 '16 at 11:58
  • 2
    in [this article](https://applidium.com/en/news/unbundling_pokemon_go/) they explain how they reverse engineer Pokemon Go – anders Aug 17 '16 at 10:31
  • Possible duplicate of [Is it possible to decompile an Android .apk file?](https://stackoverflow.com/questions/3122635/is-it-possible-to-decompile-an-android-apk-file) – ishandutta2007 Sep 20 '17 at 08:20
  • keep in mind that by using online decompilers, your code may be compromised. – htafoya Sep 21 '21 at 15:32
  • You can use one of [these methods](https://droidmaze.com/how-to-extract-code-from-apk/) to decompile apps and get the code. – w3Abhishek Feb 17 '22 at 01:47

31 Answers31

1758

Simplest way: use the online tool Decompiler, upload the apk and get the 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 (rename from filename.apk to filename.zip) and save it. Now you can access the classes.dex files. At this stage, you are able to see drawables, but not the .xml and .java files.

Step 2:

  1. Now extract this .zip file in the same folder or a new folder.

  2. Download dex2jar (Don't download the code, click on the releases button that's on the right, then download the file named dex2jar-X.X.zip) and extract it to the same folder or a new folder.

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

  4. Now open Command Prompt and change the directory to that folder. Then write d2j-dex2jar classes.dex (for Mac 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, Right click on jd-gui, click on Open File, and open classes.dex.dex2jar file from that folder: Now you get the 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 code but the .xml files are still unreadable.

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 the Command Prompt

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

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

Now you get a file folder in that folder and can easily read the .xml files.

Step 4:

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

AND ENJOY THE SOURCE CODE!

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
Prankul Garg
  • 3,416
  • 1
  • 17
  • 11
  • 2
    Thanks a lot but i have a problem after doing 3rd step all files comes n the smali format how can i read it. – Amit Thaper Jul 19 '11 at 09:42
  • 26
    @prankulgarg ,In point 5 of step 3 i am getting following error: Unable to access jarfile E:\apktojava\testt\\apktool.jar –  Jan 30 '12 at 07:00
  • 2
    got the problem of "jd-gui.exe has encountered a problem and needs to close. We are sorry for the inconvenience." at the time saving all the java source code.. – kamal_tech_view Mar 06 '12 at 06:05
  • 4
    i have completed this steps but java file name is a d c b....it's posible real java file name...? – Android Mar 13 '12 at 13:02
  • 7
    hi, i had try this Step but i m not getting classes.dex.dex2jar file. can u help me please? – Hasmukh Mar 15 '12 at 06:43
  • 4
    So where does one get the `framework-res.apk` file.. Am unable to find a trusted copy :| – Samuel Katz Jul 23 '12 at 14:48
  • 2
    Second file http://java.decompiler.free.fr/?q=jdgui is unavailable on this link.. what should i do? – Sumit Aug 18 '12 at 11:36
  • 1
    How to do this in Ubuntu i didn't get clear idea getting problem to do this can any one help? – Yog Guru Aug 31 '12 at 10:31
  • 1
    @prankulgarg where do we find "framework-res.apk" ? ? Google comes up with nothing ?? – Sir Sep 23 '12 at 03:24
  • 5
    dex2jar is not recognized as an internal or external command. How can i solve it? – Anil Kocabiyik Feb 03 '13 at 23:36
  • 2
    @user1451549 if you are using linux then use `./dex2jar` – Juned Feb 04 '13 at 14:08
  • 2
    I got an "Internal Error" for a main class... any suggestions? – Vnge Feb 27 '13 at 05:33
  • 2
    while going through second step i got dex2jar is not recognised am using wondows.. any idea..?? – elsa Mar 22 '13 at 06:50
  • 62
    Instead of writing **dex2jar classes.dex** in cmd try out **d2j-dex2jar classes.dex**. Because dex2jar classes.dex this cmd is deprecated. – Umesh Apr 12 '13 at 11:21
  • I cant extract .aidl file and interfaces included in the project? Is there any other way for that? – Shraddha Shravagi Apr 15 '13 at 11:55
  • 5
    its just useless in my case. Also tried all suggestions in comments. Wasted my lot of time for nothing. I'm getting './dex2jar: No such file or directory' and 'd2j-dex2jar: command not found' errors. – Shirish Herwade Sep 11 '13 at 09:42
  • 4
    If you are seeing class names like 'a', 'b', 'c', 'd', you are trying to reverse engineer someone else's ProGuarded application. Good luck with that. – Jacob Tabak Feb 23 '14 at 23:42
  • 2
    For Step 3 of Point 5 , **error: Unable to access jarfile** I found this http://stackoverflow.com/a/15336703/1778834 – yashhy Mar 02 '14 at 15:50
  • I can't download framework-res.apk file at 4shared.com. It requires an annoying download manager. – Alston Jun 18 '14 at 07:12
  • Extract dex2jar-0.0.9.8.zip and copy classes.dex, which is use ./d2j-dex2jar.sh classes.dex command instead of dex2jar classes.dex , because that dex2jar classes.dex is deprecated – sandeepmaaram Jul 28 '14 at 09:03
  • 25
    For those who can't find `framework-res.apk`. Download it from [here](http://www.androidfilehost.com/?fid=23212708291677144) – atulkhatri Jul 31 '14 at 13:50
  • In Step 2 after src is generated when i try to compile most of java source files are showing compile time error "cannot be resolved to a type". – Ajay Takur Dec 18 '14 at 22:48
  • Step 2 command failed, From documentation (https://code.google.com/p/dex2jar/wiki/ModifyApkWithDexTool) i successfully run this : # d2j-dex2jar.sh -f -o test_apk-debug_dex2jar.jar test_apk-debug.apk – Asad kamran Jan 04 '15 at 13:04
  • 1
    in my case Problem during step 4 of dex2jar : file or folder not found. Resolution:1. type bash in the command prompt 2. bash shell opens 3. go to the folder in dex2jar and run the cmd: bash d2j-dex2jar.sh classes.dex and the jar file will be generated –  Jan 06 '15 at 06:22
  • 1
    For those 5th step of 3rd step: [apktoolJARfile](http://connortumbleson.com/2014/11/27/apktool-rc3-released/) download here the jar file. Because command prompt says "unable to acces jar file C:\...." That jar file is on the link. Other steps is same. – Jaky71 Jan 07 '15 at 23:44
  • 4
    can't recover XML files, getting this error when running; apktool if framework-res.apk: Exception in thread "main" brut.androlib.AndrolibException: java.io.FileNotFound Exception: framework-res.apk (The system cannot find the file specified) at brut.androlib.res.AndrolibResources.installFramework(AndrolibResource s.java:406) at brut.androlib.Androlib.installFramework(Androlib.java:365) at brut.apktool.Main.cmdInstallFramework(Main.java:185) at brut.apktool.Main.main(Main.java:61)... [truncated] – Dut A. Feb 03 '15 at 11:02
  • 1
    This dex2jar link seems incomplete...It doesn't contain dex2jar.sh – Narendra Singh May 06 '15 at 12:52
  • @DroidWormNarendra did you found solution? I also can't found this. – offset May 10 '15 at 08:28
  • 2
    @DroidWormNarendra Maybe it help you: https://dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip – offset May 10 '15 at 09:00
  • I just want to add one class after that... how do I create the apk again without using Eclipse or other IDE. – inquisitive May 19 '15 at 13:28
  • 2
    For people on mac if you get permission denied when running the d2j-dex2jar.sh put sudo bash before it and inside the file on the last line add sudo bash at the front as well – Moddl Jun 22 '15 at 18:50
  • 4
    **.d2j-dex2jar.sh** is not available in extract directory..! Same I tried with **dex2jar** as well, but is same, executable is not there. suggest me if anything I'm missing here – CoDe Jul 01 '15 at 12:35
  • is there any way to manage the code actually i am confused how to manage and make a proper project, i got the both java and xml files – Sanjeev Sangral Aug 04 '15 at 10:56
  • 1
    Hi! I might also add, that there is modern alternative to this APKTool->dex2jar->JD-GUI route! Just try open-source APK and DEX decompiler called Jadx: https://sourceforge.net/projects/jadx/files/ It has also online version here: http://www.javadecompilers.com/apk/ – Andrew Rukin Aug 25 '15 at 14:51
  • 6
    make sure you give enough permissions to sh from terminal. Thus if you get an error "permission denied" while executing sh, use the following: chmod +x d2j-dex2jar.sh chmod +x d2j_invoke.sh and then run ./d2j-dex2jar.sh classes.dex – codebased Sep 15 '15 at 01:25
  • 14
    Those who didn't find d2j-dex2jar.sh, download dex2jar from here: https://dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip – Reaz Murshed Oct 20 '15 at 04:19
  • After running ./d2j-dex2jar.sh classes.dex I got this: -bash: ./d2j-dex2jar.sh: Permission denied Solution is: sudo chmod +x d2j-dex2jar.sh – codebased Nov 24 '15 at 12:34
  • after all these steps you may get encoded java codes,packages etc(at complexity level pro). and your efforts will be in vein. if the apk have security like progaurd. – Devendra Singh Dec 23 '15 at 18:41
  • 17
    For people stuck in step 2:4 download this version and it will solve the problem: https://sourceforge.net/projects/dex2jar/files/dex2jar-2.0.zip/download it did for me. @delive – siriuseteor77 Sep 29 '16 at 10:36
  • Here is the latest location to get d2j-d2x2jar.sh, in the dex-tools*.zip file: https://github.com/pxb1988/dex2jar/releases – ywu Feb 15 '17 at 19:57
  • If you get "unknown command" error in Windows , you need to run cmd.exe as Administrator – d4c0d312 Mar 18 '17 at 22:59
  • It is worth noting for future readers that the website offered in the answer produced the exact same result as the rest of the answer - so pl. don't waste time trying both. It is also worth noting that the code is full of random values like "findViewById(16908290)" and "class C07461" - and is therefore mostly unusable. Is there a way to get the original code exactly as it was coded from these random values? Thanks. – Zac Jun 02 '17 at 03:41
  • Can XML be created completely out of any apk file? Containg all the exact same functionality? – Lealo Jul 26 '17 at 22:50
  • JD-GUI is a bit outdated now. Try some command line tools like CFR or Krakatau for newer versions of java. – toster-cx Oct 03 '17 at 21:40
  • It would be really nice, If you can teach us how to get back the key file as well, I lost one of my app key, and can not give any update to may app – Reza Hamzehei Oct 13 '17 at 06:43
  • Ubuntu users, move both 'apktool' and 'apktool.jar' files to '/usr/local/bin' folder. Open the terminal path pointing to the apk folder location type **apktool d ** – KH_AJU Nov 13 '17 at 17:01
  • I Decompiled apk using several ways but I always getting only com.android.tools folder, My source code package is missing. what is the solution for such problem? – Sharad Kale Nov 17 '17 at 07:17
  • I've tried all methods, but even with simple helloworld, although it decompile it into java, Yet, trying to open the project in android studio, and compile it back to apk, fails for many reasons (duplicatation files, etc) . Is there any method which creates java which can be compiled back to apk ? – ransh Nov 28 '17 at 13:22
  • I am getting this: `C:\Windows>apktool if framework-res.apk S: WARNING: Could not write to (C:\Users\kbs\AppData\Local\apktool\framework), u sing C:\Users\kbs\AppData\Local\Temp\ instead... S: Please be aware this is a volatile directory and frameworks could go missing, please utilize --frame-path if the default storage directory is unavailable I: Framework installed to: C:\Users\kbs\AppData\Local\Temp\1.apk` – Tushar Gogna Jun 28 '18 at 13:39
  • I get a `java.nio.file.NoSuchFileException: classes.dex` but I have put `classes2.dex` in the same folder as `d2j-dex2jar` and run the CMD from that folder. – Liggliluff Oct 21 '18 at 08:45
  • WARNING: Could not write to (C:\Users\\AppData\Local\apktool\framework), using C:\Users\\AppData\Local\Temp\ instead... S: Please be aware this is a volatile directory and frameworks could go missing, please utilize --frame-path if the default storage directory is unavailable - – MindRoasterMir Apr 10 '19 at 14:18
  • apktool if framework-res.apk --frame-path c:\get-xml\ – MindRoasterMir Apr 10 '19 at 14:19
  • Step 3 made the word "java" on files extension on the 2nd module of my app be smali. Even the java folder itself became smali. How do I fix it without renaming them one-by-one? – rminaj Jul 24 '19 at 06:40
  • I have tried ```./d2j-dex2jar.sh classes.dex ``` but got ```Permission denied ``` I am using linux – Kousalya Feb 28 '20 at 12:30
  • 1
    Question: what if I have classes.dex, classes2.dex and classes3.dex? I decompile all three? – Sampgun Apr 05 '20 at 12:11
  • I am getting "The system cannot find the path specified" while running cmd - apktool if framework-res.apk – Palash Agrawal Apr 24 '20 at 12:02
  • For those getting an error of not supported version when running the dex2jar you can get this versio which works without issues: https://github.com/DexPatcher/dex2jar/releases/download/v2.1-20171001-lanchon/dex-tools-2.1-20171001-lanchon.zip – rkachach Apr 28 '20 at 13:11
  • @Kousalya please write the command like this `sudo chmod +x ./d2j-dex2jar.sh classes.dex` – Kirtikumar A. Jul 19 '20 at 08:26
  • I got all source code but all class name is encrypted with human not understandable words like "Wwwwwwwwwwwwwwwww" :) – Ergash Masharipov Dec 07 '20 at 01:46
  • 1
    if anyone getting "permission denied: ./d2j-dex2jar.sh" on Macbook then try "sudo chmod +x ./d2j-dex2jar.sh " in terminal then try again "./d2j-dex2jar.sh classes.dex " – Basant Jan 10 '21 at 15:04
  • I have used ./d2j-dex2jar.sh classes.dex command it is showing no such file or folder. – Hammad May 22 '21 at 03:31
  • 1
    Be sure to also give permissions to invoke file `sudo chmod +x d2j_invoke.sh` – htafoya Sep 21 '21 at 15:44
  • This is not useful as I couldnt find the actual source code for a very simple app that I just created. How to get the actual code? – West Jan 05 '22 at 14:28
  • @SharadKale I got same issue too. Just use jadx from https://github.com/skylot/jadx and you'll be able to see all packages – West Jan 05 '22 at 14:37
  • in my case I couldn't get past Step 4 of 3 starting from the execution of "apktool if framework-res.apk" what I did was "java -jar apktool.jar apktool d MyApp.apk" and that's it – Gerrard Feb 10 '22 at 19:55
  • I get % sudo ./d2j-dex2jar.sh classes.dex dex2jar classes.dex -> ./classes-dex2jar.jar com.googlecode.d2j.DexException: not support version. at com.googlecode.d2j.reader.DexFileReader.(DexFileReader.java:151) at com.googlecode.d2j.reader.DexFileReader.(DexFileReader.java:211) at com.googlecode.dex2jar.tools.Dex2jarCmd.doCommandLine(Dex2jarCmd.java:104) at com.googlecode.dex2jar.tools.BaseCmd.doMain(BaseCmd.java:288) at com.googlecode.dex2jar.tools.Dex2jarCmd.main(Dex2jarCmd.java:32) – SyncMaster Jun 12 '22 at 19:26
  • when i ran apktool if framework-res.apk, i got FILE NOT FOUND exception, so after downloading framework-res.apk where should i put it in?.. – Jeyasurya U R Jun 13 '22 at 14:50
  • 'apktool' is not recognized as an internal or external command, operable program or batch file. – Vaishnavi Maske Mar 21 '23 at 12:28
  • after all the setups are sucessfull now want to recomiple as debug apk – Engr.Aftab Ufaq Jul 13 '23 at 12:19
143

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

Braiam
  • 1
  • 11
  • 47
  • 78
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • 7
    I might also add, that there is modern alternative to this APKTool->dex2jar->JD-GUI route! Just try open-source APK and DEX decompiler called Jadx: https://sourceforge.net/projects/jadx/files/ It has also online version here: http://www.javadecompilers.com/apk – Andrew Rukin Sep 08 '15 at 05:42
  • 2
    It is allowed (read: legal) to decompile *any* APK file. See the following question: http://stackoverflow.com/questions/9674557/is-it-legal-to-decompile-an-apk-and-use-part-of-its-code-in-your-app – Rakete1111 Apr 30 '17 at 10:56
  • 1
    Why those `apktool` return `smali` files? How to get `java` file? – rminaj Jul 24 '19 at 07:58
  • 2
    @AndrewRukin That link redirect to sourceforge's dex2jar page. – user202729 Dec 10 '20 at 14:13
  • I don't see the classes.dex file :( – Ontropy Jun 11 '22 at 10:49
  • @Ontropy unzip the APK file after renaming it and you should find the `classes.dex` file in the root folder. If not, you might have an optimized dex file inside - see https://www.codeproject.com/Questions/720764/Some-Apk-file-doesnt-have-classes-dex. – Trinimon Jun 18 '22 at 07:39
129

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.

Ryan Gooler
  • 2,025
  • 1
  • 13
  • 14
58

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.

Cilenco
  • 6,951
  • 17
  • 72
  • 152
30

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.

Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • You will find apktool indispensible if you want to extract resources (i.e. images from an Android application. Actually, the tool has no competitors at all!) An online version of the apktool is available here: www.javadecompilers.com/apktool – Andrew Rukin Jun 30 '17 at 21:22
26

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.
Tom Hennen
  • 4,746
  • 7
  • 34
  • 45
  • 2
    I know this question is answered still , i would like to pass a information : there is a online decompiler for android apks http://www.decompileandroid.com/ Upload apk from local machine, Wait some moments, download source code in zip format. – Snehal Masne Jun 15 '14 at 10:03
  • 2
    Sounds nice. And then the decompileandroid website also has a copy of all of your source! Clever! – DDSports Apr 12 '15 at 17:42
  • One question, I have an apk decompiled but in jd gui I dont know if there are the real classes of java, do you know if an apk is decompiled are there the real Java classes ? –  Jun 21 '18 at 17:30
23

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.

Ryan Berger
  • 9,644
  • 6
  • 44
  • 56
19

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.

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

SKK
  • 1,705
  • 3
  • 28
  • 50
19

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...
    
0xcaff
  • 13,085
  • 5
  • 47
  • 55
  • I've tried jadx, but even with simple helloworld, although it decompile it into java, Yet, trying to open the project in android studio, and compile it back to apk, fails for many reasons (duplicatation files, etc) . Is there any method which creates java which can be compiled back to apk ? – ransh Nov 28 '17 at 13:22
  • 1
    @ransh I typically use jadx to read the code then apktool to modify the code. apktool doesn't create java but it allows for re-compiling. – 0xcaff Nov 28 '17 at 21:10
  • 0xcaff, thanks for clarification. that make sense now. Becuase trying to compile a decompile java in android studio, was not working. – ransh Nov 29 '17 at 06:59
9

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)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • yes, this is great, but instead of function names, it has changed all the functions names to CS1001 and so on. So how to recognize these. and one thing more that, Can we use this decompiled apk directly as our Android studio project, Will it work? – Zia Ur Rahman Feb 27 '18 at 06:21
  • then for what purpose we use to decompile some ones apk, if we can't use their functionality in our apps. – Zia Ur Rahman Feb 27 '18 at 10:11
  • @ZiaUrRahman I don't know. However I in my case I decompile code to analyse algorithm. Do you check does decompiled code from that tool can be compiled by android studio in fully automatic way - or need some "human processing"? – Kamil Kiełczewski Feb 27 '18 at 10:58
  • 1
    I imported the decompiled project in android studio, but No gradle there and the project have many files but didn't understand until we run it. Simply it didn't work. I need to make the decompiled project fully funtional in android studio and then get some core functionality of some ones, then want to use/embed it in my own project. So is there any way that how to do this? – Zia Ur Rahman Feb 27 '18 at 11:25
  • This online tool is using Jadx that anyone can download and run in their own computer with benefit of not having ads keep blinking in your face every 5 seconds. Also it fails the same way Jadx fails – AaA Aug 08 '21 at 18:29
8

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.

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
8

based on your condition, if your android apk:

Condition1: NOT harden (by Tencent Legu/Qihoo 360/...)

Choice1: using online service

such as:

using www.javadecompilers.com

goto:

to auto decode from apk to java sourcecode

steps:

upload apk file + click Run + wait some time + click Download to get zip + unzip ->

sources/com/{yourCompanyName}/{yourProjectName}

is your expected java source code

Choice2: decompile/crack by yourself

use related tool to decompile/crack by yourself:

use jadx/jadx-gui convert apk to java sourcecode

download jadx-0.9.0.zip then unzip to got bin/jadx, then:

  • command line mode:
    • in terminal run: jadx-0.9.0/bin/jadx -o output_folder /path_to_your_apk/your_apk_file.apk
    • output_folder will show decoded sources and resources
      • sources/com/{yourCompanyName}/{yourProjectName} is your expected java sourcecode
  • GUI mode
    • double click to run jadx-0.9.0/bin/jadx-gui (Linux's jadx-gui.sh / Windows's jadx-gui.bat)
    • open apk file
    • it will auto decoding -> see your expected java sourcecode
    • save all or save as Gradle project

eg:

Condition2: harden (by Tencent Legu/Qihoo 360/...)

the main method of 3 steps:

  1. apk/app to dex
  2. dex to jar
  3. jar to java src

detailed explanation:

Step1: apk/app to dex

use tool (FDex2/DumpDex) dump/hook out (one or multiple) dex file from running app

steps:

prepare environment

dump out dex from running app

  • run FDex2 then click your apk name to enable later to capture/hook out dex

  • (in phone/emulator) run your app
  • find and copy out the dump out whole apk resources in /data/data/com/yourCompanyName/yourProjectName
    • in its root folder normally will find several dex file

Step2: dex to jar

use tool (dex2jar) convert (the specific, containing app logic) dex file to jar file

download dex2jar got dex-tools-2.1-SNAPSHOT.zip, unzip got dex-tools-2.1-SNAPSHOT/d2j-dex2jar.sh, then

sh dex-tools-2.1-SNAPSHOT/d2j-dex2jar.sh -f your_dex_name.dex

eg:

dex-tools-2.1-SNAPSHOT/d2j-dex2jar.sh -f com.xxx.yyy8825612.dex
dex2jar com.xxx.yyy8825612.dex -> ./com.xxx.yyy8825612-dex2jar.jar

Step3: jar to java src

use one of tools:

convert jar to java src

for from jar to java src converting effect:

Jadx > Procyon > CRF >> JD-GUI

so recommend use: Jadx/jadx-gui

steps:

  • double click to run jadx-gui
  • open dex file
  • File -> save all

eg:

exported java src:


More detailed explanation can see my online ebook Chinese tutorial:

crifan
  • 12,947
  • 1
  • 71
  • 56
6

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

Franklin Hirata
  • 290
  • 2
  • 12
6

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.

Andrew Rukin
  • 959
  • 11
  • 16
5

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/

Biswajit Karmakar
  • 9,799
  • 4
  • 39
  • 41
4

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

Lakshmikant Deshpande
  • 826
  • 1
  • 12
  • 30
3

Android studio offers you to analyse any apk file.

1 - From build menu choose analyse apk option and select apk file. 2 - This will result in you the classes.dex file and other files. 3 - Click on classes.dex which will give you the list of folders, packages, libraries and files. 4 - From and android studio settings install a plugin called "Dex to Jar" 5 - click on any activity file of your extracted project and choose dex to jar from the build menu.

This will result in you the actual code of your java file.

Cheers.

Rohit Patil
  • 1,615
  • 12
  • 9
3

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

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: Now extract this .zip file in the same folder (or NEW FOLDER).

Download https://github.com/pxb1988/dex2jar/releases/tag/2.0 : dex2jar-2.0

Now open command prompt and change directory to that folder (or NEW FOLDER). Then execute : d2j-dex2jar.bat classes.dex

Download this decompiler http://java-decompiler.github.io/ to decompile classes-dex2jar.jar

Step 3: Now open another new folder

Put in the .apk file which you want to decode

Download the latest version of apktool AND apktool (https://ibotpeaches.github.io/Apktool/install/) install window (both can be downloaded from the same link) and place them in the same folder

Open a command window

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

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.

Copycontents of both folders to a single folder

Done !

2

In order to decompile APK and get the code from it you have multiple online tools & I usually prefer to use Online APK Decompiler.

It is very easy to use and within few minutes you will be able to download the source code from it.

If you want to download APK from playstore and decompile. you can easily download it from it too.

Abdul
  • 356
  • 3
  • 14
2

Single click solution (Windows only)

after reading this loooooonnnnnngggg post, i have decided to create an automated process to decompile APKs in a single click,

i have decided to share it on github:
https://github.com/shaybc/apk-decompiler

simply unzip,
copy the apk you want to decompile into "apk-source" folder
and run "go.bat"
find the result in the: "apk-output" folder

thats it, enjoy

Shaybc
  • 2,628
  • 29
  • 43
1

The simplest way is using Apk OneClick Decompiler. That is a tool package to decompile & disassemble APKs (android packages).

FEATURES

  • All features are integrated into the right-click menu of Windows.
  • Decompile APK classes to Java source codes.
  • Disassemble APK to smali code and decode its resources.
  • Install APK to phone by right-click.
  • Recompile APK after editing smali code and/or resources. During recompile:
  • Optimize png images
  • Sign apks
  • Zipalign

REQUIREMENTS

Java Runtime Environment (JRE) must be installed.

You can download it from this link Apk OneClick Decompiler

Enjoy that.

Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
1

I found the following as the simplest method:

  1. Rename your app.apk to app.zip (Change extension from apk to zip)
  2. Extract the zip file into a folder
  3. Use JADX tool to read the source code, present in classes.dex file.
Masood Anwar
  • 73
  • 10
1

There's an app for that and generally takes just a few clicks and you are done. https://github.com/Nuvolect/DeepDive-Android

  1. Select Apps, under "Installed Apps" select your app. If it is not there you can load the APK.
  2. Select "Extract APK"
  3. Select "Unpack APK"
  4. Select "Decompile with Jadx". This can take a few seconds or a few minutes depending on the speed of your device

After that you can browse the source code, download it to another computer with elFinder or search through it using Lucene.

In addition to Jadx it has CFR and Fernflower decompilers.

MobileMateo
  • 424
  • 5
  • 4
1

I developed a Tool - Code Analyzer, it runs in iPhone/iPad/Mac, to analyze Java/Android files, https://decompile.io

enter image description here

enter image description here

Qing Xu
  • 2,704
  • 2
  • 22
  • 21
1

Yes, it is possible.

There are tons of software available to reverse engineer an android .apk file.

Recently, I had compiled an ultimate list of 47 best APK decompilers on my website. I arranged them into 4 different sections.

  1. Open Source APK Decompilers
  2. Online APK Decompilers
  3. APK Decompiler for Windows, Mac or Linux
  4. APK Decompiler Apps

I hope this collection will be helpful to you.

Link: https://www.edopedia.com/blog/best-apk-decompilers/

1

1. Extracting the source from a native Android application

The source code of a native Android application is not difficult to obtain, but it does require extra tools that are compatible with Windows, Mac, and Linux. My personal favorites when it comes to tools are dex2jar and JD-GUI.

If you’re unfamiliar with these tools, dex2jar will read Dalvik Executable files and convert them to the standard JAR format. JD-GUI will read JAR files and decompile the class files found in them.

To make the extraction process easier to understand, let’s start by creating a fresh native Android project:

android create project --target 19 --name TestProject --path . --activity TestProjectActivity --package com.nraboy.testproject

The above command will leave you with an Android project in your current command prompt path.

Since our project has an Activity class already included, lets jump straight into the build process. You can give me a hard time all day long about using Ant instead of Gradle or Maven, but for this tutorial I’m going to stick with it.

ant debug

The above command will create a debug build typically found at bin/TestProject-debug.apk.

Using your favorite unzip tool, extract the APK file. 7-zip is a popular tool, but if you’re on a Mac you can just run the following from the Terminal:

unzip TestProject-debug.apk

This will leave us with a bunch of files and folders, but one is important to us. Make note of classes.dex because it contains all of our source code. To convert it into something readable we need to run dex2jar on it. You can use dex2jar on Mac, Linux, and Windows, but I’m going to explain from a Mac perspective.

With the classes.dex file in your extracted dex2jar directory, run the following from the Terminal:

./dex2jar.sh classes.dex

Open the JD-GUI application that you downloaded because it is now time to decode the JAR and packaged class files. Open the freshly created classes_dex2jar.jar file and you should see something like the following:

enter image description here See how easy it was to get to the source code of your native Android APK? Now what can you do to better protect yourself?

The Android SDK ships with Proguard, which is a obfuscation module. What is obfuscation you ask?

Obfuscation via Wikipedia:

Obfuscation (or beclouding) is the hiding of intended meaning in communication, making communication confusing, willfully ambiguous, and harder to interpret.

While obfuscation will not encrypt your source code, it will make it more difficult to make sense of. With Proguard configured, run Ant in release mode and your code should be obfuscated on build.

2. Extracting the source from a hybrid Android application

The source code of hybrid applications are by far the easiest to extract. You don’t need any extra software installed on your computer, just access to the APK file.

To make things easier to understand, lets first create a fresh Apache Cordova project and then extract it. Start by doing the following:

cordova create TestProject com.example.testproject TestProject
cd TestProject
cordova platform add android

During the project creation process you are left with the default Apache Cordova CSS, HTML, and JavaScript templates. That is fine for us in this example. Let’s go ahead and build our project into a distributed APK file:

cordova build android

You’re going to be left with platforms/android/ant-build/CordovaApp-debug.apk or something along the lines of platforms/android/ant-build/*-debug.apk.

Even though this is a debug build, it is still very usable. With 7-zip or similar installed, right click the APK file and choose to extract or unzip it. In the extracted directory, you should now have access to all your web based source files. I say web based because any Java files used by Apache Cordova will have been compiled into class files. However, CSS, HTML, and JavaScript files do not get touched.

You just saw how depressingly easy it is to get hybrid application source code. So what can you do to better protect yourself?

You can uglify your code, which is a form of obfuscation.

Doing this will not encrypt your code, but it will make it that much more difficult to make sense of.

If you want to uglify your code, I recommend you install UglifyJS since it is pretty much the standard as of now. If you prefer to use a task runner, Grunt and Gulp have modules for UglifyJS as well.

Hossam Hassan
  • 795
  • 2
  • 13
  • 39
1

There are lots of applications and methods in the market to decompile the apk file into java class but if the app is compiled with ProGuard rule then you are in a big trouble because this rule will shrink all the dex files into a small character name and then you can not trace back the implementation. see https://developer.android.com/studio/build/shrink-code for mode clarification.

Happy Coding...

Rohit Patil
  • 1,615
  • 12
  • 9
0

I've been driving myself crazy for days trying to get dex2jar to work on a fedora 31 laptop against an apk that just wasn't going to work. This python 3 script did the trick in minutes and installing jd-gui made class files human readable.

http://java-decompiler.github.io/

https://github.com/Storyyeller/enjarify

specifically, here's what I ran:

# i installed apktool before the rest of the stuff, may not need it but here it is
$> cd /opt
$> sudo mkdir apktool
$> cd apktool/
$> sudo wget https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/linux/apktool
$> sudo wget https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.4.1.jar
$> sudo mv apktool_2.4.1.jar apktool.jar
$> sudo mv apktool* /usr/bin/
$> sudo chmod a+x /usr/bin/apktool*

# and enjarify
$> cd /opt
$> sudo git clone https://github.com/Storyyeller/enjarify.git
$> cd enjarify/
$> sudo ln -s /opt/enjarify/enjarify.sh /usr/bin/enjarify

# and finally jd-gui
$> cd /opt
$> sudo git clone https://github.com/java-decompiler/jd-gui.git
$> cd jd-gui/
$> sudo ./gradlew build

# I made an alias to kick of the jd-gui with short commandline rather than long java -jar blahblahblah :)
$> echo "jd-gui='java -jar /opt/jd-gui/build/launch4j/lib/jd-gui-1.6.6.jar'" >> ~/.bashrc

Now one should be able to rum the following to get class files:

$> enjarify yourapkfile.apk

And to start jd-gui:

$> jd-gui

Then just open your class files!

RJ Cole
  • 2,592
  • 3
  • 18
  • 23
0

Use this tool http://www.javadecompilers.com/

But recently, a new wave of decompilers has forayed onto the market: Procyon, CFR, JD, Fernflower, Krakatau, Candle.

Here's a list of decompilers presented on this site:

CFR - Free, no source-code available, http://www.benf.org/other/cfr/ Author: Lee Benfield

Very well-updated decompiler! CFR is able to decompile modern Java features - Java 9 modules, Java 8 lambdas, Java 7 String switches etc. It'll even make a decent go of turning class files from other JVM langauges back into java!

JD - free for non-commercial use only, http://jd.benow.ca/ Author: Emmanuel Dupuy

Updated in 2015. Has its own visual interface and plugins to Eclipse and IntelliJ . Written in C++, so very fast. Supports Java 5.

Procyon - open-source, https://bitbucket.org/mstrobel/procyon/wiki/Java%20Decompiler Author: Mike Strobel

Fernflower - open-source, https://github.com/fesh0r/fernflower Author: Egor Ushakov

Updated in 2015. Very promising analytical Java decompiler, now becomes an integral part of IntelliJ 14. (https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler) Supports Java up to version 6 (Annotations, generics, enums)

JAD - given here only for historical reason. Free, no source-code available, jad download mirror Author: Pavel Kouznetsov

Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
0

For Java files

Use the accepted answer given here

For XML files

You need not use the method given in the accepted answer as I felt it complicated.

What I did instead was to convert the APK file to ZIP and extract it. After extracting, I got a folder named res:

enter image description here

I just opened it and found many folders. For the layout, you need to find the folder named layout not any other one like layout-land or something. The same thing can be done with the other resources like drawable, menu etc...

NOTE: There will be a lot of random files also which are NOT created by you. You can find the files made by you and then use them!

halfer
  • 19,824
  • 17
  • 99
  • 186
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
-1

APK files of Android apps can be decompiled using any of the following methods:

  1. APKTool (need Java to be installed on your PC)
  2. APKTool M (Android application)
  3. JavaDecompilers (Online Tool include APKTool utility)

Source: https://droidmaze.com/how-to-extract-code-from-apk/

w3Abhishek
  • 125
  • 10