15

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    This 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 set – Machinarius May 23 '15 at 00:33
  • @Machinarius, It would only require root for private apps. Check out https://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 May 24 '15 at 08: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 May 25 '15 at 09:48
  • 1
    Have you seen this < http://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode > – user978144 May 25 '15 at 16:08

2 Answers2

9

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.

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

You need to follow some steps in order to get it working:

  1. Got to the dex2jar bitbucket site and download the files. Add the jar files to your project.
  2. Via the DexFileFactory, you can load the apk, convert it to dex (loadDexFile) and save it to a file (writeDexFile).
  3. 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 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.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76