Modern Apps git

← Modern-Apps.git

appstore: install the F-Droid APK built for the device ABI (#599)

Commit
a4d93c454bd4e22ddea5ad652cded7688a1f9ac3
Author
vayun-mathur <[email protected]>
When
2026-08-24 02:10 UTC (3 months ago)
Parents
52f8c4d

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.ktindex f13c45158a..fcf420539c 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt@@ -1,6 +1,7 @@ package com.vayunmathur.appstore.data  import android.content.Context+import android.os.Build import android.util.JsonReader import android.util.JsonToken import com.vayunmathur.appstore.data.security.ApkCertificates@@ -178,15 +179,7 @@ object FDroidRepository {         var featureGraphic: String? = null         var screenshots: List<String> = emptyList()         var antiFeatures: List<String> = emptyList()-        var latestAdded: Long = -1L-        var latestFileName: String? = null-        var latestSize: Long = 0L-        var latestSha256: String? = null-        var latestSigners: List<String> = emptyList()-        var latestVersionName: String? = null-        var latestVersionCode: Long = 0L-        var latestTargetSdk: Int? = null-        var latestWhatsNew: String? = null+        val versions = mutableListOf<VersionCandidate>()          reader.beginObject()         while (reader.hasNext()) {@@ -238,6 +231,7 @@ object FDroidRepository {                             var vVersionName: String? = null                             var vVersionCode: Long = 0L                             var vTargetSdk: Int? = null+                            var vNativeCode: List<String> = emptyList()                             var vWhatsNew: String? = null                             while (reader.hasNext()) {                                 when (reader.nextName()) {@@ -270,6 +264,9 @@ object FDroidRepository {                                                     }                                                     reader.endObject()                                                 }+                                                // The ABIs this APK carries native libraries for.+                                                // Absent means it has none and runs anywhere.+                                                "nativecode" -> vNativeCode = readStringArray(reader)                                                 // signer.sha256 is the list of signing-certificate                                                 // fingerprints this APK is expected to carry.                                                 "signer" -> {@@ -292,18 +289,20 @@ object FDroidRepository {                                 }                             }                             reader.endObject()-                            // Newest version always wins; reproducibility is recorded as a-                            // badge below, not used to drop older-but-reproduced builds.-                            if (vAdded >= latestAdded) {-                                latestAdded = vAdded-                                latestFileName = vFileName-                                latestSize = vFileSize-                                latestSha256 = vFileSha256-                                latestSigners = vSigners-                                latestVersionName = vVersionName-                                latestVersionCode = vVersionCode-                                latestTargetSdk = vTargetSdk-                                latestWhatsNew = vWhatsNew+                            val fileName = vFileName+                            if (fileName != null) {+                                versions += VersionCandidate(+                                    added = vAdded,+                                    fileName = fileName,+                                    size = vFileSize,+                                    sha256 = vFileSha256,+                                    signers = vSigners,+                                    versionName = vVersionName,+                                    versionCode = vVersionCode,+                                    targetSdk = vTargetSdk,+                                    nativeCode = vNativeCode,+                                    whatsNew = vWhatsNew,+                                )                             }                         } catch (_: Exception) {                             try { reader.endObject() } catch (_: Exception) {}@@ -316,18 +315,18 @@ object FDroidRepository {         }         reader.endObject() -        // No usable version at all — drop the package rather than advertising an entry+        // Nothing this device can run — drop the package rather than advertising an entry         // that has no installable APK behind it.-        if (latestAdded < 0L || latestFileName == null) return null+        val latest = selectVersion(versions, deviceAbis) ?: return null          // index-v2 file names are repo-absolute ("/com.example_12.apk").-        val apkUrl = latestFileName.let { repoBase + "/" + it.trimStart('/') }+        val apkUrl = repoBase + "/" + latest.fileName.trimStart('/')         return UnifiedApp(             packageName = packageName,             source = source,-            expectedSigners = latestSigners,-            apkSha256 = latestSha256,-            reproducible = isReproducible(packageName, latestVersionCode),+            expectedSigners = latest.signers,+            apkSha256 = latest.sha256,+            reproducible = isReproducible(packageName, latest.versionCode),             name = metaName ?: packageName.substringAfterLast('.'),             summary = metaSummary ?: "",             description = metaDesc ?: "",@@ -337,21 +336,83 @@ object FDroidRepository {             antiFeatures = antiFeatures,             author = author,             categories = categories,-            versionName = latestVersionName,-            versionCode = latestVersionCode,-            sizeBytes = latestSize,+            versionName = latest.versionName,+            versionCode = latest.versionCode,+            sizeBytes = latest.size,             apkUrl = apkUrl,-            targetSdk = latestTargetSdk,+            targetSdk = latest.targetSdk,             license = license,             website = website,             sourceCode = sourceCode,-            whatsNew = latestWhatsNew,+            whatsNew = latest.whatsNew,             addedTimestamp = added,             lastUpdated = if (lastUpdated != 0L) lastUpdated else added,             repoUrl = repoBase         )     } +    // ---- Version selection ----++    /**+     * One entry of a package's `versions` map.+     *+     * Held as a list rather than folded into a running maximum as it is read, because+     * F-Droid publishes some apps as one APK per architecture under a single release, and+     * those all share the same [added] timestamp: which of them is installable here can+     * only be decided once every variant has been seen.+     */+    internal data class VersionCandidate(+        val added: Long,+        val fileName: String,+        val size: Long = 0L,+        val sha256: String? = null,+        val signers: List<String> = emptyList(),+        val versionName: String? = null,+        val versionCode: Long = 0L,+        val targetSdk: Int? = null,+        val nativeCode: List<String> = emptyList(),+        val whatsNew: String? = null,+    )++    private val deviceAbis: List<String> by lazy { Build.SUPPORTED_ABIS?.toList().orEmpty() }++    /**+     * Pick the one APK to advertise for a package, newest release first.+     *+     * A release split per architecture used to resolve to whichever variant happened to be+     * enumerated last, so an arm64 phone could be handed the x86_64 build. Everything before+     * the install then passed — the hash and signer come from the same chosen entry — and+     * Android rejected it only at the very end, after the download and the system prompt,+     * which read as the install doing nothing at all.+     *+     * Variants this device cannot run are dropped outright, so a package whose newest release+     * has no build for this architecture falls back to an older one that does rather than+     * disappearing.+     */+    internal fun selectVersion(+        candidates: List<VersionCandidate>,+        deviceAbis: List<String>,+    ): VersionCandidate? =+        candidates+            .mapNotNull { candidate -> abiRank(candidate.nativeCode, deviceAbis)?.let { candidate to it } }+            .minWithOrNull(+                compareByDescending<Pair<VersionCandidate, Int>> { it.first.added }+                    .thenBy { it.second }+                    .thenByDescending { it.first.versionCode }+            )+            ?.first++    /**+     * How well an APK's `nativecode` fits this device — lower is better, null means it cannot+     * run here at all. An APK with no native code is architecture-independent so it always+     * fits, but it ranks behind any variant built for one of the device's own ABIs, matching+     * how Android itself prefers the most specific available.+     */+    private fun abiRank(nativeCode: List<String>, deviceAbis: List<String>): Int? {+        if (nativeCode.isEmpty()) return deviceAbis.size+        return nativeCode.mapNotNull { abi -> deviceAbis.indexOf(abi).takeIf { it >= 0 } }.minOrNull()+    }+     // ---- Helpers ----      private fun readLocalizedString(reader: JsonReader): String? {
appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.ktindex 1535adb3e0..a950640482 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt@@ -5,10 +5,17 @@ import android.content.Context import android.content.Intent import android.content.pm.PackageInstaller import android.util.Log+import com.vayunmathur.appstore.R+import com.vayunmathur.library.util.AppMessages  /**  * Receives PackageInstaller commit callbacks.  * Action: com.vayunmathur.appstore.INSTALL_STATUS+ *+ * This is the only place the real outcome of an install arrives. `commit()` returning+ * successfully only means the session was handed over, so a failure here used to reach+ * logcat and nowhere else, leaving the user with the "Installing X" message the ViewModel+ * had already shown and no indication anything had gone wrong.  */ class InstallStatusReceiver : BroadcastReceiver() { @@ -43,8 +50,26 @@ class InstallStatusReceiver : BroadcastReceiver() {             PackageInstaller.STATUS_SUCCESS -> {                 Log.i(TAG, "Install success for $pkg")             }+            // The user backing out of the system prompt is not a failure worth reporting back.+            PackageInstaller.STATUS_FAILURE_ABORTED -> {+                Log.i(TAG, "Install aborted for $pkg")+            }             else -> {                 Log.w(TAG, "Install failed for $pkg status=$status message=$message")+                val reason = when (status) {+                    PackageInstaller.STATUS_FAILURE_INCOMPATIBLE ->+                        context.getString(R.string.install_failure_incompatible)+                    PackageInstaller.STATUS_FAILURE_STORAGE ->+                        context.getString(R.string.install_failure_storage)+                    PackageInstaller.STATUS_FAILURE_CONFLICT ->+                        context.getString(R.string.install_failure_conflict)+                    else -> message?.takeIf { it.isNotBlank() }+                        ?: context.getString(R.string.install_failure_unknown)+                }+                AppMessages.show(+                    context.getString(R.string.install_failed_reason, pkg.orEmpty(), reason),+                    duration = AppMessages.Duration.Long,+                )             }         }     }
appstore/src/main/res/values/strings.xml
diff --git a/appstore/src/main/res/values/strings.xml b/appstore/src/main/res/values/strings.xmlindex 54221e273c..72d3b78f1f 100644--- a/appstore/src/main/res/values/strings.xml+++ b/appstore/src/main/res/values/strings.xml@@ -108,6 +108,11 @@     <string name="install_started_unverified">Installing %1$s, unverified</string>     <string name="install_blocked">%1$s was blocked: %2$s</string>     <string name="install_failed">Couldn\'t install %1$s</string>+    <string name="install_failed_reason">Couldn\'t install %1$s: %2$s</string>+    <string name="install_failure_incompatible">it isn\'t built for this device</string>+    <string name="install_failure_storage">not enough storage</string>+    <string name="install_failure_conflict">it conflicts with a version already installed</string>+    <string name="install_failure_unknown">the system rejected it</string>     <string name="uninstaller_unavailable">Couldn\'t open the uninstaller</string>     <string name="share_app_text">%1$s — %2$s</string> 
appstore/src/test/java/com/vayunmathur/appstore/data/FDroidVersionSelectionTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/FDroidVersionSelectionTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/FDroidVersionSelectionTest.ktnew file mode 100644index 0000000000..a54c835830--- /dev/null+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/FDroidVersionSelectionTest.kt@@ -0,0 +1,114 @@+package com.vayunmathur.appstore.data++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * F-Droid publishes some apps as one APK per architecture within a single release, all+ * sharing the same `added` timestamp. Handing the wrong one to the installer produces a+ * failure only Android can detect, at the very end of the install, so this selection is+ * the only place the mistake can be caught.+ */+class FDroidVersionSelectionTest {++    private val arm64Device = listOf("arm64-v8a", "armeabi-v7a", "armeabi")++    private fun version(+        added: Long,+        fileName: String,+        versionCode: Long = 1L,+        nativeCode: List<String> = emptyList(),+    ) = FDroidRepository.VersionCandidate(+        added = added,+        fileName = fileName,+        versionCode = versionCode,+        nativeCode = nativeCode,+    )++    @Test+    fun `picks the variant matching the device architecture`() {+        val release = listOf(+            version(200, "/app_x86_64.apk", versionCode = 14, nativeCode = listOf("x86_64")),+            version(200, "/app_arm64.apk", versionCode = 12, nativeCode = listOf("arm64-v8a")),+            version(200, "/app_armv7.apk", versionCode = 13, nativeCode = listOf("armeabi-v7a")),+        )++        assertEquals("/app_arm64.apk", FDroidRepository.selectVersion(release, arm64Device)?.fileName)+    }++    @Test+    fun `prefers the primary ABI over a secondary one the device also supports`() {+        val release = listOf(+            version(200, "/app_armv7.apk", nativeCode = listOf("armeabi-v7a")),+            version(200, "/app_arm64.apk", nativeCode = listOf("arm64-v8a")),+        )++        assertEquals("/app_arm64.apk", FDroidRepository.selectVersion(release, arm64Device)?.fileName)+    }++    @Test+    fun `prefers an architecture-specific build over a universal one`() {+        val release = listOf(+            version(200, "/app_universal.apk"),+            version(200, "/app_arm64.apk", nativeCode = listOf("arm64-v8a")),+        )++        assertEquals("/app_arm64.apk", FDroidRepository.selectVersion(release, arm64Device)?.fileName)+    }++    @Test+    fun `falls back to the universal build when no variant matches`() {+        val release = listOf(+            version(200, "/app_x86_64.apk", nativeCode = listOf("x86_64")),+            version(200, "/app_universal.apk"),+        )++        assertEquals("/app_universal.apk", FDroidRepository.selectVersion(release, arm64Device)?.fileName)+    }++    @Test+    fun `newest release still wins over an older one`() {+        val versions = listOf(+            version(100, "/app_old_arm64.apk", nativeCode = listOf("arm64-v8a")),+            version(200, "/app_new_arm64.apk", nativeCode = listOf("arm64-v8a")),+        )++        assertEquals("/app_new_arm64.apk", FDroidRepository.selectVersion(versions, arm64Device)?.fileName)+    }++    @Test+    fun `falls back to an older release when the newest has no build for this device`() {+        val versions = listOf(+            version(100, "/app_old_arm64.apk", nativeCode = listOf("arm64-v8a")),+            version(200, "/app_new_x86.apk", nativeCode = listOf("x86_64")),+        )++        assertEquals("/app_old_arm64.apk", FDroidRepository.selectVersion(versions, arm64Device)?.fileName)+    }++    @Test+    fun `drops a package with no runnable build at all`() {+        val versions = listOf(+            version(200, "/app_x86_64.apk", nativeCode = listOf("x86_64")),+            version(100, "/app_x86.apk", nativeCode = listOf("x86")),+        )++        assertNull(FDroidRepository.selectVersion(versions, arm64Device))+    }++    @Test+    fun `drops a package with no versions`() {+        assertNull(FDroidRepository.selectVersion(emptyList(), arm64Device))+    }++    @Test+    fun `treats a multi-ABI build as matching on its best ABI`() {+        val release = listOf(+            version(200, "/app_armv7_only.apk", nativeCode = listOf("armeabi-v7a")),+            version(200, "/app_arm.apk", nativeCode = listOf("armeabi-v7a", "arm64-v8a")),+        )++        assertEquals("/app_arm.apk", FDroidRepository.selectVersion(release, arm64Device)?.fileName)+    }+}