Modern Apps git

← Modern-Apps.git

networklocation: fix build issues

Commit
854a2b6030342f2fd14a732f6ce1e6f6a83c61eb
Author
Vayun Mathur <[email protected]>
When
2026-08-10 04:24 UTC (3 months ago)
Parents
7eb6dcc

Diff

networklocation/build.gradle.kts
diff --git a/networklocation/build.gradle.kts b/networklocation/build.gradle.ktsindex 7b9d18618f..368d20bc18 100644--- a/networklocation/build.gradle.kts+++ b/networklocation/build.gradle.kts@@ -44,20 +44,24 @@ 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") {+    // Capture plain String/File locals so the task actions below don't hold references to the+    // build script itself — Gradle's configuration cache cannot serialize script object refs.+    val url = geocoderDbUrl+    val outFile = geocoderDbFile     description = "Downloads geocoder.geodb into src/main/assets if it is missing."-    outputs.file(geocoderDbFile)-    outputs.upToDateWhen { geocoderDbFile.exists() }+    outputs.file(outFile)+    outputs.upToDateWhen { outFile.exists() }     doLast {-        if (geocoderDbFile.exists()) {-            logger.lifecycle("geocoder.geodb already present (${geocoderDbFile.length()} bytes); skipping download.")+        if (outFile.exists()) {+            logger.lifecycle("geocoder.geodb already present (${outFile.length()} bytes); skipping download.")             return@doLast         }-        geocoderDbFile.parentFile.mkdirs()-        val tmp = File(geocoderDbFile.parentFile, "geocoder.geodb.part")+        outFile.parentFile.mkdirs()+        val tmp = File(outFile.parentFile, "geocoder.geodb.part")         tmp.delete()-        logger.lifecycle("Fetching geocoder.geodb from $geocoderDbUrl (this is large; first build only)…")+        logger.lifecycle("Fetching geocoder.geodb from $url (this is large; first build only)…")         try {-            val conn = (URI(geocoderDbUrl).toURL().openConnection() as HttpURLConnection).apply {+            val conn = (URI(url).toURL().openConnection() as HttpURLConnection).apply {                 connectTimeout = 30_000                 readTimeout = 60_000                 instanceFollowRedirects = true@@ -69,14 +73,14 @@ val fetchGeocoderDb = tasks.register("fetchGeocoderDb") {             }             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).")+            tmp.renameTo(outFile)+            logger.lifecycle("Fetched geocoder.geodb (${outFile.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 (scripts/geocoder_gen.sh) or check {} and rebuild.",-                e.message, geocoderDbUrl,+                e.message, url,             )         }     }
networklocation/src/main/rust/Cargo.toml
diff --git a/networklocation/src/main/rust/Cargo.toml b/networklocation/src/main/rust/Cargo.tomlindex 9429e7d932..1b38239c9e 100644--- a/networklocation/src/main/rust/Cargo.toml+++ b/networklocation/src/main/rust/Cargo.toml@@ -6,7 +6,10 @@ description = "Robust device-position estimation (RANSAC + Expectation-Maximizat license = "MIT"  [lib]-name = "network_location_position_estimation_rust"+# Must match `rustNativeLib("networklocation")` in build.gradle.kts and+# `System.loadLibrary("networklocation")` in the Kotlin loaders: cargo emits `lib<name>.so`,+# which RustNative.kt copies into jniLibs. The descriptive package name above stays as-is.+name = "networklocation" crate-type = ["cdylib", "rlib"]  [dependencies]
networklocation/src/main/rust/src/geocoder.rs
diff --git a/networklocation/src/main/rust/src/geocoder.rs b/networklocation/src/main/rust/src/geocoder.rsindex 1846e3cb4a..a48d3422bf 100644--- a/networklocation/src/main/rust/src/geocoder.rs+++ b/networklocation/src/main/rust/src/geocoder.rs@@ -571,21 +571,23 @@ mod tests {     const F_COUNTRY: usize = 6;      /// Locate a real DB: `$GEOCODER_DB`, else the bundled asset next to the crate.-    /// Returns `None` (test self-skips) when no DB is present so CI without the ~1.4 GB-    /// asset stays green; a DB that exists but fails to parse is a hard failure.+    fn db_path() -> Option<String> {+        if let Ok(p) = std::env::var("GEOCODER_DB") {+            return Some(p);+        }+        let bundled = concat!(env!("CARGO_MANIFEST_DIR"), "/../assets/geocoder.geodb");+        if std::path::Path::new(bundled).exists() {+            Some(bundled.to_string())+        } else {+            eprintln!("skip: no geocoder DB (set GEOCODER_DB or add the bundled asset)");+            None+        }+    }++    /// Open the real DB. Returns `None` (test self-skips) when no DB is present so CI without+    /// the ~1.4 GB asset stays green; a DB that exists but fails to parse is a hard failure.     fn open_db() -> Option<Reader> {-        let path = match std::env::var("GEOCODER_DB") {-            Ok(p) => p,-            Err(_) => {-                let bundled = concat!(env!("CARGO_MANIFEST_DIR"), "/../assets/geocoder.geodb");-                if std::path::Path::new(bundled).exists() {-                    bundled.to_string()-                } else {-                    eprintln!("skip: no geocoder DB (set GEOCODER_DB or add the bundled asset)");-                    return None;-                }-            }-        };+        let path = db_path()?;         match Reader::open_path(&path) {             Some(r) => Some(r),             None => panic!("geocoder DB at {path} exists but failed magic/version/parse"),@@ -707,4 +709,93 @@ mod tests {         );         assert!(recs.is_empty(), "unknown query should yield no records");     }++    /// Micro-benchmark (opt-in). Run with:+    ///   GEOCODER_DB=/path/geocoder.geodb cargo test -p network_location_position_estimation_rust \+    ///     geocoder::tests::bench_ops -- --ignored --nocapture+    #[test]+    #[ignore]+    fn bench_ops() {+        use std::time::Instant;+        let path = match db_path() {+            Some(p) => p,+            None => return,+        };++        let t = Instant::now();+        let mut r = Reader::open_path(&path).expect("open");+        eprintln!("open (inflate dicts+grid): {:.1} ms", t.elapsed().as_secs_f64() * 1e3);++        // City centers across continents (lat, lon).+        let cities = [+            (40.7128, -74.0060),+            (51.5074, -0.1278),+            (35.6762, 139.6503),+            (48.8566, 2.3522),+            (34.0522, -118.2437),+            (41.9028, 12.4964),+            (52.5200, 13.4050),+            (55.7558, 37.6173),+            (-33.8688, 151.2093),+            (19.0760, 72.8777),+            (-23.5505, -46.6333),+            (1.3521, 103.8198),+            (25.2048, 55.2708),+            (37.7749, -122.4194),+            (43.6532, -79.3832),+        ];+        let reps = 20;++        for &(la, lo) in &cities {+            let _ = r.reverse(la, lo); // warm the OS page cache+        }++        let mut rev = Vec::new();+        for _ in 0..reps {+            for &(la, lo) in &cities {+                let t = Instant::now();+                let rec = r.reverse(la, lo);+                let us = t.elapsed().as_secs_f64() * 1e6;+                if rec.is_some() {+                    rev.push(us);+                }+            }+        }++        // Forward keys taken from a real reverse hit.+        let seed = r.reverse(40.7128, -74.0060).expect("seed");+        let a = r.resolve(seed);+        let (co, st, ci, sr) = (a[6].clone(), a[5].clone(), a[4].clone(), a[3].clone());+        let mut fwd = Vec::new();+        for _ in 0..(reps * cities.len()) {+            let t = Instant::now();+            let _ = r.forward(&co, &st, &ci, &sr, 10);+            fwd.push(t.elapsed().as_secs_f64() * 1e6);+        }++        let mut res = Vec::new();+        for _ in 0..2000 {+            let t = Instant::now();+            let _ = r.resolve(seed);+            res.push(t.elapsed().as_secs_f64() * 1e6);+        }++        fn stats(label: &str, mut v: Vec<f64>) {+            v.sort_by(|a, b| a.partial_cmp(b).unwrap());+            let n = v.len();+            let sum: f64 = v.iter().sum();+            let q = |x: f64| v[(((n - 1) as f64) * x) as usize];+            eprintln!(+                "{label:8} n={n:4}  min={:.1}  p50={:.1}  avg={:.1}  p95={:.1}  max={:.1}  (µs)",+                v[0],+                q(0.5),+                sum / n as f64,+                q(0.95),+                v[n - 1]+            );+        }+        stats("reverse", rev);+        stats("forward", fwd);+        stats("resolve", res);+    } }