安装 Android 系统应用程序,无提示更新 Google 在 root 设备上播放。

Install Android system app, silent update without Google play on rooted device.

我正在尝试让我的应用以新版本 apk 的形式从 ftp 站点下载更新。下载 apk 后应静默安装,无需用户的任何确认。我控制了下载部分。我也可以在用户确认的情况下执行此操作。问题是静默更新部分。

据我所知,唯一的方法是将应用程序安装为系统应用程序。这是我需要帮助的地方。

我尝试了很多东西。我取得的最大成功如下:

  1. 正在对设备进行 Root。
  2. 正在将 *android:sharedUserId="android.uid.system" 添加到清单中。
  3. 向清单添加以下权限:

    android.permission.ACCESS_SUPERUSER and android.permission.INSTALL_PACKAGES

  4. 使用 Android Studio->Build->Generate Signed APK 对 apk 进行签名...使用这样生成的签名:

    ./keytool-importkeypair -k google_certificate.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform

我从 GitHub 上的 Android 源镜像获取 pk8 和 pem 文件的地方。

  1. 将已签名的 apk 移动到设备上的 system/app 并单击安装。

我得到的第一件事是应用程序请求的大量权限列表,而我从未这样做过。所以我想这是系统应用程序拥有的权限,到目前为止还不错 :)

收到消息后的第一时间:

App not installed.

Google不知道为什么会出现这个错误,所以我在这里问。

我走的路对吗?

为什么没有安装应用程序?

如果您的设备已获得 root 权限,您可以执行此命令:

pm install com.example.myapp

如何执行这个命令?
有两种方式:

方式一:
使用 RootTools 库:

Command command = new Command(0, "pm install com.example.myapp") {
            @Override
            public void commandCompleted(int arg0, int arg1) {
                Log.i(TAG, "App installation is completed");
            }

            @Override
            public void commandOutput(int arg0, String line) {

            }

            @Override
            public void commandTerminated(int arg0, String arg1) {

            }
}
RootTools.getShell(true).add(command);

方式二:
这种方式不需要库,但比第一种方式更难。

//Start a new process with root privileges
Process process = Runtime.getRuntime().exec("su");
//Get OutputStream of su to write commands to su process
OutputStream out = process.getOutputStream();
//Your command
String cmd = "pm install com.example.myapp";
//Write the command to su process
out.write(cmd.getBytes());
//Flush the OutputStream
out.flush();
//Close the OutputStream
out.close();
//Wait until command
process.waitFor();
Log.i(TAG, "App installation is completed");

所以几年后我又遇到了那个问题,我设法解决了它。所以最重要的部分是正确地生根 phone:这是通过 SuperSU 应用程序完成的。

下载.apk文件后,使用类似下面的方法安装更新:

private Boolean Install(String path)
{
    File file = new File(path);
    if(file.exists()){
        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","pm install -r -d " + path});
            proc.waitFor();
            BufferedReader input = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String line;
            Boolean hasError = false;
            while ((line = input.readLine()) != null) {
                if(line.contains("Failure")){
                    hasError = true;
                }
            }
            if(proc.exitValue() != 0 || hasError){
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    return false;
}