从 shell cmd 静默安装和自我更新 Android 应用程序

问题描述

我正在尝试让我的应用自动更新。我正在使用根植 Android 5.1 API 22 的特殊设备。

受这些帖子(Install Android system app,silent update without Google play on rooted device.How to Install android app programatically without prompt,)的启发,我尝试使用 pm install 安装我的测试应用程序,但我正在寻找放置我的 apk 的正确位置已安装。

这是我的代码

try {
    File dir = new File("/data/data/com.example.test");
    String output = ProcessUtil.runProcess("su -c pm install -r test.apk",dir);
    Log.d("script output",output);
} catch (IOException | InterruptedException e) {
    e.printstacktrace();
}

考虑 ProcessUtil.java:

package com.example.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ProcessUtil {
    Process process;
    int errCode;

    public ProcessUtil(String command,File dir) throws IOException,InterruptedException{
        this.process = Runtime.getRuntime().exec(command,null,dir);
        this.errCode = this.process.waitFor();
    }

    public int getErrCode() {
        return errCode;
    }

    public String getoutput() throws IOException {
        InputStream inputStream = process.getInputStream();
        InputStream errStream = process.getErrorStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line + System.getProperty("line.separator"));
        }

        br = new BufferedReader(new InputStreamReader(errStream));
        while ((line = br.readLine()) != null) {
            sb.append(line + System.getProperty("line.separator"));
        }
        return sb.toString();
    }


    public static String runProcess(String command,InterruptedException {
        ProcessUtil p = new ProcessUtil(command,dir);
        if (p.getErrCode() != 0) {
            // err
        }
        return p.getoutput();
    }

    public static void runProcessNoException(String command,File dir) {
        try {
            runProcess(command,dir);
        } catch (InterruptedException | IOException e) {
            // err
        }
    }
}

  • 使用 File dir = new File("/data/data/com.example.test"); 我得到了这个日志:
D/script:   pkg: test.apk
    Failure [INSTALL_Failed_INVALID_APK]
  • 使用 File dir = new File("/sdcard/Download"); 时:
java.io.IOException: Error running exec(). Command: [su,-c,pm,install,-r,test.apk] Working Directory: /sdcard/Download Environment: null
W/System.err:     at java.lang.ProcessManager.exec(ProcessManager.java:211)
...
...
W/System.err: Caused by: java.io.IOException: Permission denied
  • 但是当我从我的笔记本电脑手动操作时:adb shell pm install -r /sdcard/Download/test.apk 成功了。

我想我无法访问 /sdcard/Download 但我无法从 /data/data/com.example.test 安装 apk。 所以我的问题是:我应该将我的 apk 与我的代码一起安装在哪里?

感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)