diff --git a/.github/workflows/android-native.yml b/.github/workflows/android-native.yml
index 88a4205c6..86de0ab26 100644
--- a/.github/workflows/android-native.yml
+++ b/.github/workflows/android-native.yml
@@ -67,7 +67,8 @@ jobs:
sudo udevadm trigger --name-match=kvm
- name: Build the test APK before the emulator starts
- run: ./gradlew :python-multiplatform:assembleDebugAndroidTest --console=plain
+ # verifyAarStdlib: every staged stdlib file must reach the release AAR (#546).
+ run: ./gradlew :python-multiplatform:assembleDebugAndroidTest :python-multiplatform:verifyAarStdlib --console=plain
- name: Instrumented tests on the emulator
uses: reactivecircus/android-emulator-runner@v2
diff --git a/python-multiplatform/build.gradle.kts b/python-multiplatform/build.gradle.kts
index 0e4b71ac6..108972367 100644
--- a/python-multiplatform/build.gradle.kts
+++ b/python-multiplatform/build.gradle.kts
@@ -16,6 +16,7 @@ import java.lang.reflect.InvocationTargetException
import java.io.FileOutputStream
import java.security.MessageDigest
import java.util.Properties
+import java.util.zip.ZipFile
import java.io.File
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
@@ -2811,10 +2812,44 @@ fun resolveChromiumFamilyBrowser(): String? {
return candidates.firstOrNull { File(it).canExecute() }
}
+// Issue #546: every file `copyAndroidPythonAssets` stages must reach the AAR. AGP's default
+// `ignoreAssetsPattern` (`
_*` in particular) silently dropped every stdlib directory whose name
+// starts with `_` (`zipfile/_path`, `_pyrepl`, `compression/_common`, ...), and `importlib.metadata`
+// then failed on the device with "No module named 'zipfile._path'" (torchnative, 2026-11-27).
+// This compares the staged tree with the AAR's entries, so a filter that drops anything fails here.
+val verifyAarStdlib by tasks.registering {
+ group = "verification"
+ description = "Fails when the release AAR lacks a file staged under build/android/assets (#546)."
+ dependsOn("bundleReleaseAar")
+ val stagedRoot = file("$androidBuildDir/assets")
+ val aar = layout.buildDirectory.file("outputs/aar/${project.name}-release.aar")
+ doLast {
+ val staged = stagedRoot.walkTopDown().filter { it.isFile }
+ .map { "assets/" + it.relativeTo(stagedRoot).invariantSeparatorsPath }.toSet()
+ check(staged.isNotEmpty()) { "nothing is staged under $stagedRoot" }
+ val entries = ZipFile(aar.get().asFile).use { zip ->
+ zip.entries().asSequence().map { it.name }.toSet()
+ }
+ val missing = (staged - entries).sorted()
+ check(missing.isEmpty()) {
+ "the AAR lacks ${missing.size} of ${staged.size} staged asset files (#546), for example:\n" +
+ missing.take(20).joinToString("\n")
+ }
+ logger.lifecycle("verifyAarStdlib: all ${staged.size} staged asset files are in the AAR")
+ }
+}
+
android {
namespace = "python.multiplatform"
compileSdk = libs.versions.android.compileSdk.get().toInt()
+ // AGP's default ignoreAssetsPattern minus `_*`, which dropped every stdlib directory whose
+ // name starts with `_` (`zipfile/_path`, `_pyrepl`, ...) from the AAR (#546; checked by
+ // verifyAarStdlib). An app merging this AAR applies its own pattern too (toolchain #166).
+ androidResources {
+ ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
+ }
+
sourceSets["main"].assets.srcDirs("src/androidMain/assets", "$androidBuildDir/assets")
// KMP androidMain resources do not reach the AAR's classes.jar; AGP's main resources do.
sourceSets["main"].resources.srcDir(generateAndroidPythonProperties)
diff --git a/python-multiplatform/src/androidInstrumentedTest/kotlin/python/multiplatform/AndroidStdlibAssetsTest.kt b/python-multiplatform/src/androidInstrumentedTest/kotlin/python/multiplatform/AndroidStdlibAssetsTest.kt
new file mode 100644
index 000000000..6a91fe979
--- /dev/null
+++ b/python-multiplatform/src/androidInstrumentedTest/kotlin/python/multiplatform/AndroidStdlibAssetsTest.kt
@@ -0,0 +1,39 @@
+package python.multiplatform
+
+import python.multiplatform.ffi.PythonTestFixture
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+/**
+ * Issue #546: the AAR carries every stdlib directory, including the ones whose name starts with `_`.
+ *
+ * AGP's default `ignoreAssetsPattern` contains `_*`, which dropped `zipfile/_path`, `_pyrepl`,
+ * `__phello__`, `compression/_common` and every other `_` directory from the AAR's assets, although
+ * `copyAndroidPythonAssets` staged them. `importlib.metadata` imports `zipfile._path`, so it failed on
+ * the device with "No module named 'zipfile._path'" (torchnative, Flip4, 2026-11-27).
+ *
+ * Red before the fix: each lookup returns `False`, because the directory is not in the APK's assets
+ * and so is not in the unpacked stdlib.
+ */
+class AndroidStdlibAssetsTest {
+
+ private fun hasModule(name: String): String = PythonTestFixture.eval(
+ "__import__('importlib.util').util.find_spec('$name') is not None",
+ ).toString()
+
+ @Test
+ fun underscoreStdlibPackagesAreUnpacked() = PythonTestFixture.withInterpreter {
+ for (name in listOf("zipfile._path", "_pyrepl", "compression._common", "__phello__")) {
+ assertEquals("True", hasModule(name), "the stdlib package $name is missing from the AAR's assets (#546)")
+ }
+ }
+
+ @Test
+ fun importlibMetadataImports() = PythonTestFixture.withInterpreter {
+ assertEquals(
+ "True",
+ PythonTestFixture.eval("__import__('importlib.metadata').metadata is not None").toString(),
+ "importlib.metadata needs zipfile._path (#546)",
+ )
+ }
+}