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);
- 
 1This would require root, as you can't read another process/app's resources, much less their binary as your own app's uid. Just my two cents. PD: Be ready to reimplement a couple classes, Android's Java API does not replicate the full desktop API setMachinarius– Machinarius2015年05月23日 00:33:28 +00:00Commented May 23, 2015 at 0:33
- 
 @Machinarius, It would only require root for private apps. Check out play.google.com/store/apps/details?id=com.njlabs.showjava. I got it working myself a couple months ago but dex2jar runs super slow on Android.Jared Rummler– Jared Rummler2015年05月24日 08:38:19 +00:00Commented May 24, 2015 at 8:38
- 
 I was completeley sure Android's security model wouldn't let you do that without root. I stand corrected. Good job on achieving that, Congratulations. As for performance enhancements... That would require specifically meddling with dex2java's code to make it multithreaded and such.Machinarius– Machinarius2015年05月25日 09:48:54 +00:00Commented May 25, 2015 at 9:48
- 
 1Have you seen this < stackoverflow.com/questions/1249973/… >user978144– user9781442015年05月25日 16:08:12 +00:00Commented May 25, 2015 at 16:08
2 Answers 2
You can browse the sourcecode of the ShowJava application here. It looks like he is using the the Dex2Jar tool. The decompilation starts in a BackgroundService 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 ExtractJar and the DecompileJar method than it shouldn't be to hard to understand what's going on.
Comments
You need to follow some steps in order to get it working:
- Got to the dex2jar bitbucket site and download the files. Add the jar files to your project.
- Via the DexFileFactory, 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 Procyon, which seems really simple. Load the class file with an InputTypeLoaderand then callDecompiler.decompile().
Of course you can use other libraries in step 3, but for step 1 and 2, I haven't found any alternatives.