How can I programatically decompile dex files within Android?
-
I know how to extract apk files to classes using a windows based system as below: Step 1:Renaming .apk file Rename the .apk file with the extension .zip (for example let the file be "demofile.apk" then after renaming it becomes "demofile.apk.zip") Step 2:Getting java files from apk Now extract the renamed zip file in specific folder, for example let that folder be "demofolder". Now Download dex2jar from the link for windows and extract that zip file in folder "demofolder". Now open command prompt and go to the folder created in previous step and type the command "dex2jar classes.dex" and press enter.This will generate "classes.dex.dex2jar" file in the same folder. Now Download java decompiler from the link and extract it and start(double click) jd-gui.exe From jd-gui window browse the generated "classes.dex.dex2jar" file in demofolder, this will give all the class files by src name. Now from the File menu select "save all sources" this will generate a zip file named "classes_dex2jar.src.zip" consisting of all packages and java files. Extract that zip file (classes_dex2jar.src.zip) and you will get all java files of the application. Above steps will generate java files but to get xml files perform following steps. Step 3:Getting xml files from apk Download apktool and apktool install from the link and extract both files and place it in the same folder (for example "demoxmlfolder"). Place the .apk file in same folder (i.e demoxmlfolder) Now open command prompt and goto the directory where apktool is stored (here "demoxmlfolder") and type the command "apktool if framework-res.apk" Above command should result in "Framework installed ..." Now in command prompt type the command "apktool d filename.apk" (where filename is name of apk file) This will generate a folder of name filename in current directory (here demoxmlfolder) where all xml files would be stored in res\layout folder. But I would like to know how to accomplish the above programatically within Android. So my android application can simply extract another app's dex file. I don't mind if the dex file is copied elsewhere on the phone, then decompiled (it is simply the decompilation I'm stuck on) On the principle as below, that I have access to each app's dex, just not the knowledge of how to decompile it within Android. Any help would be fantastic. final PackageManager pm = getPackageManager(); PackageInfo packageInfo = null; try { packageInfo = pm.getPackageInfo("PACKAGE NAME HERE", PackageManager.GET_META_DATA); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } File file = new File(packageInfo.applicationInfo.sourceDir); Uri uri = Uri.fromFile(file);
-
Answer:
You can browse the sourcecode of the ShowJava application https://github.com/niranjan94/show-java. It looks like he is using the the Dex2Jar tool. The decompilation starts in a https://github.com/niranjan94/show-java/blob/master/app/src/main/java/com/njlabs/showjava/processor/ProcessService.java and all the magic happens in some special Jar classes. I do not know if this code comes from a library or if all this code is created by him. Look at the https://github.com/niranjan94/show-java/blob/master/app/src/main/java/com/njlabs/showjava/processor/ProcessService.java#L203 and the https://github.com/niranjan94/show-java/blob/master/app/src/main/java/com/njlabs/showjava/processor/ProcessService.java#L240 method than it shouldn't be to hard to understand what's going on.
user4920338 at Stack Overflow Visit the source
Other answers
You need to follow some steps in order to get it working: Got to the https://bitbucket.org/pxb1988/dex2jar/downloads and download the files. Add the jar files to your project. Via the http://code.google.com/p/smali/source/browse/dexlib2/src/main/java/org/jf/dexlib2/DexFileFactory.java, you can load the apk, convert it to dex (loadDexFile) and save it to a file (writeDexFile). With another library that converts jars to java-files, you can finally read the content. I found https://bitbucket.org/mstrobel/procyon/wiki/Decompiler%20API, which seems really simple. Load the class file with an InputTypeLoader and then call Decompiler.decompile(). Of course you can use other libraries in step 3, but for step 1 and 2, I haven't found any alternatives.
Manu
Related Q & A:
- How can I pause and restart animations in Android?Best solution by Stack Overflow
- How can I open the attached files?Best solution by Yahoo! Answers
- How can I send big music files from my computer to my friends by email?Best solution by Yahoo! Answers
- How can i open the .avi files at sony vegas?Best solution by Yahoo! Answers
- How can I find my pdf files?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.