Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/android-native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions python-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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` (`<dir>_*` 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 `<dir>_*`, 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 `<dir>_*`, 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)",
)
}
}