title: Installing application programmatically from Android source
body: <p>I use the following code to install an application, from my application which I have placed inside the android source code.</p>
  
  <p>I have compiled and built an emulator, I can see my application working perfect inside the emulator built from Android source.</p>
  
  <p>At a particular point in my application, I try to install another application from within my application. The APK path of the application I am trying to install is:</p>
  
  <pre><code>/data/data/com.games.golf/cache/MyGames/Golf.apk
  </code></pre>
  
  <p>Following is the code I use to install the application in the above path from my application:</p>
  
  <pre><code>    private void InstallApplicaiton(String url)
  {
      PackageParser.Package mPackage = PackageUtil.getPackageInfo(Uri.parse(url));
      installingList.addView(getProgressView(mPackage,url));
      int installFlags = 0;
      PackageManager pm = getPackageManager();
      try {
          PackageInfo pi = pm.getPackageInfo(mPackage.packageName, 
                  PackageManager.GET_UNINSTALLED_PACKAGES);
          if(pi != null) {
              System.out.println("Inside pi if not null");
              installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
          }
      } catch (NameNotFoundException e) {
          e.printStackTrace();
      }
  
  
      String installerPackageName = getIntent().getStringExtra(
              Intent.EXTRA_INSTALLER_PACKAGE_NAME);
      System.out.println("installerPackageName:"+installerPackageName);
      PackageInstallObserver observer = new PackageInstallObserver();
      pm.installPackage(Uri.parse(url), observer, installFlags, installerPackageName);
  
  }
  
      private Handler mHandler = new Handler() {
      public void handleMessage(Message msg) {
          switch (msg.what) {
              case INSTALL_COMPLETE:
                  System.out.println("package installed");
                  break;
          }
      }
  };
  class PackageInstallObserver extends IPackageInstallObserver.Stub {
      public void packageInstalled(String packageName, int returnCode) {
          Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
          msg.arg1 = returnCode;
          mHandler.sendMessage(msg);
      }
  }
  </code></pre>
  
  <p>I get SOP message inside the handler from <code>PackageInstallObserver</code> that the package is installed. But I am unable to see the installed application in launcher.</p>
  
  <p>I am aware of the fact that, third party applications cannot install / uninstall APKs programmatically. For this reason, I have compiled and built an emulator by including my application, so that I can install another applications from my application. But here also when I get stumped with the code above, I am clueless of the reason.</p>
  
  <p>I have used the same code which android uses for installing packages, but why the above code does not work and the package is not getting installed is what I am not able to understand.</p>
  
  <p>The only problem I could notice here is that <code>String installerPackageName</code> is always null.</p>
  
  <p>Any help in this regard which would help me resolve this issue , such that the package gets installed from my application, would be much appreciated.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Following is the log. Is it because that, I try to install this from emulator and that too from cache folder - <code>/data/data/com.games.golf/cache</code> . Has it got something to do with permission in accessing this folder?</p>
  
  <pre><code>05-29 19:31:57.237: W/asset(7490): Asset path     /data/data/com.games.golf/cache/MyGames/Golf.apk is neither a directory nor file (type=0).
  05-29 19:31:57.237: W/DefContainer(7490): Failed to parse package
  05-29 19:31:57.237: W/ActivityManager(79): No content provider found for permission revoke: /data/data/com.games.golf/cache/MyGames/Golf.apk
  05-29 19:31:57.297: I/ActivityManager(79): Displayed com.games.golf/.screens.PackageInstaller: +448ms
  05-29 19:31:57.607: D/dalvikvm(79): GC_EXPLICIT freed 71K, 11% free 8410K/9415K, paused 4ms+7ms
  </code></pre>