1

I'm trying to silently install an APK onto an Android device in an attempt to build an auto-update feature into my app. The app will download the latest apk and silently install it. This app won't be distributed on the Play store, I'm specifically building it for one type of device for a closed group of users. I've followed the instruction in this SO post to achieve a silent install but I've run into a problem.

I've signed my apk with a certificate I've received from the manufacturer of the device, and I'm trying to silently install my newly downloaded apk using the following code:

Process install = Runtime.getRuntime().exec("pm install -r " + path);
return install.waitFor();

The waitFor() method returns an exit value of 9. I have no idea what that means nor can I find any documentation online that explains what each code means. I found another SO post that explains these codes are arbitrary and up to the specific program to define, and that I ought to consult the documentation / source code of that specific program to determine what that code means. I'm unsure on how to go about doing this, I was expecting the Android documentation for the waitFor() method to explain each code. Does anyone know where I can find information about these exit codes?

Many thanks,

Tony

Community
  • 1
  • 1
tony
  • 155
  • 1
  • 2
  • 10
  • I think you have to sign your APK with a vendor key to be able to read that info. Meaning makign it into a system app or targeting only rooted phones. Could be wrong tho. – Shark Aug 22 '13 at 13:27
  • I think this one can be found here: https://android.googlesource.com/platform/libcore2/+/master/luni/src/main/java/java/lang/ProcessManager.java – g00dy Aug 22 '13 at 13:29
  • @Shark I've signed my apk with a .pem / .pk8 files I received from the vendor so I should have system-level access. I've taken a snapshot of LogCat to try shed some light on the issue that I've responded to KennyTM with. [Click here to have a look](https://www.dropbox.com/s/dtdzzjvucjunm56/photo.JPG). – tony Aug 22 '13 at 15:07
  • Ok, if you have `LOCAL_CERTIFICATE:=platform` in your Android.mk and it builds, it's probably fine. You need to build against NDK here I'm thinking - but I could be wrong yet again. It could be just that your app is lacking the permission to install new apps from an unknown source or some such.... – Shark Aug 22 '13 at 15:10
  • possible duplicate of [Exit value from java.lang.Process#waitFor()](http://stackoverflow.com/questions/18015491/exit-value-from-java-lang-processwaitfor) – demongolem Apr 30 '14 at 01:33

1 Answers1

2

The exit value depends entirely on the process you run, thus you won't find any explanation of it because it is entirely up to the program. Though you can always assume a non-zero exit value means failure.

In Android, the exit value may be one of these 2 things (see ):

  1. If the program finished normally, it is the true exit value (created using System.exit(n))
  2. If the program is killed by a signal, it is the number of that signal.

You are trying to execute pm. Checking the source code of pm shows that it always return 0 even on failure. So the 9 could only possibly mean a signal, which is SIGKILL here.

That means your execution has been killed.


Note that to execute a program with arguments it is better to use the overload with an array:

Runtime.getRuntime().exec(new String[]{"/system/bin/pm", "install", "-r", path});

Also check Install apps silently, with granted INSTALL_PACKAGES permission for how to properly install a package programmatically.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Thanks KennyTM. I believe you're correct in saying my app is being killed by a signal - I installed the aLogCat app from the Play store to determine if it would log anything and it in fact did, [I snapped a pic of it which can be viewed here](https://www.dropbox.com/s/dtdzzjvucjunm56/photo.JPG). It's unclear to me whether this exception has been thrown due to a problem with the Vendor keys I've been given to sign my application with. Is there a clear way to go about determining if my apk has been given system-level privileges? – tony Aug 22 '13 at 15:03
  • It's very hard determining whether or not your APK really has system/admin-level priviledges, usually by testing it with a feature that is known to require those priviledges. Chicken and the egg problem there... – Shark Aug 22 '13 at 15:11
  • @tony: Have you assigned the `android.permission.INSTALL_PACKAGES` permission to your APK? – kennytm Aug 22 '13 at 16:00
  • @KennyTM I have indeed. I've sent through a request to the firmware team to take a look through the error log to hopefully get some direction from them as I'm stumped. Only thing which makes sense is if the Vendor keys aren't correct - which I'm hoping the firmware team will fish out from the log. I'm thinking I ought to root this device to bypass the vendor key signing to verify the command I'm executing is correct - if it works it'll indicate the Vendor keys I've received aren't correct. – tony Aug 23 '13 at 10:16
  • @Shark What other actions would you propose I ought to try that would require system-level privileges? Take a look at my response to KennyTM - am hoping the silent install implementation will work as is after rooting the device, which to me will indicate the Vendor keys aren't correct. – tony Aug 23 '13 at 10:18
  • Try looking [here](http://stackoverflow.com/questions/7523075/xml-android-permissions-list-full) Also, I'd suggest trying to use the DUMP permission, as I believe it also requires root access... apart from that, build from the android root build system, using `mm` or `mmm`, instead of Eclipse. If you have greater access, before burining the image to your device put the APK in `/system/app` instead of `/data/app` as any APK installed in that folder will have root-rights. Though having it there isn't enough, you still need vendor keys in combination. If all else fails, try Genymotion. – Shark Aug 23 '13 at 12:12
  • Another idea would trying to install it not via commandline, but via asking the Package (and probably Application) Manager (Service?) to do it for you. – Shark Aug 23 '13 at 12:18