Is there a way to get the source code from an APK file?
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:
-
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 dex2jar and extract it to the same folder (or NEW FOLDER).
-
Move the classes.dex file into the dex2jar folder.
-
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. -
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.
-
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
-
Put in the .apk file which you want to decode
-
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
-
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.
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:
- download
apktool.bat
(orapktool
for Linux) andapktool_<version>.jar
from http://ibotpeaches.github.io/Apktool/install/ - rename the jar file from above to
apktool.jar
and put both files in the same folder - 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) - Start:
apktool decode [apk file]
Intermediate result: resource files,
AndroidManifest.xml
- unzip APK file with an unpacker of your choice
Intermediate result:
classes.dex
- 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= - drag and drop
classes.dex
ontodex2jar.bat
(or enter<path_to>\dex2jar.bat classes.dex
in a DOS box; for Linux usedex2jar.sh
)Intermediate result:
classes_dex2jar.jar
- unpack
classes_dex2jar.jar
(might be optional depending on used decompiler) - 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.
Frank Bozzo
Updated on August 03, 2022Comments
-
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 about 10 yearsFYI There is online decompiler check here
-
claws about 10 years
-
Meher almost 10 yearsapk extractor app play.google.com/store/apps/details?id=com.ext.ui&hl=en
-
Faizan over 9 yearsDoes this means we can get source code of million dollar worth apps like Whatsapp or Facebook? Is this all open source?
-
Vishwajit Palankar almost 9 years@Faizan have you heard about proguard??
-
dtech about 8 years@SwiftArchitect - it is not deleted, it is still here for reference. What's your problem with closing it?
-
SwiftArchitect about 8 yearsThe 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 about 8 yearsTry the online decompilers.. for more info - stackoverflow.com/a/27981520/3879847
-
anders over 7 yearsin this article they explain how they reverse engineer Pokemon Go
-
ishandutta2007 over 6 yearsPossible duplicate of Is it possible to decompile an Android .apk file?
-
htafoya over 2 yearskeep in mind that by using online decompilers, your code may be compromised.
-
w3Abhishek about 2 yearsYou can use one of these methods to decompile apps and get the code.
-
-
Amit Thaper almost 13 yearsThanks a lot but i have a problem after doing 3rd step all files comes n the smali format how can i read it.
-
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 about 12 yearsgot 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 about 12 yearsi have completed this steps but java file name is a d c b....it's posible real java file name...?
-
Hasmukh about 12 yearshi, i had try this Step but i m not getting classes.dex.dex2jar file. can u help me please?
-
Salman von Abbas almost 12 yearsSo where does one get the
framework-res.apk
file.. Am unable to find a trusted copy :| -
Sumit over 11 yearsSecond file java.decompiler.free.fr/?q=jdgui is unavailable on this link.. what should i do?
-
Yog Guru over 11 yearsHow to do this in Ubuntu i didn't get clear idea getting problem to do this can any one help?
-
Sir over 11 years@prankulgarg where do we find "framework-res.apk" ? ? Google comes up with nothing ??
-
Anil Kocabiyik about 11 yearsdex2jar is not recognized as an internal or external command. How can i solve it?
-
Juned about 11 years@user1451549 if you are using linux then use
./dex2jar
-
Jørgen R about 11 yearsWhat does this answer provide that the others don't?
-
Vnge about 11 yearsI got an "Internal Error" for a main class... any suggestions?
-
elsa about 11 yearswhile going through second step i got dex2jar is not recognised am using wondows.. any idea..??
-
Umesh about 11 yearsInstead of writing dex2jar classes.dex in cmd try out d2j-dex2jar classes.dex. Because dex2jar classes.dex this cmd is deprecated.
-
Shraddha Shravagi about 11 yearsI cant extract .aidl file and interfaces included in the project? Is there any other way for that?
-
Shirish Herwade over 10 yearsits 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 about 10 yearsIf 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 about 10 yearsFor Step 3 of Point 5 , error: Unable to access jarfile I found this stackoverflow.com/a/15336703/1778834
-
Snehal Masne almost 10 yearsI 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 almost 10 yearsI can't download framework-res.apk file at 4shared.com. It requires an annoying download manager.
-
sandeepmaaram almost 10 yearsExtract 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 over 9 yearsFor those who can't find
framework-res.apk
. Download it from here -
Ajay Takur over 9 yearsIn 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 over 9 yearsStep 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 over 9 yearsin 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 over 9 yearsFor 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. about 9 yearscan'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]
-
miva2 about 9 yearsYou don't really have to rename it to zip. I could open the apk file directly with 7-zip.
-
DDSports about 9 yearsSounds nice. And then the decompileandroid website also has a copy of all of your source! Clever!
-
Narendra Singh almost 9 yearsThis dex2jar link seems incomplete...It doesn't contain dex2jar.sh
-
offset almost 9 years@DroidWormNarendra did you found solution? I also can't found this.
-
offset almost 9 years@DroidWormNarendra Maybe it help you: dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip
-
inquisitive almost 9 yearsI just want to add one class after that... how do I create the apk again without using Eclipse or other IDE.
-
Moddl almost 9 yearsFor 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 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 over 8 yearsis 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 over 8 yearsHi! 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 over 8 yearsI 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 over 8 yearsmake 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 over 8 yearsThose who didn't find d2j-dex2jar.sh, download dex2jar from here: dex2jar.googlecode.com/files/dex2jar-0.0.9.15.zip
-
codebased over 8 yearsAfter 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 over 8 yearsafter 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 over 7 yearsFor people stuck in step 2:4 download this version and it will solve the problem: sourceforge.net/projects/dex2jar/files/dex2jar-2.0.zip/download it did for me. @delive
-
ywu about 7 yearsHere is the latest location to get d2j-d2x2jar.sh, in the dex-tools*.zip file: github.com/pxb1988/dex2jar/releases
-
d4c0d312 about 7 yearsIf you get "unknown command" error in Windows , you need to run cmd.exe as Administrator
-
Rakete1111 almost 7 yearsIt is allowed (read: legal) to decompile any APK file. See the following question: stackoverflow.com/questions/9674557/…
-
Zac almost 7 yearsIt 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 almost 7 yearsYou 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 almost 7 yearsCan XML be created completely out of any apk file? Containg all the exact same functionality?
-
WSS over 6 yearsIt lists classes and methods but does not give source code.
-
iman kazemayni over 6 yearsi couldnt retrive my java source code ... it appeare somethins like attached file and ..
-
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 over 6 yearsdo you know is multidex impact on encrypting?
-
M. Usman Khan over 6 yearsYou can get the above files only by changing ".apk" to ".zip"
-
toster-cx over 6 yearsJD-GUI is a bit outdated now. Try some command line tools like CFR or Krakatau for newer versions of java.
-
Reza Hamzehei over 6 yearsIt 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 over 6 yearsUbuntu 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 over 6 yearsI 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 over 6 yearsI'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 over 6 yearsI'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 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 over 6 years0xcaff, thanks for clarification. that make sense now. Becuase trying to compile a decompile java in android studio, was not working.
-
Zia Ur Rahman about 6 yearsyes, 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 about 6 yearsthen for what purpose we use to decompile some ones apk, if we can't use their functionality in our apps.
-
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 about 6 yearsI 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 almost 6 yearsOne 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 almost 6 yearsI 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 over 5 yearsI get a
java.nio.file.NoSuchFileException: classes.dex
but I have putclasses2.dex
in the same folder asd2j-dex2jar
and run the CMD from that folder. -
ravi152 over 5 yearsHello How to use DexPatcher with Android Studio?
-
Lakshmikant Deshpande about 5 yearsCan't explain everything in comments. Google is your friend :) There are some nice youtube tutorials as well.
-
Sunil about 5 yearsMostly online compilers are not working, But this app is working. Thank you
-
MindRoasterMir about 5 yearsWARNING: 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 about 5 yearsapktool if framework-res.apk --frame-path c:\get-xml\
-
Sajeev almost 5 years@MidhunVijayakumar could you able to to the apk to source code conversion using this?
-
rminaj almost 5 yearsStep 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 almost 5 yearsWhy those
apktool
returnsmali
files? How to getjava
file? -
Kousalya about 4 yearsI have tried
./d2j-dex2jar.sh classes.dex
but gotPermission denied
I am using linux -
Sampgun about 4 yearsQuestion: what if I have classes.dex, classes2.dex and classes3.dex? I decompile all three?
-
Palash Agrawal about 4 yearsI am getting "The system cannot find the path specified" while running cmd - apktool if framework-res.apk
-
rkachach almost 4 yearsFor 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. almost 4 years@Kousalya please write the command like this
sudo chmod +x ./d2j-dex2jar.sh classes.dex
-
Ergash Masharipov over 3 yearsI got all source code but all class name is encrypted with human not understandable words like "Wwwwwwwwwwwwwwwww" :)
-
user202729 over 3 years@AndrewRukin That link redirect to sourceforge's dex2jar page.
-
Basant over 3 yearsif 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 almost 3 yearsI have used ./d2j-dex2jar.sh classes.dex command it is showing no such file or folder.
-
AaA over 2 yearsThis 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 over 2 yearsBe sure to also give permissions to invoke file
sudo chmod +x d2j_invoke.sh
-
West over 2 yearsThis 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 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 about 2 yearsin 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 almost 2 yearsI don't see the classes.dex file :(
-
SyncMaster almost 2 yearsI 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(Dex2jarCmd.java:104) at com.googlecode.dex2jar.tools.BaseCmd.doMain(BaseCmd.java:288) at com.googlecode.dex2jar.tools.Dex2jarCmd.main(Dex2jarCmd.java:32)
-
Jeyasurya almost 2 yearswhen 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 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/….