SmartAppUpdates

Introduction: Android App 增量升级,包含前后端方案、Demo、以及 so 库,可用于商店或大体积 App 差分升级
More: Author   ReportBugs   
Tags:

APK delta generation and reconstruction for Android.

English | 简体中文

SmartAppUpdates generates a BSDIFF40 patch from two APKs and reconstructs the new APK from the exact old APK and that patch. It provides an Android library, a host command-line tool, and an offline sample covering reconstruction, integrity checks, and Android's installation flow.

Patch size depends on the differences between the APKs; a smaller download is not guaranteed.

Components

Component Purpose Documentation
ApkPatchLibrary Android AAR exposing the Java/JNI patch API Library
ApkPatchLibraryServer Java/JNI CLI for generating and applying patches on macOS and Linux CLI
ApkPatchLibrarySample Offline Android sample using a source-built test app Sample

The CLI and Android library share the same patch parser and bzip2 sources. The parser validates patch structure and bounds and reports native failures through error codes. The CLI isolates native operations in worker JVMs, with timeouts and temporary-output cleanup.

Build requirements

Run commands from the repository root on macOS or Linux. Native Windows builds are not currently supported.

Dependency Version / requirement
JDK 17 or 21
Gradle 8.11.1, supplied by the Wrapper
Android Gradle Plugin 8.10.1
Android SDK Platform 36
Android NDK 29.0.14206865
CMake 3.31.6
Host tools Python 3 and a C compiler

Configure the Android SDK through ANDROID_HOME or sdk.dir in a local, Git-ignored local.properties file. The first build requires access to dependency repositories.

The Android library supports API 21+ and builds armeabi-v7a, arm64-v8a, and x86_64 native libraries with 16 KB page alignment. The sample targets API 36.

Quick start

For prebuilt artifacts, see GitHub Releases and the AAR integration guide.

Build the library and CLI

These commands do not require the sample APKs:

./gradlew :apkPatchLibrary:check :apkPatchLibrary:assembleRelease :server:build

If you only need the host CLI, build just that module from the repository root:

./gradlew :server:build

Build outputs:

  • Android AAR: ApkPatchLibrary/build/outputs/aar/
  • Executable CLI JAR: ApkPatchLibraryServer/build/libs/ApkPatchLibraryServer-2.0-all.jar
  • CLI distributions: ApkPatchLibraryServer/build/distributions/

The CLI artifacts include a native library for the build host's OS and JVM architecture. Build separately for each target platform.

Generate and apply a patch

Use absolute paths for inputs and outputs. The output parent directory must exist, and the output file must not already exist.

java -jar ApkPatchLibraryServer/build/libs/ApkPatchLibraryServer-2.0-all.jar \
  diff /path/to/old.apk /path/to/new.apk /path/to/update.patch

java -jar ApkPatchLibraryServer/build/libs/ApkPatchLibraryServer-2.0-all.jar \
  patch /path/to/old.apk /path/to/rebuilt.apk /path/to/update.patch

cmp /path/to/new.apk /path/to/rebuilt.apk

The argument order is diff <old> <new> <patch> and patch <old> <new> <patch>. The CLI accepts any suitable input pair; it is not tied to the sample APKs. ApkPatchLibraryServer is a local command-line tool, not an HTTP update service.

Native operations default to a 900-second timeout. Add --timeout-seconds 1200 before diff or patch to override it. A timeout exits with code 124 after terminating the worker. See the CLI reference for other exit codes and output handling.

Use the Android library

The sample consumes the library directly from this checkout:

dependencies {
    implementation project(':apkPatchLibrary')
}

For another project, build the release AAR and add it as a local dependency. The patch API is:

import com.cundong.utils.PatchUtils;

// Run on a background thread. Paths must refer to app-accessible files.
int result = PatchUtils.patch(oldApkPath, outputApkPath, patchPath);
if (result == PatchUtils.SUCCESS) {
    // Verify the output against trusted update metadata before installation.
} else {
    // Handle the corresponding PatchUtils.ERR_* code.
}

The old APK must match the version used to generate the patch byte for byte. A successful return indicates reconstruction completed; the caller is responsible for validating the resulting APK and initiating installation. See the sample implementation for the complete flow.

Run the offline sample

A fresh checkout builds two revisions of a small project-owned Android app from the fixture source. Both use the local Android debug signing key. The build generates the patch and checks that applying it reproduces the target APK byte for byte. No APK download or preinstalled target app is required.

./gradlew :app:assembleDebug
./gradlew :app:installDebug

Open ApkPatchSample to see the old and new versions. Tap 合成并安装 to rebuild rebuilt.apk from the bundled old APK and patch. After checking the SHA-256, package name and version, the demo opens the system installer for confirmation. Back in the demo, the result shows elapsed time and APK size. Tap 校验详情 for the SHA-256 and output path.

For the Taobao 10.59.20 → 10.59.40 demo, place taobao-10.59.20.apk, update.patch and taobao-10.59.40.apk in Apks/taobao-10.59/ and run ./gradlew :app:installDebug. Git ignores these three local files. The build packages the old APK as assets/old.apk and the patch as assets/update.patch. It uses the full 10.59.40 APK to verify that the reconstructed file matches byte for byte. See local fixtures for case selection.

Android sample showing Taobao versions 10.59.20 and 10.59.40 and the rebuild and install button

Testing

Public-source checks, without the local APK pair:

./gradlew fixtureScriptTest :apkPatchLibrary:check :server:check
python3 scripts/check_repository.py
git diff --check

Full checks and builds, including the public sample:

./gradlew check build
./gradlew :app:assembleRelease :app:lintRelease

The CI workflow builds and lints the public Sample, runs an emulator upgrade test, and checks native peak RSS while reconstructing a 128 MiB test file. To repeat the device test after assembling Debug with -PsampleFixture=public, select a test emulator with ANDROID_SERIAL, put adb on PATH, and run python3 scripts/smoke_sample.py. For Taobao 10.59 on a phone, follow the reconstruction and installer confirmation check.

Scope and limitations

  • Supports reconstruction of a single APK. Split APK installation and Android App Bundle delivery are outside the current scope.
  • Does not provide update hosting, download orchestration, signed update metadata, or rollback policy. Production integrations must define their own trust and delivery model; fixed sample hashes are not a metadata authentication service.
  • Reconstruction and installation are separate steps. Android can reject an otherwise correctly reconstructed APK because of device compatibility, signing, or version constraints.
  • Keep files below 2 GB when targeting 32-bit ABIs. Patch application reads and writes in 64 KiB chunks using 128 KiB of work buffers plus bzip2 state. Diff generation can use substantially more memory than the input size.

Contributing

See CONTRIBUTING.md for the contribution workflow and AGENTS.md for repository conventions used by coding agents. Include reproduction steps and relevant build or test results when reporting bugs or proposing changes.

License

Project code is licensed under Apache License 2.0. Vendored bzip2 and bsdiff-derived code retain their respective license notices; see THIRD_PARTY_NOTICES.md and NOTICE.

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools
AI Daily Digest