Keeping a web app and an Android app identical without a framework
androidwebviewsyncsha256content-addresseddeployno-gradle
For a content site that also ships as an Android app, the app does not need its own build of the
content. Treat the web deploy as the single source: publish a manifest of every file and its
hash, and have a thin native app mirror those exact bytes at those exact URLs. New content then
reaches the app with no APK release, and a deploy script makes drift detectable and blockable.
The encyclopedia itself works this way (web at wiki.jhampton.io, app
io.jhampton.encyclopedia).
The manifest
- The build writes
sync.json:{format, content_version, built, app{version_code, apk}, files{path: sha256}}. - Derive
content_versionfrom the sortedpath hashlist only, never from a timestamp, so rebuilding the same content gives the same version and causes no needless downloads. - Keep anything that changes on every build (build time, app version) OUT of the content files, or every page's hash changes each build. Pages can read it from the manifest with JS.
- Include a
formatinteger. An app that sees a higher format than it knows should keep its copy and ask for an update rather than misread the manifest.
The app side
- A WebView loads the real web URL;
WebViewClient.shouldInterceptRequestanswers every request under that prefix from local storage (returnnullfor anything not in the manifest to fall through to the network). Links,fetch()and relative paths behave exactly as on the web; other origins open in the browser viashouldOverrideUrlLoading. - Bundle a full snapshot in
assets/so a fresh install works offline. Store synced files content-addressed (objects/<sha256>), and look up the bundle by hash as well, so a file is downloaded only if neither place has that hash. - Sync: fetch the manifest; if the version differs, download missing hashes, verify each
sha256, write the manifest to a temp file and
rename()it into place, then reload. A crash or a hash mismatch mid-deploy leaves the old version fully intact; already downloaded objects are reused next time. Garbage-collect objects the new manifest does not reference. - On startup, if the APK's bundled manifest is newer (
built) than the stored one, the app was updated: use the bundle and drop the stored copy. - A tiny
addJavascriptInterfacebridge (info(),sync()) lets the page footer show the content and app version and offer "Check for updates" and "App update available".
The deploy side
- Deploy from the committed tree (
git archive HEAD), not the working tree. - Upload order matters: new files, then the manifest, then delete stale files. The manifest must never point at files that are not live yet.
- Publish a new APK only when the app version is bumped, and before the web, because the web manifest advertises it. Store the published versionCode plus a hash of the app sources on the server, and refuse to deploy if the sources changed without a bump.
- Finish with a verifier that downloads every listed file and checks its hash, and checks
the live APK's versionCode (
aapt2 dump badging) and signer (apksigner verify --print-certs). - A versioned
post-commithook (git config core.hooksPath .githooks) that runs the deploy detached makes "commit" mean "ship". Have the deploy re-assertcore.hooksPath, since.git/configis not versioned.
Building the APK without Gradle
aapt2 compile + aapt2 link (manifest, resources, -A assets, --version-code,
--min/target-sdk-version, --java gen for R.java) → javac → d8 → zip classes.dex into
the APK → zipalign -p 4 → apksigner sign. About a second of tool time for a small app.
Gotchas
javac -bootclasspath android.jarfails on lambdas ("Unable to find method metafactory"): android.jar has noLambdaMetafactory. Compile with--release 11 -classpath android.jarinstead; d8 desugars the lambdas.- targetSdk 35+ forces edge-to-edge: pad the root view from
WindowInsets.Type.systemBars() | ime() | displayCutout()or the page hides under the bars. - targetSdk 36 enables predictive back, and
onBackPressed()is no longer called. Register anOnBackInvokedCallbackonly whilewebView.canGoBack(), so the system back still leaves the app. - If the server sends no
Cache-Control, browsers cache HTML heuristically; serve the site (and especially the manifest) withno-cache.
See also
- In-app updates for sideloaded Android apps
- A self-hosted F-Droid repository and APK catalog
- Android app signing keys and why losing one is permanent
- Android window insets, the soft keyboard, and edge-to-edge
- Deploy scripts with rsync (patterns and traps)