Android silent apk update

2019-03-04 10:20发布

问题:

I would like to have a silent update in my app without any user interaction. But I always get the error code 139. The hardware is rooted! Can anyone help?

Here is the code:

public class UpdateAPK extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.updateapk);
    if (isRooted() == true) {
        Toast.makeText(UpdateAPK.this, "Hardware is rooted", Toast.LENGTH_LONG).show();
        try {
            Process install = Runtime.getRuntime().exec(new String[] {"su", "-c", "pm install -r /mnt/sdcard/app.apk"});
            install.waitFor();
            if (install.exitValue() == 0) {
                // Success :)
                Toast.makeText(UpdateAPK.this, "Success!", Toast.LENGTH_LONG).show();
            } else {
                // Fail
                Toast.makeText(UpdateAPK.this, "Failure. Exit code: " + String.valueOf(install.exitValue()), Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            System.out.println(e.toString());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {

        //Do soemthing else

    }
}

Thank you!

回答1:

I'd avoid hard-coding entire SD card path. Try something like this:

String filePath = Environment.getExternalStorageDirectory().toString() + "/your_app_directory/your_app_filename.apk";
Process installProcess = null;
int installResult = -666;

try
{
    installProcess = Runtime.getRuntime().exec("su -c pm install -r " + filePath);
}
catch (IOException e)
{
    // Handle IOException the way you like.
}

if (installProcess != null)
{
    try
    {
        installResult = installProcess.waitFor();
    }
    catch(InterruptedException e)
    {
        // Handle InterruptedException the way you like.
    }
}

if (installResult == 0)
{
    // Success!
}
else
{
    // Failure. :-/
}

Also, be careful about permissions... You could add:

<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />


回答2:

First declare this variables, then call function wherever you want. Then grant superuser, on your superuser application, check the option to always grant, for non user interaction.

final String libs = "LD_LIBRARY_PATH=/vendor/lib:/system/lib ";

final String commands = libs + "pm install -r " + "your apk directory"+ "app.apk";

instalarApk(commands);



private void instalarApk( String commands ) {
    try {

        Process p = Runtime.getRuntime().exec( "su" );
        InputStream es = p.getErrorStream();
        DataOutputStream os = new DataOutputStream(p.getOutputStream());

        os.writeBytes(commands + "\n");

        os.writeBytes("exit\n");
        os.flush();

        int read;
        byte[] buffer = new byte[4096];
        String output = new String();
        while ((read = es.read(buffer)) > 0) {
            output += new String(buffer, 0, read);
        }

        p.waitFor();

    } catch (IOException e) {
        Log.v(Debug.TAG, e.toString());
    } catch (InterruptedException e) {
        Log.v(Debug.TAG, e.toString());
    }
}