flet icon indicating copy to clipboard operation
flet copied to clipboard

Flet app apk packing including external libraries

Open islam-Kawasa opened this issue 1 year ago • 9 comments

Hi Flet Team,

Thanks for the awesome work on Flet! I have developed many desktop apps using flet and it was excellent as i had been able to use external python libraries that unleashes python's unlimited power beside using flutter ui through the simplicity of python i find the results really amazing. I’m exploring using Flet for Android deployment but noticed that currently, there’s no built-in support for packaging Flet apps along with external Python libraries into a standalone APK.

Are there any plans or ongoing efforts to support this in future releases? It would be great to have an official, streamlined way to bundle dependencies and produce a fully self-contained Android app so that i can develop android apps that inherit the power of python and it's external libraries.

Thanks for any info you can share!

Best regards,

islam-Kawasa avatar Jun 10 '25 10:06 islam-Kawasa

Hello @islam-Kawasa ,

Basically let's talk python. Flet is a great tool that lets us build apps using Python and run them on mobile, but it does have some limits, especially with certain external Python libraries. That’s mostly because Python was originally designed for desktop environments, not mobile phones.

On desktops, Python has access to all the normal system features, drivers, and dependencies so pretty much any library can run if it has the right setup. But on mobile (especially Android and iOS), things are very different. Mobile platforms are much more locked-down for performance and security reasons, and they don’t support a lot of the things that regular Python libraries need — like certain compilers, native extensions, or system-level access.

So when we try to use certain libraries in a Flet app (which runs Python under the hood), we hit those mobile system restrictions. It’s not that Flet is doing something wrong. it’s just working within the limits of mobile operating systems and the way Python itself was originally designed.

Python is like a fish that swims perfectly in a desktop ocean but we’re asking it to fly on mobile, and we have to give it wings one piece at a time. Tools like Flet are helping make that happen, but it’s still a work in progress.

So currently the the team working hard to include and support as many external libraries as possible.

Thanks.

HappyCoder256 avatar Jun 12 '25 01:06 HappyCoder256

@islam-Kawasa flet has support for quite a number of python packages both nonpure and pure python packages. And the team is building wheels for others!

Here is a blog post on that: https://flet.dev/blog/flet-packaging-update/

Benitmulindwa avatar Jun 13 '25 17:06 Benitmulindwa

@islam-Kawasa flet has support for quite a number of python packages both nonpure and pure python packages. And the team is building wheels for others!

Here is a blog post on that: https://flet.dev/blog/flet-packaging-update/

Do you know how can i use this building wheels , in my project, I mean how can i benefit from this wheels , and use it in android for example

Youssef3082004 avatar Jun 13 '25 17:06 Youssef3082004

You just build your app with those packages included,… but make sure they’re among or they’re built on top of these ones:⬇️

https://flet.dev/docs/reference/binary-packages-android-ios

Benitmulindwa avatar Jun 13 '25 20:06 Benitmulindwa

Here's a workaround,

If you're building your APK using Flet, and some packages like non pure aren't being included, the workaround is to manually include those packages in your project directory. Just create a folder like libs, place the required .py files or modules inside, and add sys.path.insert(0, "libs") at the top of your script. This ensures they're imported directly, bypassing issues with packaging during the APK build

HappyCoder256 avatar Jun 14 '25 07:06 HappyCoder256

Here's a workaround,

If you're building your APK using Flet, and some packages like non pure aren't being included, the workaround is to manually include those packages in your project directory. Just create a folder like libs, place the required .py files or modules inside, and add sys.path.insert(0, "libs") at the top of your script. This ensures they're imported directly, bypassing issues with packaging during the APK build

I tried this but it outputs different error

Traceback (most recent call last):
  File "<string>", line 77, in <module>
ModuleNotFoundError: No module named 'certifi'

I Downloaded this file opencv_python_4_10_0_84_1_cp312_cp312_android_24_arm64_v8a_whl from this link https://flet.dev/docs/reference/binary-packages-android-ios

And I put it in the Libs folder, and I added it in the first line after calling sys lib sys.path.insert(0, "libs") , so what is wrong I did to make this error?

Youssef3082004 avatar Jun 22 '25 23:06 Youssef3082004

this is pyproject.toml file

[project]
name = "qrscanner"
version = "0.1.0"
description = ""



dependencies = [
  "flet==0.28.3", 
  "opencv-python==4.10.0.84",
]




[tool.flet.compile]
app = true
packages = false
# cleanup = true # remove unnecessary files upon successful compilation.




[tool.flet.splash]
color = "#FFFF00" # --splash-color
dark_color = "#8B8000" # --splash-dark-color
web = false # --no-web-splash
ios = false # --no-ios-splash
android = false # --no-android-splash


[tool.flet.android]
split_per_abi = false


[tool.flet.android.permission] # --android-permissions
"android.permission.CAMERA" = true
"android.permission.ACCESS_FINE_LOCATION" = false
"android.permission.ACCESS_COARSE_LOCATION" = false
"android.permission.ACCESS_BACKGROUND_LOCATION" = false
"android.permission.RECORD_AUDIO" = false
"android.permission.WRITE_EXTERNAL_STORAGE" = false
"android.permission.READ_EXTERNAL_STORAGE" = false


[tool.flet.android.feature] # --android-features
"android.hardware.camera" = true
"android.hardware.location.network" = false
"android.hardware.location.gps" = false

Youssef3082004 avatar Jun 22 '25 23:06 Youssef3082004

Did you find the solution? i got the same problem when i building a windows app

SinJoker avatar Apr 21 '26 07:04 SinJoker

Closing this as answered, since what's being asked for here is already supported, and has improved a lot since this thread started.

Bundling external libraries into an APK is built into flet build Declare your dependencies in pyproject.toml and flet build apk packages them into the app automatically:

[project]
dependencies = [
  "flet",
  "opencv-python",
]

Pure-Python packages are resolved from PyPI as usual. Non-pure (binary) packages are pulled as pre-built Android/iOS wheels from Flet's own package index (pypi.flet.dev) — see the list of available binary packages. opencv-python is among them (currently up to 5.0.0.93, for Python 3.12–3.14).

A few clarifications on the workarounds discussed above:

  • The libs/ folder + sys.path.insert(0, "libs") trick does not work for binary packages: Python cannot import from a .whl file sitting in a directory, and compiled extension modules can't be loaded from an archive at all. Wheels must be installed by the build pipeline — just list the package in dependencies and let flet build handle it.
  • The No module named 'certifi' error was a side effect of that workaround (bypassing the resolver skips transitive dependencies like certifi), not a packaging gap.

Note: Currently, binary packages must have an Android/iOS wheel available — either on PyPI or on Flet's index. If a package you need isn't on the supported list, you can request its support.

If you hit a packaging problem on a recent Flet version (≥ 0.86), please open a new issue with your pyproject.toml and the full build log. See the Android packaging guide for current docs.

@SinJoker your report is about a Windows desktop build, which is unrelated to this Android thread — please open a separate issue with a minimal repro and build log.

ndonkoHenri avatar Aug 13 '26 09:08 ndonkoHenri