0

I'am trying to install apk silently without any prompt.

This is the code which installs the apk file by using adb command.

public void InstallAPK(String filename){
    File file = new File(filename);
    if(file.exists()){
        try {
            String command;
            command = "adb install -r " + filename;
            Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
            proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But when I run this code, I am getting the following error.

java.io.IOException: Error running exec(). Command: [su, -c, adb install -r /storage/emulated/0/Download/sampleapp.apk] Working Directory: null Environment: null

I have given these permissions.

<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Please can someone help me to solve this error.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Sridhar
  • 668
  • 1
  • 11
  • 22

1 Answers1

0

You can't silently run a command in an application's scope to install an apk outside that application's scope.

But you can rise an intent to make AOSP do that. Check this answer maybe: Install Application programmatically on Android

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
     .setDataAndType(Uri.parse("file:///path/to/your.apk"), 
                     "application/vnd.android.package-archive");   
startActivity(promptInstall);
Community
  • 1
  • 1
cokceken
  • 2,068
  • 11
  • 22