How sunilnull/python-for-android works
Project Type and Purpose
• This is a CLI-based packaging tool (“python-for-android”) that builds and bundles Python applications into Android APKs.
• It orchestrates a recipe-based build system, cross‐compiles Python modules and native libraries against the Android NDK, and assembles them with chosen bootstraps (SDL2, WebView, Service, etc.) into a final APK.
Key Top‐Level Components (from file tree)
• CLI Entrypoint: p4a script via entrypoints.py and main modules (build.py, distribution.py, toolchain.py).
• Core Library: pythonforandroid/ contains sub‐packages:
• recipe.py, graph.py, pythonpackage.py, patching.py, archs.py, util.py, logger.py
• recipes/: directory of individual build “recipes” (one per dependency).
• bootstraps/: templates & code for different Android app backends.
• tools/: helper utilities for binary linking (biglink, liblink.sh).
• Configuration & CI:
• Dockerfile, Makefile, .travis.yml, GitHub Actions workflow, .deepsource.toml.
• Documentation: doc/ (Sphinx sources and custom theme).
• Tests: tests/ and testapps/.
Relationships and Data Flow
• User invokes p4a CLI → parses arguments (e.g., requirements, bootstrap choice) → calls into distribution.py / build.py.
• The graph engine (graph.py) builds a dependency DAG of requested recipes.
• For each recipe:
• recipe.py defines metadata (source, configure/build steps).
• toolchain.py orchestrates cross‐compilation using the Android NDK, C/C++ toolchain, JNI helpers.
• Patches / source manipulations via patching.py.
• Intermediate artifacts (compiled .so, Python stdlib, site-packages) → packaged by pythonpackage.py.
• Bootstrap templates from bootstraps/ are populated with assets and launchers (Java/Gradle, AndroidManifest templates) → assembled alongside compiled libraries.
• Final packaging step invokes the Android SDK/Gradle (or Ant) to produce a signed APK.
Important Architectural Patterns & Design Principles
• Modular “recipe” pattern: each dependency is encapsulated in its own recipe directory, enabling parallel builds and easy extension.
• Graph-based dependency resolution: recipe graph ensures correct build order.
• Separation of concerns:
• CLI & argument parsing
• Dependency graph resolution
• Recipe build logic
• Toolchain invocation (compiler, linker)
• Packaging & distribution
• Pluggable bootstraps: allows multiple app backends with shared build machinery.