Skip to content

Commit

Permalink
FastDeploy refactor: 2+GB APK support, optimizations, tests.
Browse files Browse the repository at this point in the history
- removed 2GB apk size cap,
- removed zip archive parsing on device (1.1M->236K agent size reduction),
- optimized matching entries search,
- added more robust matching entries search based on hash of CDr entry,
- reduced patch size by reusing Local File Header of matched entries,
- removed extra manifest parsing and extra agent calls,
- added device-side tests for agent,
- fix for Windows patch creation.

Test: atest adb_test fastdeploy_test FastDeployTests

Total time for 0-size patch reduction for 1.7G apk: 1m1.778s->0m36.234s.

Change-Id: I66d2cef1adf5b2be3325e355a7e72e9c99992369
  • Loading branch information
alexbuy committed Oct 2, 2019
1 parent 1aab898 commit 1af550e
Show file tree
Hide file tree
Showing 34 changed files with 1,847 additions and 729 deletions.
6 changes: 5 additions & 1 deletion Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ cc_defaults {
"-DADB_HOST=1", // overridden by adbd_defaults
"-DALLOW_ADBD_ROOT=0", // overridden by adbd_defaults
"-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION=1",
"-DENABLE_FASTDEPLOY=1", // enable fast deploy
],
cpp_std: "experimental",

Expand Down Expand Up @@ -691,6 +690,7 @@ cc_library_host_static {
name: "libfastdeploy_host",
defaults: ["adb_defaults"],
srcs: [
"fastdeploy/deploypatchgenerator/apk_archive.cpp",
"fastdeploy/deploypatchgenerator/deploy_patch_generator.cpp",
"fastdeploy/deploypatchgenerator/patch_utils.cpp",
"fastdeploy/proto/ApkEntry.proto",
Expand Down Expand Up @@ -727,6 +727,7 @@ cc_test_host {
name: "fastdeploy_test",
defaults: ["adb_defaults"],
srcs: [
"fastdeploy/deploypatchgenerator/apk_archive_test.cpp",
"fastdeploy/deploypatchgenerator/deploy_patch_generator_test.cpp",
"fastdeploy/deploypatchgenerator/patch_utils_test.cpp",
],
Expand Down Expand Up @@ -754,6 +755,9 @@ cc_test_host {
},
},
data: [
"fastdeploy/testdata/rotating_cube-metadata-release.data",
"fastdeploy/testdata/rotating_cube-release.apk",
"fastdeploy/testdata/sample.apk",
"fastdeploy/testdata/sample.cd",
],
}
112 changes: 28 additions & 84 deletions client/adb_install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
#include "commandline.h"
#include "fastdeploy.h"

#if defined(ENABLE_FASTDEPLOY)
static constexpr int kFastDeployMinApi = 24;
#endif

namespace {

Expand Down Expand Up @@ -146,15 +144,7 @@ static void read_status_line(int fd, char* buf, size_t count) {
*buf = '\0';
}

#if defined(ENABLE_FASTDEPLOY)
static int delete_device_patch_file(const char* apkPath) {
std::string patchDevicePath = get_patch_path(apkPath);
return delete_device_file(patchDevicePath);
}
#endif

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

// The last argument must be the APK file
Expand All @@ -176,31 +166,15 @@ static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy
error_exit("--fastdeploy doesn't support .apex files");
}

if (use_fastdeploy == true) {
#if defined(ENABLE_FASTDEPLOY)
TemporaryFile metadataTmpFile;
std::string patchTmpFilePath;
{
TemporaryFile patchTmpFile;
patchTmpFile.DoNotRemove();
patchTmpFilePath = patchTmpFile.path;
if (use_fastdeploy) {
auto metadata = extract_metadata(file);
if (metadata.has_value()) {
// pass all but 1st (command) and last (apk path) parameters through to pm for
// session creation
std::vector<const char*> pm_args{argv + 1, argv + argc - 1};
auto patchFd = install_patch(pm_args.size(), pm_args.data());
return stream_patch(file, std::move(metadata.value()), std::move(patchFd));
}

FILE* metadataFile = fopen(metadataTmpFile.path, "wb");
extract_metadata(file, metadataFile);
fclose(metadataFile);

create_patch(file, metadataTmpFile.path, patchTmpFilePath.c_str());
// pass all but 1st (command) and last (apk path) parameters through to pm for
// session creation
std::vector<const char*> pm_args{argv + 1, argv + argc - 1};
install_patch(file, patchTmpFilePath.c_str(), pm_args.size(), pm_args.data());
adb_unlink(patchTmpFilePath.c_str());
delete_device_patch_file(file);
return 0;
#else
error_exit("fastdeploy is disabled");
#endif
}

struct stat sb;
Expand Down Expand Up @@ -265,8 +239,7 @@ static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy
return 1;
}

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

// Find last APK argument.
Expand All @@ -287,35 +260,26 @@ static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy,
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]);
argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */

if (use_fastdeploy == true) {
#if defined(ENABLE_FASTDEPLOY)
TemporaryFile metadataTmpFile;
TemporaryFile patchTmpFile;
if (use_fastdeploy) {
auto metadata = extract_metadata(apk_file[0]);
if (metadata.has_value()) {
auto patchFd = apply_patch_on_device(apk_dest.c_str());
int status = stream_patch(apk_file[0], std::move(metadata.value()), std::move(patchFd));

FILE* metadataFile = fopen(metadataTmpFile.path, "wb");
extract_metadata(apk_file[0], metadataFile);
fclose(metadataFile);
result = pm_command(argc, argv);
delete_device_file(apk_dest);

create_patch(apk_file[0], metadataTmpFile.path, patchTmpFile.path);
apply_patch_on_device(apk_file[0], patchTmpFile.path, apk_dest.c_str());
#else
error_exit("fastdeploy is disabled");
#endif
} else {
if (!do_sync_push(apk_file, apk_dest.c_str(), false)) goto cleanup_apk;
return status;
}
}

argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
result = pm_command(argc, argv);

cleanup_apk:
if (use_fastdeploy == true) {
#if defined(ENABLE_FASTDEPLOY)
delete_device_patch_file(apk_file[0]);
#endif
if (do_sync_push(apk_file, apk_dest.c_str(), false)) {
result = pm_command(argc, argv);
delete_device_file(apk_dest);
}
delete_device_file(apk_dest);

return result;
}

Expand All @@ -324,7 +288,6 @@ int install_app(int argc, const char** argv) {
InstallMode installMode = INSTALL_DEFAULT;
bool use_fastdeploy = false;
bool is_reinstall = false;
bool use_localagent = false;
FastDeploy_AgentUpdateStrategy agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion;

for (int i = 1; i < argc; i++) {
Expand Down Expand Up @@ -353,11 +316,6 @@ int install_app(int argc, const char** argv) {
} else if (!strcmp(argv[i], "--version-check-agent")) {
processedArgIndicies.push_back(i);
agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion;
#ifndef _WIN32
} else if (!strcmp(argv[i], "--local-agent")) {
processedArgIndicies.push_back(i);
use_localagent = true;
#endif
}
}

Expand All @@ -369,14 +327,13 @@ int install_app(int argc, const char** argv) {
error_exit("Attempting to use streaming install on unsupported device");
}

#if defined(ENABLE_FASTDEPLOY)
if (use_fastdeploy == true && get_device_api_level() < kFastDeployMinApi) {
if (use_fastdeploy && get_device_api_level() < kFastDeployMinApi) {
printf("Fast Deploy is only compatible with devices of API version %d or higher, "
"ignoring.\n",
kFastDeployMinApi);
use_fastdeploy = false;
}
#endif
fastdeploy_set_agent_update_strategy(agent_update_strategy);

std::vector<const char*> passthrough_argv;
for (int i = 0; i < argc; i++) {
Expand All @@ -389,26 +346,13 @@ int install_app(int argc, const char** argv) {
error_exit("install requires an apk argument");
}

if (use_fastdeploy == true) {
#if defined(ENABLE_FASTDEPLOY)
fastdeploy_set_local_agent(use_localagent);
update_agent(agent_update_strategy);

// The last argument must be the APK file
const char* file = passthrough_argv.back();
use_fastdeploy = find_package(file);
#else
error_exit("fastdeploy is disabled");
#endif
}

switch (installMode) {
case INSTALL_PUSH:
return install_app_legacy(passthrough_argv.size(), passthrough_argv.data(),
use_fastdeploy, use_localagent);
use_fastdeploy);
case INSTALL_STREAM:
return install_app_streamed(passthrough_argv.size(), passthrough_argv.data(),
use_fastdeploy, use_localagent);
use_fastdeploy);
case INSTALL_DEFAULT:
default:
return 1;
Expand Down
9 changes: 2 additions & 7 deletions client/commandline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,8 @@ static void stdin_raw_restore() {
}
#endif

// Reads from |fd| and prints received data. If |use_shell_protocol| is true
// this expects that incoming data will use the shell protocol, in which case
// stdout/stderr are routed independently and the remote exit code will be
// returned.
// if |callback| is non-null, stdout/stderr output will be handled by it.
int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false,
StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK) {
int read_and_dump(borrowed_fd fd, bool use_shell_protocol,
StandardStreamsCallbackInterface* callback) {
int exit_code = 0;
if (fd < 0) return exit_code;

Expand Down
8 changes: 8 additions & 0 deletions client/commandline.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ int send_shell_command(
const std::string& command, bool disable_shell_protocol = false,
StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);

// Reads from |fd| and prints received data. If |use_shell_protocol| is true
// this expects that incoming data will use the shell protocol, in which case
// stdout/stderr are routed independently and the remote exit code will be
// returned.
// if |callback| is non-null, stdout/stderr output will be handled by it.
int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false,
StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);

// Connects to the device "abb" service with |command| and returns the fd.
template <typename ContainerT>
unique_fd send_abb_exec_command(const ContainerT& command_args, std::string* error) {
Expand Down
Loading

0 comments on commit 1af550e

Please sign in to comment.