Packaging app for Android
Introduction
Flet CLI provides flet build apk and flet build aab commands that allow packaging Flet app into Android APK and Android App Bundle (AAB) respectively.
Prerequisites
Native Python packages
Native Python packages (vs "pure" Python packages written in Python only) are packages that partially written in C, Rust or other languages producing native code. Example packages are numpy, cryptography, lxml, pydantic.
When packaging Flet app for Android with flet build command such packages cannot be installed from PyPI, because there are no wheels (.whl) for Android platform.
Therefore, you have to compile native packages for Android on your computer before running flet build command.
Work in progress
We are actively working on automating the process described below - it's #1 item in our backlog.
Flet uses Kivy for Android to build Python and native Python packages for Android.
To build your own Python distributive with custom native packages and use it with flet build command you need to use p4a tool provided by Kivy for Android.
p4a command-line tool can be run on macOS and Linux (WSL on Windows).
To get Android SDK install Android Studio.
On macOS Android SDK will be located at $HOME/Library/Android/sdk.
Install Temurin8 to get JRE 1.8 required by sdkmanager tool:
مطلب مورد نیاز شما به توضیح مراحل بستهبندی یک برنامه فلت برای سیستم عامل اندروید میپردازد. ابزار خط فرمان (CLI) فلت، دستورات flet build apk و flet build aab را برای ساخت فایلهای APK و AAB فراهم میکند.
پیشنیازها
کتابخانههای بومی پایتون: این بستهها به جز کد پایتون معمولی، شامل کدی هستند که به زبانهای دیگری مانند C یا Rust نوشته شدهاند. برای ساخت یک برنامه اندروید با فلت، نمیتوانید این بستهها را مستقیماً از PyPI نصب کنید و باید ابتدا آنها را برای اندروید بر روی کامپیوتر خود کامپایل کنید.
ابزار p4a از Kivy: برای ساخت توزیع پایتون با بستههای سفارشی، باید از ابزار p4a که توسط Kivy ارائه شده استفاده کنید.
Choose No to "Do you want automatically install prerequisite JDK? [y/N]".
NOTE: The library you want to build with p4a command should have a recipe in this folder. You can submit a request to make a recipe for the library you need or create your own recipe and submit a PR.
When p4a command completes a Python distributive with your custom libraries will be located at:
$HOME/.python-for-android/dists/mydist
In the terminal where you run flet build apk command to build your Flet Android app run the following command to store distributive full path in SERIOUS_PYTHON_P4A_DIST environment variable:
Build your app by running flet build apk command to build .apk.
You app's bundle now includes custom Python libraries.
flet build apk
Build an Android APK file from your app.
This command builds release version. 'release' builds don't support debugging and are suitable for deploying to app stores. If you are deploying the app to the Play Store, it's recommended to use Android App Bundles (AAB) or split the APK to reduce the APK size.
Installing APK to a device
The easiest way to install APK to your device is to use adb (Android Debug Bridge) tool.
adb is a part of Android SDK. For example, on macOS, if Android SDK was installed with Android Studio the location of adb tool will be at ~/Library/Android/sdk/platform-tools/adb.
Check this article for more information about installing and using adb tool on various platforms.
To install APK to a device run the following command:
adb install <path-to-your.apk>
If more than one device is connected to your computer (say, emulator and a physical phone) you can use -s option to specify which device you want to install .apk on:
adb -s install <path-to-your.apk>
where can be found with adb devices command.
Building platform-specific APK
By default, Flet builds "fat" APK which includes binaries for both arm64-v8a and armeabi-v7a architectures.
If you know/control Android device your app will be distributed on you can build a smaller APK for a specific architecture.
Troubleshooting Android
To run interactive commands inside simulator or device:
adb shell
To overcome "permissions denied" error while trying to browse file system in interactive Android shell:
su
To download a file from a device to your local computer:
adb pull
flet build aab
Build an Android App Bundle (AAB) file from your app.
This command builds release version. 'release' builds don't support debugging and are suitable for deploying to app stores. App bundle is the recommended way to publish to the Play Store as it improves your app size.
Splash screen
By default, generated Android app will be showing a splash screen with an image from assets directory (see below) or Flet logo. You can disable splash screen for Android app with --no-android-splash option.
به فارسی در مورد متون بالا توضیح بده
مراحل نصب و راهاندازی
ابتدا SDK اندروید و نرمافزار Android Studio را نصب کنید.
JRE مورد نیاز (نسخه 1.8) را با نصب Temurin8 دریافت کنید.
متغیرهای محیطی مخصوص SDK و NDK اندروید را تنظیم کنید.
ابزار p4a را از نسخه فورک شده در گیت هاب نصب کنید.
کتابخانه Cython را نیز نصب کنید.
با کمک دستور p4a توزیعی از پایتون با کتابخانههای سفارشی خود ایجاد کنید.
ساخت برنامه
پس از آمادهسازی محیط و توزیع اختصاصی پایتون، میتوانید با اجرای دستور flet build apk فایل APK برنامه خود را بسازید. همچنین میتوانید با استفاده از دستور adb install این فایل را بر روی دستگاه اندرویدی خود نصب کنید.
نکات اضافی
امکان ایجاد فایل AAB برای بهبود اندازه و کارایی برنامه وجود دارد.
میتوانید فایلهای APK خاص معماری مشخص را بسازید تا حجم بهتری داشته باشید.
برای شبیهسازی و بررسی خطاها، میتوانید از ابزارهای اندروید مثل adb shell استفاده کنید.
این مراحل، فرآیند ساخت و توزیع برنامههای فلت برای اندروید را بهبود میبخشد و به روشی مناسب برای توسعه دهندگان جهت ایجاد بستههای قابل استفاده در فروشگاههای اپلیکیشن تبدیل میشود.