5

I want to install .apk silently in background by BusyBox command. I`ve seen some similar questions like THIS, but I still cant get working my code properly...

I have:

  1. My .apk I need to install on /sdcard/download/app.apk
  2. Root
  3. BusyBox installed

Code (not working):

String sss = Environment.getExternalStorageDirectory() + "/download/" + "app.apk";
Process install;
install = Runtime.getRuntime().exec("/system/xbin/busybox pm install " + sss); 
int success = install.waitFor();

If I use "install" instead of "pm install" it copies file well.

P.S. Code above is executing in AsyncTask. No errors, but also nothing happens... Please help!

Also I tried this, but I`m getting exit value 139 and no result:

        Process process;
        process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("pm install /mnt/sdcard/app.apk\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();

        int i = process.waitFor();
Community
  • 1
  • 1
yuralife
  • 1,545
  • 2
  • 21
  • 35
  • Apps can't install other apps. You need to execute the install command with root permissions (su). This is already mentioned in the answer of the question you link to tin your question. – Robert Jan 18 '13 at 13:05
  • @Robert I just tried to execute su before install: install = Runtime.getRuntime().exec("su"); It checks root permission but don`t install .apk – yuralife Jan 18 '13 at 14:38
  • It works through PC ADB: 1."adb shell"; 2."pm install /mnt/sdcard/download/app.apk". But I can`t repeat it programmatically by executing commands. My Android OS is ICS. – yuralife Jan 21 '13 at 11:50
  • Your app doesn't get root permission by executing `su`. You have to create a command line that contains su and your command: `su -c 'pm install ...'` – Robert Jan 21 '13 at 12:23
  • @Robert I understand that, but I can`t write right commands. Can you help me? I tried this: process = Runtime.getRuntime().exec("su"); os.writeBytes("pm install /mnt/sdcard/app.apk\n"); But it doesn`t works. – yuralife Jan 23 '13 at 16:24
  • I tried also such executing: process = Runtime.getRuntime().exec(new String[] { "su", "-c", "pm install /mnt/sdcard/app.apk\n" }); but still get EXIT VALUE 139 and the .apk is not installed. – yuralife Jan 24 '13 at 11:43
  • Take a look at the answer to this question: http://stackoverflow.com/a/7102780/624109 – Muzikant Feb 22 '13 at 13:52
  • Thanks for link, but that`s just the same code. I`ve tried it on two devices: Samsung Galaxy Gio (2.3.7) and Samsung Galaxy Tab 2 7.0 (4.0.3). Gio - everything goes fine, but Tab - nothing happens after executing =( Something bad with ICS... Any suggestions? – yuralife Feb 27 '13 at 12:00

3 Answers3

3

maybe this code will help you

Process p = null;
try
{
    p = Runtime.getRuntime().exec("su");
    DataOutputStream outs=new DataOutputStream(p.getOutputStream());

    String cmd="pm install /mnt/sdcard/app.apk";
    outs.writeBytes(cmd+"\n");
}
catch (IOException e)
{
    e.printStackTrace();
}
avrahamcool
  • 13,888
  • 5
  • 47
  • 58
Ferdiyan Syah
  • 404
  • 4
  • 6
1

After a lot of investigations on many android devices I realized that this code is correct and works!

There was just some problem with one device (Samsung Galaxy Tab 2 7.0 - 4.0.3 ICS). Maybe that is some strange feature of ICS. After updating firmware to 4.1.2 (Jelly Bean) problem has been resolved.

yuralife
  • 1,545
  • 2
  • 21
  • 35
1

You can simply use adb install command to install/update APK silently. Sample code is below

public static void InstallAPK(String filename){
    File file = new File(filename); 
    if(file.exists()){
        try {   
            String command;
            filename = StringUtil.insertEscape(filename);
            command = "adb install -r " + filename;
            Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
            proc.waitFor();
        } catch (Exception e) {
        e.printStackTrace();
        }
     }
  }
ZeeShaN AbbAs
  • 1,365
  • 2
  • 15
  • 31