【Android12】Android Framework系列---Adb和PMS安装apk源码流程

Adb和PMS安装apk源码流程

adb install命令

通过adb install命令可以将apk安装到Android系统(注意:特定类型的apk,比如persist类型是无法通过adb安装的)

下述命令中adb解析install命令,并调用Android PackageManagerService进行apk安装。

# -r : replace existing application
# -t : allow test packages\n"
# -d : allow version code downgrade (debuggable packages only)
adb install -r -t -d  linduo_test.apk

基于Android12,分析从adb install到 PakcageManagerService安装apk的流程。
在这里插入图片描述

源码流程分析

adb install命令的源码实现

Android中adb的源码被放在 “packages/modules/adb”这个仓库中,这个仓库编译后的主要成果物是adbd和**adb。**adbd是a服务端进程,adb是提供的客户端工具。adb和adbd两者间,通过socket完成相互间的通信。

# packages/modules/adb/Android.bp
# adbd,在Android系统中运行
cc_binary {
    name: "adbd",
    defaults: ["adbd_defaults", "host_adbd_supported", "libadbd_binary_dependencies"],
    recovery_available: true,
    apex_available: ["com.android.adbd"],

    srcs: [
        "daemon/main.cpp",
    ],
	# 省略...
}
# adb,提供给Host端(就是主机PC)
cc_binary_host {
    name: "adb",

    stl: "libc++_static",
    defaults: ["adb_defaults"],

    srcs: [
        "client/adb_client.cpp",
        "client/bugreport.cpp",
        "client/commandline.cpp",
        "client/file_sync_client.cpp",
        "client/main.cpp",
        "client/console.cpp",
        "client/adb_install.cpp",
        "client/line_printer.cpp",
        "client/fastdeploy.cpp",
        "client/fastdeploycallbacks.cpp",
        "client/incremental.cpp",
        "client/incremental_server.cpp",
        "client/incremental_utils.cpp",
        "shell_service_protocol.cpp",
    ],
    
    # 省略...
}

这里关注adb install命令对应的源码实现,adb这个bin程序对应的main函数定义在“packages/modules/adb/client/main.cpp”,main函数中调用adb_trace_init进行Trace(记录运行Log)的初始化,然后调用adb_commandline解析adb命令参数。

int main(int argc, char* argv[], char* envp[]) {
    __adb_argv = const_cast<const char**>(argv);
    __adb_envp = const_cast<const char**>(envp);
    adb_trace_init(argv);
	// 这里忽略了第一个入参(程序名自己)
    return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
}

adb_commandline函数在"packages/modules/adb/client/commandline.cpp"中实现,在这个函数中解析adb命令的参数。如果是adb install命令,会调用install_app函数进行APK安装的相关流程。

int adb_commandline(int argc, const char** argv) {
	// 省略...
	// 解析各个参数
    while (argc > 0) {
		// 解析第一个参数。就是adb命令后第一个参数
        if (!strcmp(argv[0], "server")) {
			// 省略了很多代码...
        } else {
			// adb install 的情况下走这里,跳出循环
            /* out of recognized modifiers and flags */
            break;
        }
        argc--;
        argv++;
    }

    // 省略...
    /* adb_connect() commands */
    if (!strcmp(argv[0], "devices")) {
		// 省略了很多代码(对各个命令的判断)
    } else if (!strcmp(argv[0], "install")) {
		// 判断参数为install,执行app安装相关函数
        if (argc < 2) error_exit("install requires an argument");
        return install_app(argc, argv);
    } else if (!strcmp(argv[0], "install-multiple")) {
        if (argc < 2) error_exit("install-multiple requires an argument");
        return install_multiple_app(argc, argv);
    } else if (!strcmp(argv[0], "install-multi-package")) {
        if (argc < 2) error_exit("install-multi-package requires an argument");
        return install_multi_package(argc, argv);
    } else if (!strcmp(argv[0], "uninstall")) {
        if (argc < 2) error_exit("uninstall requires an argument");
        return uninstall_app(argc, argv);
    } else if (!strcmp(argv[0], "sync")) {
		// 省略...
    }
	
	// 省略...
	// 如果输入了错误的参数,会报下面的错误,并结束此次adb命令的执行
    error_exit("unknown command %s", argv[0]);
    __builtin_unreachable();
}

install_app函数在“packages/modules/adb/client/adb_install.cpp”中定义。该函数先判断APK的安装模式是

INSTALL_PUSH/INSTALL_STREAM/INSTALL_INCREMENTAL中哪一种。默认情况下是INSTALL_PUSH模式,然后调用install_app_legacy函数进行APK安装。

  • INSTALL_PUSH:通过推送的方式安装APK,将APK整个推送到设置上进行安装。
  • INSTALL_STREAM:流式传输安装APK,APK被分割成多份并在安装过程中逐个传输到系统设置上进行组装和安装,这种方式可以减少安装的时间(如果系统支持,默认会使用这种方式安装)
  • INSTALL_INCREMENTAL:增量方式安装APK,仅安装差异的部分不需要重新安装整个Apk,加速apk安装时间。大型的APK(如GB级别)应用较多(需要系统支持)
int install_app(int argc, const char** argv) {
    InstallMode install_mode = INSTALL_DEFAULT;
    auto incremental_request = CmdlineOption::None;
    bool incremental_wait = false;

    bool use_fastdeploy = false;
    FastDeploy_AgentUpdateStrategy agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion;
	// 进行安装默认的判断,通过parse_install_mode/parse_fast_deploy_mode/calculate_install_mode实现
	// 具体实现方式请自行查看源码,这里就不关注了
    auto unused_argv = parse_install_mode({argv, argv + argc}, &install_mode, &incremental_request,
                                          &incremental_wait);
    auto passthrough_argv =
            parse_fast_deploy_mode(std::move(unused_argv), &use_fastdeploy, &agent_update_strategy);

    auto [primary_mode, fallback_mode] =
            calculate_install_mode(install_mode, use_fastdeploy, incremental_request);
	// 省略...
	// adb install默认使用INSTALL_PUSH方式安装
    auto run_install_mode = [&](InstallMode install_mode, bool silent) {
        switch (install_mode) {
            case INSTALL_PUSH:
                return install_app_legacy(passthrough_argv.size(), passthrough_argv.data(),
                                          use_fastdeploy);
            case INSTALL_STREAM:
                return install_app_streamed(passthrough_argv.size(), passthrough_argv.data(),
                                            use_fastdeploy);
            case INSTALL_INCREMENTAL:
                return install_app_incremental(passthrough_argv.size(), passthrough_argv.data(),
                                               incremental_wait, silent);
            case INSTALL_DEFAULT:
            default:
                error_exit("invalid install mode");
        }
    };
	// 如果安装失败,会使用fallback mode再次安装。默认情况下,fallback mode是空(不进行安装)
    auto res = run_install_mode(primary_mode, fallback_mode.has_value());
    if (res && fallback_mode.value_or(primary_mode) != primary_mode) {
        res = run_install_mode(*fallback_mode, false);
    }
    return res;
}

install_app_legacy函数定义在"packages/modules/adb/client/adb_install.cpp"中。函数执行时,会在终端输出"Performing Push Install"提示使用者。函数中判断adb install命令是否以.apk字符结束,并将apk文件push到/data/local/tmp/目录下。然后调用pm_command函数安装/data/local/tmp/目录下的apk文件。最后将/data/local/tmp/下面的apk文件删除。

static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy) {
    printf("Performing Push Install\n");

	// 这里判断 adb install命令是否以 .apk结尾。如果没有就认为没有提供待安装apk
    // Find last APK argument.
    // All other arguments passed through verbatim.
    int last_apk = -1;
    for (int i = argc - 1; i >= 0; i--) {
        if (android::base::EndsWithIgnoreCase(argv[i], ".apex")) {
            error_exit("APEX packages are only compatible with Streamed Install");
        }
        if (android::base::EndsWithIgnoreCase(argv[i], ".apk")) {
            last_apk = i;
            break;
        }
    }
	
	// 如果在adb install的命令最后字符 不是.apk的形式,报错。
    if (last_apk == -1) error_exit("need APK file on command line");

	// 创建临时目录 /data/local/tmp/xxx.apk,存放待安装的apk
    int result = -1;
    std::vector<const char*> apk_file = {argv[last_apk]};
    std::string apk_dest = "/data/local/tmp/" + android::base::Basename(argv[last_apk]);
	// 注意,这里将adb install 中apk文件的路径,换成了 /data/local/tmp/xxx.apk
    argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
	// 如果使用快速部署(默认为false)
    if (use_fastdeploy) {
		// 省略 ...
    }

	// 将Apk push到/data/local/tmp/目录下
    if (do_sync_push(apk_file, apk_dest.c_str(), false, CompressionType::Any, false)) {
		// 执行安装
        result = pm_command(argc, argv);
		// 安装完成后,删除临时目录/data/local/tmp/下的apk文件。
        delete_device_file(apk_dest);
    }

    return result;
}

pm_command函数,顾名思义给执行pm(PackageManager)相关的命令。所以在函数中,为字符串拼接了 "pm"字符(aosp提供的pm命令)

// packages/modules/adb/client/adb_install.cpp
static int pm_command(int argc, const char** argv) {
    std::string cmd = "pm";

    while (argc-- > 0) {
        cmd += " " + escape_arg(*argv++);
    }

    return send_shell_command(cmd);
}

// packages/modules/adb/client/commandline.cpp

int send_shell_command(const std::string& command, bool disable_shell_protocol,
                       StandardStreamsCallbackInterface* callback) {
    unique_fd fd;
    bool use_shell_protocol = false;

    while (true) {
        bool attempt_connection = true;

        // Use shell protocol if it's supported and the caller doesn't explicitly
        // disable it.
        if (!disable_shell_protocol) {
            auto&& features = adb_get_feature_set(nullptr);
            if (features) {
                use_shell_protocol = CanUseFeature(*features, kFeatureShell2);
            } else {
                // Device was unreachable.
                attempt_connection = false;
            }
        }

        if (attempt_connection) {
            std::string error;
            std::string service_string = ShellServiceString(use_shell_protocol, "", command);
			// 将命令发送给adbd,由adbd执(注意adb运行在Host端,比如PC。adbd才是运行在Android系统上的服务)
            fd.reset(adb_connect(service_string, &error));
            if (fd >= 0) {
                break;
            }
        }

        fprintf(stderr, "- waiting for device -\n");
        if (!wait_for_device("wait-for-device")) {
            return 1;
        }
    }

    return read_and_dump(fd.get(), use_shell_protocol, callback);
}

adbd服务端的处理流程

adbd(daemon进程)在收到Client端(adb)请求,并执行相关动作。在函数StartCommandInProcess中,执行客户端发过来的 “shell pm … /data/local/temp/xxx.apk"命令。

int main(int argc, char* const argv[]) {
    signal(SIGPIPE, SIG_IGN);

    int fd = STDIN_FILENO;
    std::string data;
    while (true) {
        std::string error;
		// 读取客户端的请求
        if (!ReadProtocolString(fd, &data, &error)) {
            PLOG(ERROR) << "Failed to read message: " << error;
            break;
        }

        std::string_view name = data;
        auto protocol = SubprocessProtocol::kShell;
        if (android::base::ConsumePrefix(&name, "abb:")) {
            protocol = SubprocessProtocol::kShell;
        } else if (android::base::ConsumePrefix(&name, "abb_exec:")) {
            protocol = SubprocessProtocol::kNone;
        } else {
            LOG(FATAL) << "Unknown command prefix for abb: " << data;
        }
	
		// 处理请求命令,具体实现可以参考源码。
		// 基本上就是创建了一个Process,在Process中执行传过来的命令。
        unique_fd result = StartCommandInProcess(std::string(name), &execCmd, protocol);
        int max_buf = LINUX_MAX_SOCKET_SIZE;
        adb_setsockopt(result, SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
		// 处理结果发送给客户端
        if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) {
            PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
            break;
        }
    }
}

pm命令的处理流程

上述流程中最终执行的是 pm install -r -t -d /data/local/tmp/linduo.apk。Android pm命令实际上是一段bash程序。实现在frameworks/base/cmds/pm/pm中,只是简单的调用了cmd package,并将入参传了进去。

#!/system/bin/sh
cmd package "$@"

cmd是一个bin程序,其实现在frameworks/native/cmds/cmd/中。

# frameworks/native/cmds/cmd/Android.bp
package {
    default_applicable_licenses: ["frameworks_native_cmds_cmd_license"],
}

// Added automatically by a large-scale-change
// See: http://go/android-license-faq
license {
    name: "frameworks_native_cmds_cmd_license",
    visibility: [":__subpackages__"],
    license_kinds: [
        "SPDX-license-identifier-Apache-2.0",
    ],
    license_text: [
        "NOTICE",
    ],
}

cc_library_static {
    name: "libcmd",

    srcs: ["cmd.cpp"],
    export_include_dirs: ["."],

    shared_libs: [
        "libutils",
        "liblog",
        "libselinux",
        "libbinder",
    ],

    cflags: [
        "-Wall",
        "-Werror",
        "-DXP_UNIX",
    ],
}

cc_binary {
    name: "cmd",

    srcs: ["main.cpp"],

    static_libs: [
        "libcmd",
    ],

    shared_libs: [
        "libutils",
        "liblog",
        "libselinux",
        "libbinder",
    ],

    cflags: [
        "-Wall",
        "-Werror",
        "-DXP_UNIX",
    ],
}

如果调用的是cmd package,cmd程序中会通过Android 的ServiceManager连接到PMS服务。然后将命令通过binder发送给PMS服务。

// frameworks/native/cmds/cmd/main.cpp
#include <unistd.h>

#include "cmd.h"

int main(int argc, char* const argv[]) {
    signal(SIGPIPE, SIG_IGN);
	
	// 这里将入参的第一个参数去掉。比如 cmd package xxxxx,去掉后就是 package xxxx
    std::vector<std::string_view> arguments;
    arguments.reserve(argc - 1);
    // 0th argument is a program name, skipping.
    for (int i = 1; i < argc; ++i) {
        arguments.emplace_back(argv[i]);
    }
	// main程序中调用cmdMain
    return cmdMain(arguments, android::aout, android::aerr, STDIN_FILENO, STDOUT_FILENO,
                   STDERR_FILENO, RunMode::kStandalone);
}

// frameworks/native/cmds/cmd/cmd.cpp
int cmdMain(const std::vector<std::string_view>& argv, TextOutput& outputLog, TextOutput& errorLog,
            int in, int out, int err, RunMode runMode) {
    sp<ProcessState> proc = ProcessState::self();
    proc->startThreadPool();

#if DEBUG
    ALOGD("cmd: starting");
#endif
	// 获取ServiceManager对象
    sp<IServiceManager> sm = defaultServiceManager();
   
    // 这里判断一下,cmd命令中想要调用的服务在当前系统中是否存在。不存在则返回
    if ((argc == 1) && (argv[0] == "-l")) {
        Vector<String16> services = sm->listServices();
        services.sort(sort_func);
        outputLog << "Currently running services:" << endl;

        for (size_t i=0; i<services.size(); i++) {
            sp<IBinder> service = sm->checkService(services[i]);
            if (service != nullptr) {
                outputLog << "  " << services[i] << endl;
            }
        }
        return 0;
    }

	// 判断是否输入了-w选项。
    bool waitForService = ((argc > 1) && (argv[0] == "-w"));
    int serviceIdx = (waitForService) ? 1 : 0;
    const auto cmd = argv[serviceIdx];

    Vector<String16> args;
    String16 serviceName = String16(cmd.data(), cmd.size());
    for (int i = serviceIdx + 1; i < argc; i++) {
        args.add(String16(argv[i].data(), argv[i].size()));
    }
    sp<IBinder> service;
    if(waitForService) {
        service = sm->waitForService(serviceName);
    } else {
        service = sm->checkService(serviceName);
    }

    if (service == nullptr) {
        if (runMode == RunMode::kStandalone) {
            ALOGW("Can't find service %.*s", static_cast<int>(cmd.size()), cmd.data());
        }
        errorLog << "cmd: Can't find service: " << cmd << endl;
        return 20;
    }

    sp<MyShellCallback> cb = new MyShellCallback(errorLog);
    sp<MyResultReceiver> result = new MyResultReceiver();

#if DEBUG
    ALOGD("cmd: Invoking %.*s in=%d, out=%d, err=%d",
          static_cast<int>(cmd.size()), cmd.data(), in, out, err);
#endif


	// 这里将命令发送给对应的服务端执行。
    // TODO: block until a result is returned to MyResultReceiver.
    status_t error = IBinder::shellCommand(service, in, out, err, args, cb, result);
	
	// 省略...
}

PackageManagerService接收安装Apk的请求,将Apk安装到Android系统

adb将安装apk的命令发送到adbd(服务端),adbd通过binder调用PMS服务安装APK。PMS接收到shell调用后,会创建PackageManagerShellCommand对象并调用它exec函数的进行处理。

@Override
public void onShellCommand(FileDescriptor in, FileDescriptor out,
		FileDescriptor err, String[] args, ShellCallback callback,
		ResultReceiver resultReceiver) {
	(new PackageManagerShellCommand(this, mContext,mDomainVerificationManager.getShell()))
			.exec(this, in, out, err, args, callback, resultReceiver);
}

PackageManagerShellCommand的exec函数,有其继承的父类定义。该函数中会调用onCommand解析命令并调用不同的处理分支。对于本文中的情况(执行INSTALL_PUSH方式安装)会调用runInstall函数进行处理。runInstall函数中调用makeInstallParams将组织APK安装的参数,然后调用doRunInstall进程安装。

// frameworks/base/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
@Override
public int onCommand(String cmd) {
	if (cmd == null) {
		return handleDefaultCommands(cmd);
	}

	final PrintWriter pw = getOutPrintWriter();
	try {
		switch (cmd) {
			case "path":
				return runPath();
			case "dump":
				return runDump();
			case "list":
				return runList();
			case "resolve-activity":
				return runResolveActivity();
			case "query-activities":
				return runQueryIntentActivities();
			case "query-services":
				return runQueryIntentServices();
			case "query-receivers":
				return runQueryIntentReceivers();
			case "install":
				return runInstall();
			case "install-streaming":
				return runStreamingInstall();
			case "install-incremental":
				return runIncrementalInstall();
			// 省略...
	} catch (RemoteException e) {
		pw.println("Remote exception: " + e);
	}
	return -1;
}

private int runInstall() throws RemoteException {
	return doRunInstall(makeInstallParams());
}

makeInstallParams函数会根据adb install [apk相关参数] 时输入的参数进行组织。其实现如下

// frameworks/base/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
// 例如: adb install -t -r -d linduo.apk(注意安装路径会被替换成  /data/local/temp/linduo.apk )
private InstallParams makeInstallParams() {
	final SessionParams sessionParams = new SessionParams(SessionParams.MODE_FULL_INSTALL);
	final InstallParams params = new InstallParams();

	params.sessionParams = sessionParams;
	// Allowlist all permissions by default
	sessionParams.installFlags |= PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS;

	String opt;
	boolean replaceExisting = true;
	boolean forceNonStaged = false;
	while ((opt = getNextOption()) != null) {
		switch (opt) {
			case "-r": // ignore
				break;
			case "-R":
				replaceExisting = false;
				break;
			case "-i":
				params.installerPackageName = getNextArg();
				if (params.installerPackageName == null) {
					throw new IllegalArgumentException("Missing installer package");
				}
				break;
			case "-t":  
				sessionParams.installFlags |= PackageManager.INSTALL_ALLOW_TEST;
				break;
			case "-f":
				sessionParams.installFlags |= PackageManager.INSTALL_INTERNAL;
				break;
			case "-d":
				sessionParams.installFlags |= PackageManager.INSTALL_REQUEST_DOWNGRADE;
				break;
			case "-g":
				sessionParams.installFlags |= PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS;
				break;
			case "--restrict-permissions":
				sessionParams.installFlags &=
						~PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS;
				break;
			case "--dont-kill":
				sessionParams.installFlags |= PackageManager.INSTALL_DONT_KILL_APP;
				break;
			case "--originating-uri":
				sessionParams.originatingUri = Uri.parse(getNextArg());
				break;
			case "--referrer":
				sessionParams.referrerUri = Uri.parse(getNextArg());
				break;
			case "-p":
				sessionParams.mode = SessionParams.MODE_INHERIT_EXISTING;
				sessionParams.appPackageName = getNextArg();
				if (sessionParams.appPackageName == null) {
					throw new IllegalArgumentException("Missing inherit package name");
				}
				break;
			case "--pkg":
				sessionParams.appPackageName = getNextArg();
				if (sessionParams.appPackageName == null) {
					throw new IllegalArgumentException("Missing package name");
				}
				break;
			case "-S":
				final long sizeBytes = Long.parseLong(getNextArg());
				if (sizeBytes <= 0) {
					throw new IllegalArgumentException("Size must be positive");
				}
				sessionParams.setSize(sizeBytes);
				break;
			case "--abi":
				sessionParams.abiOverride = checkAbiArgument(getNextArg());
				break;
			case "--ephemeral":
			case "--instant":
			case "--instantapp":
				sessionParams.setInstallAsInstantApp(true /*isInstantApp*/);
				break;
			case "--full":
				sessionParams.setInstallAsInstantApp(false /*isInstantApp*/);
				break;
			case "--preload":
				sessionParams.setInstallAsVirtualPreload();
				break;
			case "--user":
				params.userId = UserHandle.parseUserArg(getNextArgRequired());
				break;
			case "--install-location":
				sessionParams.installLocation = Integer.parseInt(getNextArg());
				break;
			case "--install-reason":
				sessionParams.installReason = Integer.parseInt(getNextArg());
				break;
			case "--force-uuid":
				sessionParams.installFlags |= PackageManager.INSTALL_FORCE_VOLUME_UUID;
				sessionParams.volumeUuid = getNextArg();
				if ("internal".equals(sessionParams.volumeUuid)) {
					sessionParams.volumeUuid = null;
				}
				break;
			case "--force-sdk": // ignore
				break;
			case "--apex":
				sessionParams.setInstallAsApex();
				sessionParams.setStaged();
				break;
			case "--force-non-staged":
				forceNonStaged = true;
				break;
			case "--multi-package":
				sessionParams.setMultiPackage();
				break;
			case "--staged":
				sessionParams.setStaged();
				break;
			case "--force-queryable":
				sessionParams.setForceQueryable();
				break;
			case "--enable-rollback":
				if (params.installerPackageName == null) {
					// com.android.shell has the TEST_MANAGE_ROLLBACKS
					// permission needed to enable rollback for non-module
					// packages, which is likely what the user wants when
					// enabling rollback through the shell command. Set
					// the installer to com.android.shell if no installer
					// has been provided so that the user doesn't have to
					// remember to set it themselves.
					params.installerPackageName = "com.android.shell";
				}
				sessionParams.installFlags |= PackageManager.INSTALL_ENABLE_ROLLBACK;
				break;
			case "--staged-ready-timeout":
				params.stagedReadyTimeoutMs = Long.parseLong(getNextArgRequired());
				break;
			case "--skip-verification":
				sessionParams.installFlags |= PackageManager.INSTALL_DISABLE_VERIFICATION;
				break;
			default:
				throw new IllegalArgumentException("Unknown option " + opt);
		}
	}
	if (replaceExisting) {
		sessionParams.installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
	}
	if (forceNonStaged) {
		sessionParams.isStaged = false;
	}
	return params;
}

doRunInstall函数根据安装参数进行APK的安装,该函数主要做了两个事情。一个是通过doCreateSession函数,创建了PackageInstallerSession对象,做了些Apk安装准工作。另一个是通过doCommitSession函数,利用PackageInstaller.Session对象进行apk的安装。

private int doRunInstall(final InstallParams params) throws RemoteException {
	final PrintWriter pw = getOutPrintWriter();
	
	// 判断安装类型
	final boolean isStreaming = params.sessionParams.dataLoaderParams != null;
	final boolean isApex =
			(params.sessionParams.installFlags & PackageManager.INSTALL_APEX) != 0;

	ArrayList<String> args = getRemainingArgs();

	final boolean fromStdIn = args.isEmpty() || STDIN_PATH.equals(args.get(0));
	final boolean hasSplits = args.size() > 1;

	if (fromStdIn && params.sessionParams.sizeBytes == -1) {
		pw.println("Error: must either specify a package size or an APK file");
		return 1;
	}

	if (isApex && hasSplits) {
		pw.println("Error: can't specify SPLIT(s) for APEX");
		return 1;
	}

	if (!isStreaming) {
		if (fromStdIn && hasSplits) {
			pw.println("Error: can't specify SPLIT(s) along with STDIN");
			return 1;
		}

		if (args.isEmpty()) {
			args.add(STDIN_PATH);
		} else {
			setParamsSize(params, args);
		}
	}

	// 创建SessionID(PMS会创建PackageInstallerSession对象),可以理解成创建了安装APK的任务
	// 通过调用PackageInstallerService的createSession实现
	final int sessionId = doCreateSession(params.sessionParams,
			params.installerPackageName, params.userId);
	boolean abandonSession = true;
	try {
		if (isStreaming) {
			if (doAddFiles(sessionId, args, params.sessionParams.sizeBytes, isApex)
					!= PackageInstaller.STATUS_SUCCESS) {
				return 1;
			}
		} else {
			if (doWriteSplits(sessionId, args, params.sessionParams.sizeBytes, isApex)
					!= PackageInstaller.STATUS_SUCCESS) {
				return 1;
			}
		}
		
		// 安装APK(通过提交sessionID),主要关注这个函数。
		if (doCommitSession(sessionId, false /*logSuccess*/)
				!= PackageInstaller.STATUS_SUCCESS) {
			return 1;
		}
		abandonSession = false;

		if (params.sessionParams.isStaged && params.stagedReadyTimeoutMs > 0) {
			return doWaitForStagedSessionReady(sessionId, params.stagedReadyTimeoutMs, pw);
		}

		pw.println("Success");
		return 0;
	} finally {
		if (abandonSession) {
			try {
				doAbandonSession(sessionId, false /*logSuccess*/);
			} catch (Exception ignore) {
			}
		}
	}
}

doCommitSession函数中创建了PackageInstaller.Session对象,通过与上面创建的sessionId进行绑定。然后调用PackageInstaller.Session.commit进行APK安装。

private int doCommitSession(int sessionId, boolean logSuccess)
		throws RemoteException {

	final PrintWriter pw = getOutPrintWriter();
	PackageInstaller.Session session = null;
	try {
		session = new PackageInstaller.Session(
				mInterface.getPackageInstaller().openSession(sessionId));
		if (!session.isMultiPackage() && !session.isStaged()) {
			// Validity check that all .dm files match an apk.
			// (The installer does not support standalone .dm files and will not process them.)
			try {
				DexMetadataHelper.validateDexPaths(session.getNames());
			} catch (IllegalStateException | IOException e) {
				pw.println(
						"Warning [Could not validate the dex paths: " + e.getMessage() + "]");
			}
		}
		final LocalIntentReceiver receiver = new LocalIntentReceiver();
		//调用commit安装APK
		session.commit(receiver.getIntentSender());
		final Intent result = receiver.getResult();
		final int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS,
				PackageInstaller.STATUS_FAILURE);
		// 判断APK安装状态
		if (status == PackageInstaller.STATUS_SUCCESS) {
			if (logSuccess) {
				pw.println("Success");
			}
		} else {
			pw.println("Failure ["
					+ result.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) + "]");
		}
		return status;
	} finally {
		IoUtils.closeQuietly(session);
	}
}

PackageInstaller.Session的实现如下,调用了IPackageInstallerSession(通过mInterface.getPackageInstaller().openSession(sessionId)取得)对象的commit函数。

public static class Session implements Closeable {
	public Session(IPackageInstallerSession session) {
		mSession = session;
	}

	public void commit(@NonNull IntentSender statusReceiver) {
		try {
			mSession.commit(statusReceiver, false);
		} catch (RemoteException e) {
			throw e.rethrowFromSystemServer();
		}
	}
}

PackageInstallerSession的commit函数,实现在"frameworks/base/services/core/java/com/android/server/pm/PackageInstallerSession.java“。commit函数中,会将处理转到自身的Handle中。通过一系列调用commit -> dispatchSessionSealed -> handleSessionSealed -> dispatchStreamValidateAndCommit -> handleStreamValidateAndCommit -> handleInstall -> verify -> verifyNonStaged,在verifyNonStaged函数中,调用PMS的verifyStage验证APK。验证成功后,通过onVerificationComplete回调,调用install函数,安装APK

// frameworks/base/services/core/java/com/android/server/pm/PackageInstallerSession.java
@Override
public void commit(@NonNull IntentSender statusReceiver, boolean forTransfer) {
	// 省略
	dispatchSessionSealed();
}

private void dispatchSessionSealed() {
	// 转到Handle线程处理
	mHandler.obtainMessage(MSG_ON_SESSION_SEALED).sendToTarget();
}

private void handleSessionSealed() {
	assertSealed("dispatchSessionSealed");
	// Persist the fact that we've sealed ourselves to prevent
	// mutations of any hard links we create.
	mCallback.onSessionSealedBlocking(this);
	dispatchStreamValidateAndCommit();
}

private void dispatchStreamValidateAndCommit() {
	mHandler.obtainMessage(MSG_STREAM_VALIDATE_AND_COMMIT).sendToTarget();
}

private void handleStreamValidateAndCommit() {
	// 省略
	mHandler.obtainMessage(MSG_INSTALL).sendToTarget();
}

private void handleInstall() {
	// 检查、验证APK包
	verify();
}

private void verify() {
	try {
		verifyNonStaged();
	} catch (PackageManagerException e) {
		final String completeMsg = ExceptionUtils.getCompleteMessage(e);
		onSessionVerificationFailure(e.error, completeMsg);
	}
}
// 调用PMS的verifyStage,验证APK。验证成功会,调用install方法进行安装。
private void verifyNonStaged()
		throws PackageManagerException {
	final PackageManagerService.VerificationParams verifyingSession =
			prepareForVerification();
	if (verifyingSession == null) {
		return;
	}
	if (isMultiPackage()) {
		// 省略
		mPm.verifyStage(verifyingSession, verifyingChildSessions);
	} else {
		mPm.verifyStage(verifyingSession);
	}
}

// 验证成功,通过回调。调用install开始安装APK
private void onVerificationComplete() {
	// Staged sessions will be installed later during boot
	if (isStaged()) {
		// TODO(b/136257624): Remove this once all verification logic has been transferred out
		//  of StagingManager.
		mStagingManager.notifyPreRebootVerification_Apk_Complete(mStagedSession);
		// TODO(b/136257624): We also need to destroy internals for verified staged session,
		//  otherwise file descriptors are never closed for verified staged session until reboot
		return;
	}

	install();
}

在install函数中,调用了PMS的installStage函数,对APK进行了安装。

// frameworks/base/services/core/java/com/android/server/pm/PackageInstallerSession.java
private void install() {
	try {
		installNonStaged();
	} catch (PackageManagerException e) {
		final String completeMsg = ExceptionUtils.getCompleteMessage(e);
		onSessionInstallationFailure(e.error, completeMsg);
	}
}

调用PMS的installStage函数,安装APK
private void installNonStaged()
		throws PackageManagerException {
	final PackageManagerService.InstallParams installingSession = makeInstallParams();
	if (installingSession == null) {
		throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
				"Session should contain at least one apk session for installation");
	}
	if (isMultiPackage()) {
		// 省略...
		mPm.installStage(installingSession, installingChildSessions);
	} else {
		mPm.installStage(installingSession);
	}
}

PackageManagerService的installStage会对APK进行实际的安装。

// frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
void installStage(InstallParams params) {
	// 首先进行copy
	final Message msg = mHandler.obtainMessage(INIT_COPY);
	params.setTraceMethod("installStage").setTraceCookie(System.identityHashCode(params));
	msg.obj = params;

	Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "installStage",
			System.identityHashCode(msg.obj));
	Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "queueInstall",
			System.identityHashCode(msg.obj));

	mHandler.sendMessage(msg);
}

转Handle处理INIT_COPY,调用startCopy函数,在startCopy函数中调用handleStartCopy和handleReturnCode进行APK的拷贝,从/data/local/tmp/目录把APK文件拷贝拷贝到/data/app/包名/目录。然后调用processInstallRequestsAsync进行安装。

// frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
private abstract class HandlerParams {
        final void startCopy() {
            if (DEBUG_INSTALL) Slog.i(TAG, "startCopy " + mUser + ": " + this);
            handleStartCopy();
            handleReturnCode();
        }
}

class InstallParams extends HandlerParams {
  // 根据安装Flag,进行预处理
  public void handleStartCopy() {
            if ((installFlags & PackageManager.INSTALL_APEX) != 0) {
                mRet = INSTALL_SUCCEEDED;
                return;
            }
            PackageInfoLite pkgLite = PackageManagerServiceUtils.getMinimalPackageInfo(mContext,
                    mPackageLite, origin.resolvedPath, installFlags, packageAbiOverride);

            // For staged session, there is a delay between its verification and install. Device
            // state can change within this delay and hence we need to re-verify certain conditions.
            boolean isStaged = (installFlags & INSTALL_STAGED) != 0;
            if (isStaged) {
                mRet = verifyReplacingVersionCode(
                        pkgLite, requiredInstalledVersionCode, installFlags);
                if (mRet != INSTALL_SUCCEEDED) {
                    return;
                }
            }

            mRet = overrideInstallLocation(pkgLite);
        }

        @Override
        void handleReturnCode() {
            processPendingInstall();
        }

   		private void processPendingInstall() {
            InstallArgs args = createInstallArgs(this);
            if (mRet == PackageManager.INSTALL_SUCCEEDED) {
				// 这里会对apk进行拷贝。拷贝到/data/app/包名/ 目录下
                mRet = args.copyApk();
            }
            if (mRet == PackageManager.INSTALL_SUCCEEDED) {
				// 解压apk相关的一些资源
                F2fsUtils.releaseCompressedBlocks(
                        mContext.getContentResolver(), new File(args.getCodePath()));
            }
            if (mParentInstallParams != null) {
				// 安装多个APK
                mParentInstallParams.tryProcessInstallRequest(args, mRet);
            } else {
				// 安装一个APK
                PackageInstalledInfo res = createPackageInstalledInfo(mRet);
                processInstallRequestsAsync(
                        res.returnCode == PackageManager.INSTALL_SUCCEEDED,
                        Collections.singletonList(new InstallRequest(args, res)));
            }
        }
}

processInstallRequestsAsync 异步安装APK。最终会调用PMS的installPackagesLI函数进行安装。安装流程包括四个流程:Prepare、Scan、Reconcile、Commit。安装后系统关于该APK的信息会更新,启动该APK时会以新的信息进行启动(安装成功状态)。

// frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
// Queue up an async operation since the package installation may take a little while.
private void processInstallRequestsAsync(boolean success,
		List<InstallRequest> installRequests) {
	mHandler.post(() -> {
		// 省略
		for (InstallRequest request : apkInstallRequests) {
			restoreAndPostInstall(request.args.user.getIdentifier(), request.installResult,
					new PostInstallData(request.args, request.installResult, null));
		}
	});
}

// Queue up an async operation since the package installation may take a little while.
private void processInstallRequestsAsync(boolean success,
		List<InstallRequest> installRequests) {
	mHandler.post(() -> {
		// 省略...
		if (success) {
			for (InstallRequest request : apkInstallRequests) {
				request.args.doPreInstall(request.installResult.returnCode);
			}
			synchronized (mInstallLock) {
				// 安装APK
				installPackagesTracedLI(apkInstallRequests);
			}
			for (InstallRequest request : apkInstallRequests) {
				request.args.doPostInstall(
						request.installResult.returnCode, request.installResult.uid);
			}
		}
		for (InstallRequest request : apkInstallRequests) {
			restoreAndPostInstall(request.args.user.getIdentifier(), request.installResult,
					new PostInstallData(request.args, request.installResult, null));
		}
	});
}


@GuardedBy("mInstallLock")
private void installPackagesTracedLI(List<InstallRequest> requests) {
	try {
		Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "installPackages");
		installPackagesLI(requests);
	} finally {
		Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
	}
}


@GuardedBy("mInstallLock")
private void installPackagesLI(List<InstallRequest> requests) {
	// 省略..
	// 安装APK Prepare-> Scan -> Reconcile -> Commit
	// Prepare: Analyzes any current install state, parses the package and does initial
	// Scan:Interrogates the parsed packages given the context collected in prepare
	// Reconcile:Validates scanned packages in the context of each other and the current system 
	// state to ensure that the install will be successful
	// Commit:Commits all scanned packages and updates system state. 
}

总结

通过adb install安装apk,大概流程是 adb(client)端通过socke命令给adbd(service),然后adbd通过ServiceManager以Binder方式将命令发送给PackageManagerService。PackageManagerService解析命令,复制APK及相关资源到指定路径,并更新系统中APK相关配置信息。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/338043.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

KAGGLE · GETTING STARTED CODE COMPETITION 图像风格迁移 示例代码阅读

本博文阅读的代码来自于I’m Something of a Painter Myself | Kaggle倾情推荐&#xff1a; Monet CycleGAN Tutorial | Kaggle 数据集说明 I’m Something of a Painter Myself | Kaggle Files monet_jpg - 300 Monet paintings sized 256x256 in JPEG formatmonet_tfrec -…

go语言(十一)----面向对象继承

一、面向对象继承 写一个父类 package mainimport "fmt"type Human struct {name stringsex string }func (this *Human) Eat() {fmt.Println("Human.Eat()...") }func (this *Human) Walk() {fmt.Println("Human.Walk()...") }func main() {h…

B(l)utter:一款针对Flutter移动端应用程序的逆向工程分析工具

关于B(l)utter B(l)utter是一款针对Flutter移动端应用程序的逆向工程分析工具&#xff0c;当前版本的B(l)utter仅支持Android libapp.so&#xff08;ARM64&#xff09;&#xff0c;可以帮助广大研究人员对基于Flutter开发的移动端应用程序进行逆向工程分析。 环境搭建 该应用…

dpdk网络转发环境的搭建

文章目录 前言ip命令的使用配置dpdk-basicfwd需要的网络结构测试dpdk-basicfwddpdk-basicfwd代码分析附录basicfwd在tcp转发时的失败抓包信息DPDK的相关设置 前言 上手dpdk有两难。其一为环境搭建。被绑定之后的网卡没有IP&#xff0c;我如何给它发送数据呢&#xff1f;当然&a…

全国各省市上市公司数量数据,Shp、excel格式,含上市企业数量、行政区划中心点位经纬度等字段

基本信息. 数据名称: 全国各省市上市公司数量数据 数据格式: Shp、excel 数据时间: 2023年1月 数据几何类型: 面 数据坐标系: WGS84 数据来源&#xff1a;网络公开数据 数据字段&#xff1a; 序号字段名称字段说明1province省份名称2provin_dm省份代码3city城市名…

学习CANopen --- [12] Abort报文

当我们使用SDO进行读写操作时&#xff0c;有时device会返回abort报文&#xff0c;意味着本次SDO读写失败。本文使用例子来讲解Abort报文&#xff0c;以及如何解读失败原因。 一 Device例子 下面是device的python代码&#xff0c;文件名叫device.py&#xff0c;device的CANopen…

02. VBA从入门到精通——基础语法

数据类型 常用数据类型 Integer&#xff1a;整数&#xff0c;-32,768到32,767之间的整数 Long&#xff1a;较长长整数&#xff0c;-2,147,483,648到2,147,483,647之间的整数 Single:浮点数&#xff0c;它可以存储大约&#xff1a;6到7位小数的精度。 Double&#xff1a;较长浮…

免费的WordPress插件大全

在当今数字化的时代&#xff0c;拥有一个强大的在线存在变得至关重要。而对于使用WordPress建站的用户来说&#xff0c;插件是提高网站功能的关键。在这篇文章中&#xff0c;我们将为您推荐三款免费的WordPress插件&#xff0c;它们不仅是147SEO软件中的佼佼者&#xff0c;而且…

【Leetcode】接雨水(双指针、单调栈)

目录 &#x1f4a1;题目描述 &#x1f4a1;双指针解法 &#x1f4a1;单调栈解法 &#x1f4a1;题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 提示&#xff1a; n height.length1 < n…

this.$set的用法

作用&#xff1a; 在data里面绑定的数据具有响应式的效果,也就是我们说的V-Model 数据更新视图,视图也能更新数据&#xff0c;如果不是data里面的数据如何添加响应式呢&#xff1f; this.$Set这个方法能够实现 用法&#xff1a; this.$Set(要添加的对象,要添加的属性’,要添…

为什么C++17要引入std::string_view?

目录 1.引言 2.原理分析 2.1.结构 2.2.构造函数 2.3.成员函数 2.4.std::string_view字面量 3.实例 3.1.std::string_view和std::string的运算符操作 3.2.查找函数使用 3.3.std::string_view和临时字符串 4.总结 1.引言 在C/C日常编程中&#xff0c;我们常进行数据的…

Offer必备算法_双指针_八道力扣OJ题详解(由浅到深)

目录 双指针算法原理 ①力扣283. 移动零 解析代码 ②力扣1089. 复写零 解析代码 ③力扣202. 快乐数 解析代码 ④力扣11. 盛最多水的容器 解析代码 ⑤力扣611. 有效三角形的个数 解析代码 ⑥剑指 Offer 57. 和为s的两个数字 解析代码&#xff1a; ⑦力扣15. 三数之…

最通俗易懂的JVM内存管理与对象创建原理

前言 对于Java程序员来说&#xff0c;在虚拟机自动内存管理机制的帮助下&#xff0c;不再需要像 C/C程序为每一个new操作去写配对 的delete/free代码&#xff0c;不容易出现内存泄漏和内存溢出问题。也正是因为Java程序员把控制内存的权力交给了Java虚拟机&#xff0c;一旦出现…

SpringMVC基础知识学习笔记

Universe Infinity Inc. 目录 一、学习SpringMVC主要是学什么1、SpringMVC的基本原理2、SpringMVC学习串联 二、快速体验SpringMVC的开发1、新建项目&#xff0c;转成web项目2、引入依赖3、编写Spring的配置类4、配置web启动类&#xff0c;替代web.xml5、编写Handler&#xff…

都在卷鸿蒙开发,那就推荐 几个鸿蒙开源项目

如果要问2024年最火的技术是什么,那鸿蒙开发必须占据一些位置,HarmonyOS是华为自主研发的物联网操作系统,自2019年8月正式发布以来便受到了广大开发者的追崇。为了方便大家学习鸿蒙开发,本文分享 12 个开源的鸿蒙实战项目,希望能从这些项目中获得启发和实用经验。 小狐浏…

IDEA的database使用

一、数据据库 在使用database之前&#xff0c;首先你的电脑要安装好了数据库并且启动。 MySQL卸载手册 链接&#xff1a;https://pan.baidu.com/doc/share/AVXW5SG6T76puBOWnPegmw-602323264797863 提取码&#xff1a;hlgf MySQL安装图解 链接&#xff1a;https://pan.baidu.…

Win10/11中VMware Workstation设置网络桥接模式

文章目录 一、添加VMware Bridge Protocol服务二、配置桥接参数1.启用系统Device Install Service服务2.配置VMware 需要确认物理网卡是否有添加VMware Bridge Protocol服务 添加VMware Bridge Protocol服务 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参…

艾比森的“增长炼金术”:从四力驱动到三层面增长

在技术驱动的LED显示行业&#xff0c;产品迭代快、周期波动大&#xff0c;企业经营时时承压。 但是&#xff0c;市场上总不乏打破“传统”的个例。根据2023年业绩预告&#xff0c;艾比森预计2023年实现归母净利润3.10亿-3.50亿元&#xff0c;同比增长52.70%-72.40%&#xff1b…

HttpServletRequest getServerPort()、getLocalPort() 、getRemotePort() 区别

getRemotePort() 、getServerPort()、getLocalPort() request.getServerPort()、request.getLocalPort() 和 request.getRemotePort() 这三个方法都是获取与HTTP请求相关的端口信息的 客户端(如浏览器)通过某个随机分配的网络连接端口(7070) 向服务器发送HTTP请求( http://exam…

【Docker】未来已来 | Docker技术在云计算、边缘计算领域的应用前景

欢迎来到英杰社区&#xff1a; https://bbs.csdn.net/topics/617804998 欢迎来到阿Q社区&#xff1a; https://bbs.csdn.net/topics/617897397 &#x1f4d5;作者简介&#xff1a;热爱跑步的恒川&#xff0c;致力于C/C、Java、Python等多编程语言&#xff0c;热爱跑步&#xff…