vendredi 22 mai 2015

How can I programatically decompile dex files within Android?

I know how to extract apk files using a windows based system as below:

As we now know that apk file is just a zip file containing all program resource file, we can now get java code from apk files with ease. Following are steps to get java code from apk files.

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 accomplish the above programatically within Android. So that my application could potentially read the source code of another application.

So far I know how to get the apk that I'd like to extract to classes, as below. But other than that the above is very complicated, so some 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);

Aucun commentaire:

Enregistrer un commentaire