以编程方式安装 APK 时出现异常

Exception with Install APK programmatically

我正在尝试为 Google Glass 开发一个 android 应用程序,它可以从远程服务器中提取 apk,卸载并重新安装。

Glass 已 root,我在 /system/app 中安装了安装程序应用程序,我通过从证书和 pk8 文件创建密钥库文件对应用程序进行了签名,

密钥库文件,

  1. platform.pk8
  2. 平台。x509.pem

https://android.googlesource.com/platform/build/+/android-4.4.2_r2.0.1/target/product/security

我按照安装程序 apk 中的代码进行安装,

private void install(String result) throws Exception{
    text.setText("Installing...");
    final String libs = "LD_LIBRARY_PATH=/vendor/lib:/system/lib ";
    final String[] commands = {
            libs + "pm install --user -1 -r " + result
            //        ,libs + "am start -n " + context.getPackageName() + "/" + get_main_activity()
    };


    File file = new File(result);
    if (file.exists()){
        Log.d(TAG,"Found file" + file.getAbsolutePath());
    }else{
        Log.d(TAG, "File not found");
    }




   execute_as_root(commands);


}

private void execute_as_root( String[] commands ) throws Exception{
    try {
        // Do the magic
        Process p = Runtime.getRuntime().exec( "su" );
        InputStream es = p.getErrorStream();
        DataOutputStream os = new DataOutputStream(p.getOutputStream());

        for( String command : commands ) {
            //Log.i(TAG,command);
            os.writeBytes(command + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
        os.close();

        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();

        Log.e(TAG, output.trim() + " (" + p.exitValue() + ")");
        text.setText("Done");


    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    } catch (InterruptedException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }finally{
        manager.release();
        finish();
    }
}

我遇到以下异常

java.lang.SecurityException: Permission Denial: broadcast asks to run as user -1 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS
                                                                                           at android.os.Parcel.readException(Parcel.java:1465)
                                                                                           at android.os.Parcel.readException(Parcel.java:1419)
                                                                                           at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2373)
                                                                                           at com.android.commands.am.Am.sendBroadcast(Am.java:810)
                                                                                           at com.android.commands.am.Am.onRun(Am.java:285)
                                                                                           at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
                                                                                           at com.android.commands.am.Am.main(Am.java:77)
                                                                                           at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
                                                                                           at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
                                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                                       Permission denied (1)

清单权限

<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.DELETE_PACKAGES"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>

想通了,

只需将 APK 放入 /sys/priv-app 文件夹即可。