美文网首页
Android静默安装与静默卸载(系统应用)

Android静默安装与静默卸载(系统应用)

作者: MiHomes | 来源:发表于2019-03-27 15:26 被阅读0次

    一.轰隆一声雳响,小编闪亮登场。

    本篇基于已有系统证书(从Android设备厂家获得)的情况下实现静默安装与静默卸载,可分为三部分讲解:将apk内置为系统应用,apk静默安装与apk静默卸载。

    1.将apk内置为系统应用。内置的方法有共性,也有区别。基础操作是共性,区别就在于Android4.4以上版本与Android4.4以下版本。

      A.内置为系统应用要做的第一步,就是在清单文件的根节点manifest上添加声明:android:sharedUserId="android.uid.system" 如下图
    
    image.png
        B.内置为系统应用要做的第二步,就是给没有签名过的apk文件签名。
    
          a.若设备厂家有提供.keystore(系统证书)文件,则利用Android Studio的Build选项下Grnerate Signed Bundle/apk方式直接指定
          签名文件给apk签名即可。默认密码一般都为 android 
    
          b.若没有设备厂家提供的系统证书,那么也可以从对应的Android版本中提取签名文件,只是适配性会有很大的局限。
          可参考https://blog.csdn.net/starhosea/article/details/78696460
    
        C.内置为系统应用要做的第三步,就是将声明好,签名过的apk,内置到Android系统中(划重点:内置操作所需的adb命令需要先将手机/设备
        ROOT):
    
          a. Android 4.4以下:4.4以下可利用adb命令将apk内置到 system/app 目录下,再重启系统即可生效。
          详情可参考 https://blog.csdn.net/m0_37135879/article/details/81134472
    
          b. Android 4.4以上:4.4以上版本将主要的系统应用更改到了system/priv-app目录下,导致很多小伙伴用4.4以下的方式尝试内置系统
          应用时产生了内置无效的情况。4.4以上版本与4.4以下版本内置apk为系统应用的方式是大同小异的,利用adb命令将apk内置到
          system/priv-app 目录下,再重启系统即可生效。
          详情可参考 https://blog.csdn.net/starhosea/article/details/78697007
    
        D.此步为知识补充,基础adb命令:
    
          安装:adb install apk路径
    
          重装:adb install -r 已安装过带签名的apk重装
    
          卸载:adb uninstall apk包名
    
          启动:adb shell am start -n 包名/包名.activity
    
          关闭:adb shell am force-stop 包名
    

    2.apk静默安装。

     /**
     * 21      * APK静默安装
     * 22      *
     * 23      * @param apkPath
     * 24      *            APK安装包路径
     * 25      * @return true 静默安装成功 false 静默安装失败
     * 26
     */
    public static boolean install(String apkPath) {
        String[] args = {"pm", INSTALL_CMD, "-r", apkPath};
        String result = apkProcess(args);
        Log.e(TAG, "install log:" + result);
        if (result != null
                && (result.endsWith("Success") || result.endsWith("Success\n"))) {
            return true;
        }
        return false;
    }
    
     /**
     * 57      * 应用安装、卸载处理
     * 58      *
     * 59      * @param args
     * 60      *            安装、卸载参数
     * 61      * @return Apk安装、卸载结果
     * 62
     */
    public static String apkProcess(String[] args) {
        String result = null;
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return result;
    }
    

    3.apk静默卸载。

     /**
     * 39      * APK静默卸载
     * 40      *
     * 41      * @param packageName
     * 42      *            需要卸载应用的包名
     * 43      * @return true 静默卸载成功 false 静默卸载失败
     * 44
     */
    public static boolean uninstall(String packageName) {
        String[] args = {"pm", UNINSTALL_CMD, packageName};
        String result = apkProcess(args);
        Log.e(TAG, "uninstall log:" + result);
        if (result != null
                && (result.endsWith("Success") || result.endsWith("Success\n"))) {
            return true;
        }
        return false;
    }
    

    二.若您觉得本文对您有帮助,记得点个关注哟~

    相关文章

      网友评论

          本文标题:Android静默安装与静默卸载(系统应用)

          本文链接:https://www.haomeiwen.com/subject/kcsavqtx.html