Android Packaging

This document outlines the process of packaging Flet applications for Android, generating both APK and Android App Bundle (AAB) formats.

Prerequisites

  • **Java Development Kit (JDK):** Required for the build process. If missing or incompatible, Flet CLI will automatically install it in $HOME/java/{version}.
  • **Android SDK:** Required for the build process. If Android Studio is installed, Flet CLI uses its bundled SDK. Otherwise, it installs a standalone SDK in $HOME/Android/sdk.
  • **Android Wheels for Binary Python Packages:** Ensure all non-pure Python packages (e.g., numpy, cryptography) have pre-built wheels available for Android.
  • Build Commands

  • **flet build apk**
  • * Builds a **release** Android APK. Release builds do not support debugging and are intended for app store deployment. * **Split APK per ABI:** To reduce APK size, you can split the fat APK into smaller APKs for specific architectures (e.g., arm64-v8a, armeabi-v7a). * Configuration: `[tool.flet.android] split_per_abi = true or flet build apk --split-per-abi`. * Note: Distribute the correct APK for the user's device architecture. * **Installing APK to a device:** Use the Android Debug Bridge (adb). * Command: adb install . * Specify device: adb -s install (use adb devices to list devices).

  • **flet build aab**
  • * Builds a **release** Android App Bundle (AAB) file. Recommended for publishing to the Google Play Store due to optimized app size.

    Signing an Android Bundle

    Android requires all APKs to be digitally signed. For AABs, you sign with an upload key, and Google Play handles app signing. For APKs distributed elsewhere, you must manually sign.

  • **Upload Key:** Used to sign the bundle/APK uploaded to the Play Store.
  • **App Signing Key:** Used by Google Play to sign the final APK delivered to users.
  • **Create an Upload Keystore:** If you don't have one, create it using keytool.
  • * macOS/Linux: keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload * Windows (PowerShell): keytool -genkey -v -keystore $env:USERPROFILE\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload * Remember the keystore password and alias. Keep the keystore file private. * Note: keytool is part of the JDK. If not in PATH, use the full path (e.g., from flutter doctor -v). Use -storetype JKS for Java 9+.

  • **Configuration (Signing):**
  • * **Key Alias:** flet build aab --android-signing-key-alias or pyproject.toml: `[tool.flet.android.signing] key_alias = "value" or Environment Variable: FLET_ANDROID_SIGNING_KEY_ALIAS="value"`. * **Key Store:** flet build aab --android-signing-key-store or pyproject.toml: `[tool.flet.android.signing] key_store = "path/to/store.jks"`. * **Key Store Password:** flet build aab --android-signing-key-store-password or Environment Variable: FLET_ANDROID_SIGNING_KEY_STORE_PASSWORD="value". (Not read from pyproject.toml for security). * **Key Password:** flet build aab --android-signing-key-password or Environment Variable: FLET_ANDROID_SIGNING_KEY_PASSWORD="value". (Defaults to key store password if not provided. Not read from pyproject.toml for security).

    Disable Splash Screen

  • The splash screen is enabled by default.
  • Disable via CLI: flet build apk --no-android-splash.
  • Disable via pyproject.toml: `[tool.flet.splash]
  • android = false`.

    Android Manifest Configuration

    Configure elements for AndroidManifest.xml via CLI or pyproject.toml.

  • **Meta-data:** Add arbitrary name-value pairs.
  • * CLI: flet build apk --android-meta-data name_1=value_1 name_2=value_2 * pyproject.toml: `[tool.flet.android.meta_data] "name_1" = value_1 "name_2" = value_2` * Supported value types: String, Integer, Boolean, Float.

  • **Features:** Declare hardware or software features used by the app.
  • * CLI: flet build apk --android-features name_1=required_1 name_2=required_2 * pyproject.toml: `[tool.flet.android.feature] "name_1" = required_1 "name_2" = required_2` * required is a boolean (true or false). * Default features: "android.software.leanback" = False, "android.hardware.touchscreen" = False.

  • **Permissions:** Declare necessary permissions.
  • * CLI: flet build --android-permissions permission=True|False ... * Example: flet build --android-permissions android.permission.READ_EXTERNAL_STORAGE=True android.permission.WRITE_EXTERNAL_STORAGE=True --android-features android.hardware.location.network=False * pyproject.toml: `[tool.flet.android.permission] "android.permission.CAMERA" = true` * Default permissions: android.permission.INTERNET (can be disabled with android.permission.INTERNET=False).

    ADB Tips

  • **Interactive Shell:** adb shell
  • **Root Access:** su (in adb shell)
  • **Download Files:** adb pull