Android 阻断adb install

阅读更多
frameworks\base\services\core\java\com\android\server\pm\PackageManagerShellCommand.java

adb install 流程会走到PackageManagerShellCommand中的runInstall()方法,在此处做阻断操作可使 failed to install *.apk;

    private int runInstall() throws RemoteException {
        
        Log.d("Ginger", "runInstall");
        boolean refuse = SystemProperties.getBoolean("adb_install_refused", true);
        if(refuse){
            Log.d("Ginger", "refuse to install apk by adb");
            return 1;
        }
        
        final PrintWriter pw = getOutPrintWriter();
        final InstallParams params = makeInstallParams();
        final String inPath = getNextArg();
        boolean installExternal =
                (params.sessionParams.installFlags & PackageManager.INSTALL_EXTERNAL) != 0;
        if (params.sessionParams.sizeBytes < 0 && inPath != null) {
            File file = new File(inPath);
            if (file.isFile()) {
                if (installExternal) {
                    try {
                        ApkLite baseApk = PackageParser.parseApkLite(file, 0);
                        PackageLite pkgLite = new PackageLite(null, baseApk, null, null, null);
                        params.sessionParams.setSize(
                                PackageHelper.calculateInstalledSize(pkgLite, false,
                                        params.sessionParams.abiOverride));
                    } catch (PackageParserException | IOException e) {
                        pw.println("Error: Failed to parse APK file : " + e);
                        return 1;
                    }
                } else {
                    params.sessionParams.setSize(file.length());
                }
            }
        }

        final int sessionId = doCreateSession(params.sessionParams,
                params.installerPackageName, params.userId);
        boolean abandonSession = true;
        try {
            if (inPath == null && params.sessionParams.sizeBytes == 0) {
                pw.println("Error: must either specify a package size or an APK file");
                return 1;
            }
            if (doWriteSplit(sessionId, inPath, params.sessionParams.sizeBytes, "base.apk",
                    false /*logSuccess*/) != PackageInstaller.STATUS_SUCCESS) {
                return 1;
            }
            if (doCommitSession(sessionId, false /*logSuccess*/)
                    != PackageInstaller.STATUS_SUCCESS) {
                return 1;
            }
            abandonSession = false;
            pw.println("Success");
            return 0;
        } finally {
            if (abandonSession) {
                try {
                    doAbandonSession(sessionId, false /*logSuccess*/);
                } catch (Exception ignore) {
                }
            }
        }
    }

你可能感兴趣的:(android)