在root设备上安装Android系统应用,无需谷歌播放即可进行静音更新。

时间:2015-08-11 08:05:46

标签: android auto-update android-permissions

我正在尝试让我的应用程序以apk的新版本的形式从ftp站点下载更新。下载后,应该静默安装apk,无需用户确认。我控制下载部分。我也可以通过用户的确认来做到这一点。问题是静默更新部分。

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

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

  1. 生根设备。
  2. 添加* android:sharedUserId =" android.uid.system"到清单。
  3. 向清单添加以下权限:

      

    android.permission.ACCESS_SUPERUSER和android.permission.INSTALL_PACKAGES

  4. 使用Android Studio签署apk-> Build->使用如下生成的签名生成签名APK ...

      

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

  5. 我从GitHub上的Android源镜像中获取了pk8和pem文件。

    1. 将已签名的apk移至设备上的system / app,然后单击“安装”。
    2. 我得到的第一件事是应用程序请求的巨大权限列表,我从未做过。所以我想这是系统应用程序的权限,到目前为止一直很好:)

      我收到消息后立即:

        

      应用未安装。

      谷歌无法说明为什么会出现这种错误,所以我在这里问。

      我是在正确的道路上吗?

      为什么没有安装该应用?

2 个答案:

答案 0 :(得分:1)

如果您的设备是root用户,则可以执行以下命令:

pm install com.example.myapp

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

#p>方式#1:
使用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);
方式#2:
这种方式不需要库,但它比第一种方式更难。

//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");

答案 1 :(得分:1)

因此,几年后,我再次遇到了这个问题,并且设法解决了这个问题。因此,最重要的部分是使手机正确生根:这是通过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;
}
相关问题