In-app updates for sideloaded Android apps
androidupdaterapkpackageinstallersideloadversion-jsondeploy
Apps that are not on Google Play (Message, Keyboard, Spectre, Guardian, Tokay) update themselves from Justin's server: the app fetches a small version file, downloads the newer APK, and hands it to Android's installer. The pattern has been ported between apps several times and is now settled.
Server side
- A public APK at a stable URL (for example
https://tldrcom.com/message.apk,https://jhampton.io/keyboard.apk) plus a version file:{"version":"X.Y.Z","sha256":"<apk hash>"}(some apps also carryversionCode,url, changelog notes). - Upload the APK first and the version file last, so nobody is told about an APK that is not there yet.
- Always include the sha256. One updater silently skipped integrity verification whenever the field was missing.
- The deploy script should refuse: a version already live (unless forced), a versionCode not above the live one, a missing changelog entry, a dirty tree, and an APK whose signing certificate differs from the live APK's.
- Verify after deploy: served sha256 and size match the build, the version file parses, a
Rangerequest returns 206 (needed for resume). - Keeping versioned releases behind a symlink allows server rollback (
deploy.sh rollback 2.31.0) so no one else auto-updates to a bad build. It cannot help phones already updated: Android refuses a versionCode downgrade. The fix for a bad release is always a newer release. See Release-directory deploys with an atomic symlink swap.
App side (zero dependencies)
- Check on app or settings open (the request is tiny; no throttle needed) and from a manual "Check for updates" button that bypasses dismissal. Compare versions numerically, not as strings.
- Show a dismissible banner; store the dismissal per version.
- Download to
cacheDir/<name>.apk.partwithRange+If-Rangeso it can resume; on HTTP 416 restart; auto-resume a stalled connection a few times. - Verify: sha256 when provided, the archive parses (
getPackageArchiveInfo), same package name, higher versionCode. - Install through a framework
PackageInstallersession with a non-exported result receiver, or open the APK through a FileProvider withACTION_VIEW. RequiresREQUEST_INSTALL_PACKAGES. - Do not install mid-task without asking (Spectre asks first while a drive is recording).
- Show download progress, and ask the server for
Accept-Encoding: identity. With a compressed transfer there is no usable content length, so a percentage cannot be computed. A button that fills left to right with "Downloading 1.2… 40%", then "Installing…", or "Download failed · tap to retry" tells the user everything in one place. - In a game, the natural homes for the update line are the start and game-over screens: a quiet "version · Check for updates" line that becomes a filled "Version X is available · INSTALL" pill.
Previewing a build on the user's phone
A release-signed build with the same versionCode as the published one installs over it as an update (same key, versionCode not lower). That lets the user try a preview build (shared as a plain APK link, with a versionName like 0.3.1-about-preview) before the real release; the real release then bumps the versionCode and replaces it normally. Used for Tokay's About screen (post #1784).
Telling the user what changed
Phone 0.1.2 (2026-10-28) added the pattern worth reusing in every app:
- One changelog file in the app's assets (assets/changelog.md, newest first, sections ## <versionName> · <date> with - notes), parsed by a small unit-tested parser. A unit test also parses the shipped file, so a malformed entry fails the build.
- Settings > About > What's new shows the whole file and marks the installed version.
- An "App updated to X" banner after an update, with a link to What's new and a dismiss button. It shows while a stored "seen" versionCode is below the running one. A fresh install (firstInstallTime == lastUpdateTime) records the current version as seen at first launch, so new users do not get the banner.
- The update banner can show the notes too. When the update feed is the F-Droid index, fdroidserver publishes per-version notes as the app's localized.en-US.whatsNew (see A self-hosted F-Droid repository and APK catalog), and the updater shows a few lines of it under "X is available". Only builds that contain this code can show it, so the first visible case is one release later.
- The deploy script refuses to ship unless the changelog's top section matches the versionName and has notes, and it writes the same notes into the catalog's metadata.
What the user sees
- The first time, Android asks to "allow from this source", then continues to the update prompt by itself. Pressing Back there cancels the install.
- After a successful install, Android leaves the user on the home screen; the app does not reopen.
- A build with no updater needs one manual install of the first build that has one.
Gotchas
- A rate-throttled local test server stalled after about 100 KB per connection through the emulator network, which looked like a resume bug. Test with an unthrottled server.
- A debug build cannot update a release install (signature mismatch); switching requires uninstalling, which wipes local-only data. See Android app signing keys and why losing one is permanent.
- Debug-only test hooks (an override base URL, cleartext HTTP to
10.0.2.2) must be ignored in release builds, natively as well as in Dart/Kotlin.
See also
- Android app signing keys and why losing one is permanent
- A self-hosted F-Droid repository and APK catalog
Linked from
- A keyless maps app on Android (MapLibre + OpenStreetMap services)
- A self-hosted F-Droid repository and APK catalog
- An Android file manager, photo bursts, and cropping
- Android app signing keys and why losing one is permanent
- Android TV / Google TV apps (Compose for TV, Media3, sideloading)
- Building an Android browser on GeckoView (Tokay)
- Flutter Android release builds, app size, and store rollout
- Keeping a web app and an Android app identical without a framework
- Keyboard, Justin's Android input method (overview and canon)
- Locking down an Android phone with a device-owner app
- Message, Justin's Android SMS app (architecture and decisions)
- Sharing a generated image from an Android WebView app (FileProvider, ClipData)
- Writing a replacement phone (dialer) app for Android