title: Android development RecoverySystem.installPackage() cannot write to /cache/recovery/command permission denied
body: <p>I have been asked to write a small simple app for an Android-based product. The device comes with two Android system images with different features. The app I'm writing is just a proof of concept where when you click on a button, it uses the Recovery System to replace the current OS with one of the images.</p>
  
  <p>The device is rooted and the application runs as a system app.</p>
  
  <p>I use </p>
  
  <pre><code>RecoverySystem.installPackage(context, packageFile);
  </code></pre>
  
  <p>(<a href="http://developer.android.com/reference/android/os/RecoverySystem.html#installPackage%28android.content.Context,%20java.io.File%29" rel="noreferrer">see here for reference</a>) to replace the OS with one of the images. This should reboot the system and initialize the recovery system to install the image.</p>
  
  <p>The problem that I have is that this call fails because the <code>RecoverySystem.installPackage</code> method can't seem to access the /cache/recovery/command file. I guess it tries to write some commands for the recovery system to be executed on reboot, but fails. Here is the exception I am getting:</p>
  
  <pre><code>02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439): java.io.FileNotFoundException: /cache/recovery/command: open failed: EACCES (Permission denied)
  02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439):  at libcore.io.IoBridge.open(IoBridge.java:416)
  02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439):  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:88)
  02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439):  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:73)
  02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439):  at java.io.FileWriter.&lt;init&gt;(FileWriter.java:42)
  02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439):  at android.os.RecoverySystem.bootCommand(RecoverySystem.java:381)
  02-27 16:44:39.463: E/BroadSign Resolution Switcher(4439):  at android.os.RecoverySystem.installPackage(RecoverySystem.java:330)
  </code></pre>
  
  <p>So, I am assuming that I don't have the proper permission to access this file. Though here are the permission that I have set in my manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.REBOOT" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" /&gt;
  &lt;uses-permission android:name="android.permission.DELETE_CACHE_FILES" /&gt;
  </code></pre>
  
  <p>I know I need the REBOOT permission for the <code>RecoverySystem</code>. However, I don't know if the other two are relevant. I don't even know if I need some other permission to create/write to files in the /cache partition.</p>
  
  <p>Does anybody know what I could be missing?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Well, it looks like I figured it out. The permissions were right after all. Everything was right.</p>
  
  <p>When I was setting up the app as a system app by moving the apk file to /system/app I hadn't put all the permissions in yet. When I ran the latest code on my device, it installed it in /data/app and recognized it as an update to the system app. Because of that, only the permissions of the original system app were recognized.</p>
  
  <p>After I re-set up my latest code to run from /system/app directly, it worked.</p>
  
  <p>Now the only issue I have is that when it reboots it doesn't install the image and I end up with a dead green robot with an exclamation mark. I'll keep investigating.</p>
  
  <p>I hope this post helps anyone with the same issue. I'll be happy to answer any questions as well.</p>