title: How can I programmatically decompile dex files within Android?
body: <p>I know how to extract apk files to classes using a windows based system as below:</p>
  
  <blockquote>
    <p><strong>Step 1:Renaming .apk file</strong></p>
    
    <p>Rename the .apk file with the extension .zip (for example let the file
    be "demofile.apk" then after renaming it becomes "demofile.apk.zip")</p>
    
    <p><strong>Step 2:Getting java files from apk</strong></p>
    
    <p>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".</p>
    
    <p>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.</p>
    
    <p>Now Download java decompiler from the link and extract it and
    start(double click) jd-gui.exe</p>
    
    <p>From jd-gui window browse the generated "classes.dex.dex2jar" file in
    demofolder, this will give all the class files by src name.</p>
    
    <p>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.</p>
    
    <p>Extract that zip file (classes_dex2jar.src.zip) and you will get all
    java files of the application.</p>
    
    <p>Above steps will generate java files but to get xml files perform
    following steps.</p>
    
    <p><strong>Step 3:Getting xml files from apk</strong></p>
    
    <p>Download apktool and apktool install from the link and extract both
    files and place it in the same folder (for example "demoxmlfolder").</p>
    
    <p>Place the .apk file in same folder (i.e demoxmlfolder)</p>
    
    <p>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.</p>
  </blockquote>
  
  <p>But I would like to know how to accomplish the above <strong>programatically within Android</strong>. 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)</p>
  
  <p>On the principle as below, that <strong>I have access to each app's dex</strong>, just not the knowledge of how to decompile it within Android. </p>
  
  <p>Any help would be fantastic.</p>
  
  <pre><code>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);
  </code></pre>