$ show kivy/python-for-android
python-for-android (p4a) is a build toolchain that packages Python applications into Android APK, AAB, and AAR binaries. It cross-compiles Python interpreters, native libraries, and Python packages for Android architectures (arm64-v8a, armeabi-v7a, x86_64, x86) using the Android NDK and SDK. The system is driven by a recipe system where each Python package or native library defines its download, patching, and build steps. A bootstrap system (SDL2, SDL3, webview, Qt, service-only) generates the Android project structure and Gradle build files. The tool is primarily used via CLI (`p4a` or `python-for-android`) or integrated with Buildozer. It resolves dependencies via a graph algorithm that handles conflicts, alternatives, and optional dependencies. The codebase is large and mature, with hundreds of recipes and extensive CI testing, but carries technical debt from version-specific patches and workarounds for Android's evolving ecosystem.
The LLM identified 6 files worth highlighting for reuse:
Implements a dependency graph for recipes with support for alternative dependencies (tuples), optional dependencies, blacklists, and conflict detection. The `get_recipe_order_and_bootstrap` function computes a valid build order and selects the appropriate bootstrap. This module is self-contained and could be extracted for any build system requiring complex dependency resolution with constraints.
Provides per-architecture environment builders that compose CC, CXX, CFLAGS, LDFLAGS, AR, STRIP, and other toolchain variables from NDK paths and arch-specific flags. Supports arm64-v8a, armeabi-v7a, x86_64, and x86. This module is reusable for any project that needs to set up a cross-compilation environment for Android NDK.
Contains the `shprint` function that wraps `sh` library commands with live streaming output, color control, error filtering, and tailing of last lines on failure. It centralizes subprocess execution and error formatting. This pattern is reusable in any Python project that runs shell commands and needs robust logging.
Extracts package name, version, and dependencies from pip packages without installing them, using `pip download --no-deps` and reading metadata files. It serves as a workaround for pip's limited introspection API. This module is self-contained and reusable for any tool that needs to inspect Python package metadata offline.
Provides functions to apply patches conditionally based on context (e.g., `will_build`, `is_arch`, `is_linux`). Patches are defined declaratively in recipes with a list of patch files and optional conditions. This pattern is reusable for any build system that needs to apply platform-specific patches.
A Makefile that downloads and installs Android SDK command-line tools, NDK, and sets up the build environment. It pins specific versions (NDK 28c, SDK tools 6514223, API level 36) and handles platform detection. This is reusable as a CI setup script for any Android cross-compilation project.