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

1,342,009

Solution 1

Simple way: use online tool https://www.decompiler.com/, 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 (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)

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 folders(in this case, both new folders) to the single one

and enjoy the source code...

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
1,342,009
Frank Bozzo
Author by

Frank Bozzo

Updated on August 03, 2022

Comments

  • Frank Bozzo
    Frank Bozzo over 1 year

    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?

    • edwin
      edwin about 10 years
      FYI There is online decompiler check here
    • claws
      claws about 10 years
    • Meher
      Meher almost 10 years
    • Faizan
      Faizan over 9 years
      Does this means we can get source code of million dollar worth apps like Whatsapp or Facebook? Is this all open source?
    • Vishwajit Palankar
      Vishwajit Palankar almost 9 years
      @Faizan have you heard about proguard??
    • dtech
      dtech about 8 years
      @SwiftArchitect - it is not deleted, it is still here for reference. What's your problem with closing it?
    • SwiftArchitect
      SwiftArchitect about 8 years
      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.
    • Ranjithkumar
      Ranjithkumar about 8 years
      Try the online decompilers.. for more info - stackoverflow.com/a/27981520/3879847
    • anders
      anders over 7 years
      in this article they explain how they reverse engineer Pokemon Go
    • ishandutta2007
      ishandutta2007 over 6 years
    • htafoya
      htafoya over 2 years
      keep in mind that by using online decompilers, your code may be compromised.
    • w3Abhishek
      w3Abhishek about 2 years
      You can use one of these methods to decompile apps and get the code.
  • Amit Thaper
    Amit Thaper almost 13 years
    Thanks a lot but i have a problem after doing 3rd step all files comes n the smali format how can i read it.
  • Admin
    Admin about 12 years
    @prankulgarg ,In point 5 of step 3 i am getting following error: Unable to access jarfile E:\apktojava\testt\\apktool.jar
  • kamal_tech_view
    kamal_tech_view about 12 years
    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..
  • Android
    Android about 12 years
    i have completed this steps but java file name is a d c b....it's posible real java file name...?
  • Hasmukh
    Hasmukh about 12 years
    hi, i had try this Step but i m not getting classes.dex.dex2jar file. can u help me please?
  • Salman von Abbas
    Salman von Abbas almost 12 years
    So where does one get the framework-res.apk file.. Am unable to find a trusted copy :|
  • Sumit
    Sumit over 11 years
    Second file java.decompiler.free.fr/?q=jdgui is unavailable on this link.. what should i do?
  • Yog Guru
    Yog Guru over 11 years
    How to do this in Ubuntu i didn't get clear idea getting problem to do this can any one help?
  • Sir
    Sir over 11 years
    @prankulgarg where do we find "framework-res.apk" ? ? Google comes up with nothing ??
  • Anil Kocabiyik
    Anil Kocabiyik about 11 years
    dex2jar is not recognized as an internal or external command. How can i solve it?
  • Juned
    Juned about 11 years
    @user1451549 if you are using linux then use ./dex2jar
  • Jørgen R
    Jørgen R about 11 years
    What does this answer provide that the others don't?
  • Vnge
    Vnge about 11 years
    I got an "Internal Error" for a main class... any suggestions?
  • elsa
    elsa about 11 years
    while going through second step i got dex2jar is not recognised am using wondows.. any idea..??
  • Umesh
    Umesh about 11 years
    Instead of writing dex2jar classes.dex in cmd try out d2j-dex2jar classes.dex. Because dex2jar classes.dex this cmd is deprecated.
  • Shraddha Shravagi
    Shraddha Shravagi about 11 years
    I cant extract .aidl file and interfaces included in the project? Is there any other way for that?
  • Shirish Herwade
    Shirish Herwade over 10 years
    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.
  • Jacob Tabak
    Jacob Tabak about 10 years
    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.
  • yashhy
    yashhy about 10 years
    For Step 3 of Point 5 , error: Unable to access jarfile I found this stackoverflow.com/a/15336703/1778834
  • Snehal Masne
    Snehal Masne almost 10 years
    I know this question is answered still , i would like to pass a information : there is a online decompiler for android apks decompileandroid.com Upload apk from local machine, Wait some moments, download source code in zip format.
  • Alston
    Alston almost 10 years
    I can't download framework-res.apk file at 4shared.com. It requires an annoying download manager.
  • sandeepmaaram
    sandeepmaaram almost 10 years
    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
  • atulkhatri
    atulkhatri over 9 years
    For those who can't find framework-res.apk. Download it from here
  • Ajay Takur
    Ajay Takur over 9 years
    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".
  • Asad kamran
    Asad kamran over 9 years
    Step 2 command failed, From documentation (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
  • Admin
    Admin over 9 years
    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
  • Jaky71
    Jaky71 over 9 years
    For those 5th step of 3rd step: apktoolJARfile 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.
  • Dut A.
    Dut A. about 9 years
    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(Androli‌​bResource 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]
  • miva2
    miva2 about 9 years
    You don't really have to rename it to zip. I could open the apk file directly with 7-zip.
  • DDSports
    DDSports about 9 years
    Sounds nice. And then the decompileandroid website also has a copy of all of your source! Clever!
  • Narendra Singh
    Narendra Singh almost 9 years
    This dex2jar link seems incomplete...It doesn't contain dex2jar.sh
  • offset
    offset almost 9 years
    @DroidWormNarendra did you found solution? I also can't found this.
  • offset
    offset almost 9 years
    @DroidWormNarendra Maybe it help you: dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip
  • inquisitive
    inquisitive almost 9 years
    I just want to add one class after that... how do I create the apk again without using Eclipse or other IDE.
  • Moddl
    Moddl almost 9 years
    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
  • CoDe
    CoDe almost 9 years
    .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
  • Sanjeev Sangral
    Sanjeev Sangral over 8 years
    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
  • Andrew Rukin
    Andrew Rukin over 8 years
    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: sourceforge.net/projects/jadx/files It has also online version here: javadecompilers.com/apk
  • Andrew Rukin
    Andrew Rukin over 8 years
    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: sourceforge.net/projects/jadx/files It has also online version here: javadecompilers.com/apk
  • codebased
    codebased over 8 years
    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
  • Reaz Murshed
    Reaz Murshed over 8 years
    Those who didn't find d2j-dex2jar.sh, download dex2jar from here: dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip
  • codebased
    codebased over 8 years
    After running ./d2j-dex2jar.sh classes.dex I got this: -bash: ./d2j-dex2jar.sh: Permission denied Solution is: sudo chmod +x d2j-dex2jar.sh
  • Devendra Singh
    Devendra Singh over 8 years
    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.
  • siriuseteor77
    siriuseteor77 over 7 years
    For people stuck in step 2:4 download this version and it will solve the problem: sourceforge.net/projects/dex2jar/files/dex2jar-2.0.zip/downl‌​oad it did for me. @delive
  • ywu
    ywu about 7 years
    Here is the latest location to get d2j-d2x2jar.sh, in the dex-tools*.zip file: github.com/pxb1988/dex2jar/releases
  • d4c0d312
    d4c0d312 about 7 years
    If you get "unknown command" error in Windows , you need to run cmd.exe as Administrator
  • Rakete1111
    Rakete1111 almost 7 years
    It is allowed (read: legal) to decompile any APK file. See the following question: stackoverflow.com/questions/9674557/…
  • Zac
    Zac almost 7 years
    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.
  • Andrew Rukin
    Andrew Rukin almost 7 years
    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
  • Lealo
    Lealo almost 7 years
    Can XML be created completely out of any apk file? Containg all the exact same functionality?
  • WSS
    WSS over 6 years
    It lists classes and methods but does not give source code.
  • iman kazemayni
    iman kazemayni over 6 years
    i couldnt retrive my java source code ... it appeare somethins like attached file and ..
  • M. Usman Khan
    M. Usman Khan over 6 years
    @imankazemayni in the link above, "browse" to your APK file and click on "Decompile your APK". It'll take some time and then it should give you the source code, if the code is not Encrypted.
  • iman kazemayni
    iman kazemayni over 6 years
    do you know is multidex impact on encrypting?
  • M. Usman Khan
    M. Usman Khan over 6 years
    You can get the above files only by changing ".apk" to ".zip"
  • toster-cx
    toster-cx over 6 years
    JD-GUI is a bit outdated now. Try some command line tools like CFR or Krakatau for newer versions of java.
  • Reza Hamzehei
    Reza Hamzehei over 6 years
    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
  • KH_AJU
    KH_AJU over 6 years
    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 <your-app-name.apk>
  • Sharad Kale
    Sharad Kale over 6 years
    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?
  • ransh
    ransh over 6 years
    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
    ransh over 6 years
    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 ?
  • 0xcaff
    0xcaff over 6 years
    @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.
  • ransh
    ransh over 6 years
    0xcaff, thanks for clarification. that make sense now. Becuase trying to compile a decompile java in android studio, was not working.
  • Zia Ur Rahman
    Zia Ur Rahman about 6 years
    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
    Zia Ur Rahman about 6 years
    then for what purpose we use to decompile some ones apk, if we can't use their functionality in our apps.
  • Kamil Kiełczewski
    Kamil Kiełczewski about 6 years
    @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"?
  • Zia Ur Rahman
    Zia Ur Rahman about 6 years
    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?
  • Admin
    Admin almost 6 years
    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 ?
  • Tushar Gogna
    Tushar Gogna almost 6 years
    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
  • Liggliluff
    Liggliluff over 5 years
    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.
  • ravi152
    ravi152 over 5 years
    Hello How to use DexPatcher with Android Studio?
  • Lakshmikant Deshpande
    Lakshmikant Deshpande about 5 years
    Can't explain everything in comments. Google is your friend :) There are some nice youtube tutorials as well.
  • Sunil
    Sunil about 5 years
    Mostly online compilers are not working, But this app is working. Thank you
  • MindRoasterMir
    MindRoasterMir about 5 years
    WARNING: Could not write to (C:\Users\<user-name>\AppData\Local\apktool\framework), using C:\Users\<user-name>\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
    MindRoasterMir about 5 years
    apktool if framework-res.apk --frame-path c:\get-xml\
  • Sajeev
    Sajeev almost 5 years
    @MidhunVijayakumar could you able to to the apk to source code conversion using this?
  • rminaj
    rminaj almost 5 years
    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
    rminaj almost 5 years
    Why those apktool return smali files? How to get java file?
  • Kousalya
    Kousalya about 4 years
    I have tried ./d2j-dex2jar.sh classes.dex but got Permission denied I am using linux
  • Sampgun
    Sampgun about 4 years
    Question: what if I have classes.dex, classes2.dex and classes3.dex? I decompile all three?
  • Palash Agrawal
    Palash Agrawal about 4 years
    I am getting "The system cannot find the path specified" while running cmd - apktool if framework-res.apk
  • rkachach
    rkachach almost 4 years
    For those getting an error of not supported version when running the dex2jar you can get this versio which works without issues: github.com/DexPatcher/dex2jar/releases/download/…
  • Kirtikumar A.
    Kirtikumar A. almost 4 years
    @Kousalya please write the command like this sudo chmod +x ./d2j-dex2jar.sh classes.dex
  • Ergash Masharipov
    Ergash Masharipov over 3 years
    I got all source code but all class name is encrypted with human not understandable words like "Wwwwwwwwwwwwwwwww" :)
  • user202729
    user202729 over 3 years
    @AndrewRukin That link redirect to sourceforge's dex2jar page.
  • Basant
    Basant over 3 years
    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 "
  • Hammad
    Hammad almost 3 years
    I have used ./d2j-dex2jar.sh classes.dex command it is showing no such file or folder.
  • AaA
    AaA over 2 years
    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
  • htafoya
    htafoya over 2 years
    Be sure to also give permissions to invoke file sudo chmod +x d2j_invoke.sh
  • West
    West over 2 years
    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
    West over 2 years
    @SharadKale I got same issue too. Just use jadx from github.com/skylot/jadx and you'll be able to see all packages
  • Gerrard
    Gerrard about 2 years
    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
  • Ontropy
    Ontropy almost 2 years
    I don't see the classes.dex file :(
  • SyncMaster
    SyncMaster almost 2 years
    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.<init>(DexFileReader‌​.java:151) at com.googlecode.d2j.reader.DexFileReader.<init>(DexFileReader‌​.java:211) at com.googlecode.dex2jar.tools.Dex2jarCmd.doCommandLine(Dex2ja‌​rCmd.java:104) at com.googlecode.dex2jar.tools.BaseCmd.doMain(BaseCmd.java:288‌​) at com.googlecode.dex2jar.tools.Dex2jarCmd.main(Dex2jarCmd.java‌​:32)
  • Jeyasurya
    Jeyasurya almost 2 years
    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?..
  • Trinimon
    Trinimon almost 2 years
    @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 codeproject.com/Questions/720764/….