Modern Apps git

← Modern-Apps.git

networklocation: auto-fetch geocoder.geodb into assets at build time (fail-soft, preBuild)

Commit
a192979dedf7d5799440288a0d6cc2b194151674
Author
Vayun Mathur <[email protected]>
When
2026-08-09 22:04 UTC (3 months ago)
Parents
bb7e2d4

Diff

networklocation/build.gradle.kts
diff --git a/networklocation/build.gradle.kts b/networklocation/build.gradle.ktsindex 60f7920075..7e585683fd 100644--- a/networklocation/build.gradle.kts+++ b/networklocation/build.gradle.kts@@ -29,6 +29,57 @@ androidComponents { // squares over the cached gs-loc beacon fixes). See networklocation/src/main/rust/. rustNativeLib("networklocation") +// The packed geocoder DB (~1-1.5 GB) is not committed (see src/main/assets/.gitignore).+// Fetch it into assets at build time so the assembled APK — which MAOS also consumes —+// ships the offline geocoder. Only downloads when the asset is absent; fails soft (warns,+// leaves no partial file) so a network hiccup or an intentionally DB-less dev build still+// compiles.+val geocoderDbUrl = "https://data.vayunmathur.com/geocoder/geocoder.geodb"+val geocoderDbFile = layout.projectDirectory.file("src/main/assets/geocoder.geodb").asFile++val fetchGeocoderDb = tasks.register("fetchGeocoderDb") {+    description = "Downloads geocoder.geodb into src/main/assets if it is missing."+    outputs.file(geocoderDbFile)+    outputs.upToDateWhen { geocoderDbFile.exists() }+    doLast {+        if (geocoderDbFile.exists()) {+            logger.lifecycle("geocoder.geodb already present (${geocoderDbFile.length()} bytes); skipping download.")+            return@doLast+        }+        geocoderDbFile.parentFile.mkdirs()+        val tmp = java.io.File(geocoderDbFile.parentFile, "geocoder.geodb.part")+        tmp.delete()+        logger.lifecycle("Fetching geocoder.geodb from $geocoderDbUrl (this is large; first build only)…")+        try {+            val conn = (java.net.URI(geocoderDbUrl).toURL().openConnection() as java.net.HttpURLConnection).apply {+                connectTimeout = 30_000+                readTimeout = 60_000+                instanceFollowRedirects = true+            }+            val code = conn.responseCode+            if (code != java.net.HttpURLConnection.HTTP_OK) {+                conn.disconnect()+                error("server returned HTTP $code")+            }+            conn.inputStream.use { input -> tmp.outputStream().use { output -> input.copyTo(output, 1 shl 20) } }+            conn.disconnect()+            tmp.renameTo(geocoderDbFile)+            logger.lifecycle("Fetched geocoder.geodb (${geocoderDbFile.length()} bytes).")+        } catch (e: Exception) {+            tmp.delete()+            logger.warn(+                "WARNING: could not fetch geocoder.geodb ({}). The APK will build WITHOUT the offline " ++                    "geocoder DB. Generate it locally (tools/) or check {} and rebuild.",+                e.message, geocoderDbUrl,+            )+        }+    }+}++tasks.matching { it.name == "preBuild" }.configureEach {+    dependsOn(fetchGeocoderDb)+}+ // The geocoder generator runs on the JVM test classpath (GeoDb* are pure-Kotlin). The planet // build needs a large heap; override with GEOCODER_HEAP (e.g. 100g). Normal unit tests are // unaffected — they won't allocate near this ceiling.