flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
10.85k stars 417 forks source link

Local Lottie Assets Not Rendering in Deployed Android APK #3083

Open auphof opened 4 months ago

auphof commented 4 months ago

Issue Description: I am developing an Android application using Flet, where I'm trying to display a local Lottie asset (B.json) within a deployed APK.

# -------------------------------------------------------- 
# Assets folder tree view
# --------------------------------------------
# assets
# ├── B.json
# ├── icon.png
# └── images
#     ├── B.json
#     └── photo.png
  1. ❌ the local Lottie asset fails to display in a deployed APK,
  2. ❌ src for lottie asset is different on LINUX app vs APK app
  3. ✅ if src is a URL lottie asset renders correctly in both APK and LINUX app
  4. ✅ Image assets (png) render correctly in both APK and LINUX app

I've verified that the file B.json is indeed present in the APK app.zip. However the assets folder is not present in the temporary flutter build folder nor is the assets dir present in the pubspec.yaml

in modifying the flet build as follows I can make it work

diff --git a/sdk/python/packages/flet/src/flet/cli/commands/build.py b/sdk/python/packages/flet/src/flet/cli/commands/build.py
index 0006751..5f28698 100644
--- a/sdk/python/packages/flet/src/flet/cli/commands/build.py
+++ b/sdk/python/packages/flet/src/flet/cli/commands/build.py
@@ -432,6 +432,35 @@ class Command(BaseCommand):
         print("Customizing app icons and splash images...", end="")
         assets_path = python_app_path.joinpath("assets")
         if assets_path.exists():
+            if target_platform == "apk":
+                # Configure in APK for assets and update pubspec.yaml
+                # TODO: I expect this should apply to abb as well
+                flutter_apk_assets_dir = "assets"
+                flutter_apk_assets_path = self.flutter_dir / flutter_apk_assets_dir
+                flutter_apk_assets_path.mkdir(exist_ok=True)
+
+                def ignore_files(dir, files):
+                    # TODO: understand what should be excluded or included
+                    return [f for f in files if f.endswith(".png") or f == "temp"]
+
+                print(
+                    f"\nCopying additional APK assets:  {str(flutter_apk_assets_path)}"
+                )
+
+                # copy `assets` directory contents to the output directory ignoring some files
+                copy_tree(
+                    str(assets_path), str(flutter_apk_assets_path), ignore=ignore_files
+                )
+
+                print(
+                    f'PreBuild pubspec.yaml flutter assets: {pubspec["flutter"]["assets"]}'
+                )
+                if f"{flutter_apk_assets_dir}/" not in pubspec["flutter"]["assets"]:
+                    print(f"adding {flutter_apk_assets_dir}/ to pubspec.yaml")
+                    pubspec["flutter"]["assets"].append(f"{flutter_apk_assets_dir}/")
+                    print(
+                        f'Build pubspec.yaml flutter assets: {pubspec["flutter"]["assets"]}'
+                    )
+
             images_dir = "images"
             images_path = self.flutter_dir.joinpath(images_dir)
             images_path.mkdir(exist_ok=True)

Environment:

Expected Behavior: The Lottie asset B.json should render as it does in the local linux development environment. local Lottie asset src should be consistent across target deployments ie Linux

Actual Behavior: The Lottie local asset does not render in the deployed APK, while other assets such as local PNG's and Lottie URL asset appear correctly.

Code Example: Here is the relevant portion of code where the Lottie assets are consumed for intended to display: In the spirit of keeping this issue as brief as possible, the code is in a gist , The following gist creates a series of cards to find what permutations work and what if any errors are able to be captured see https://gist.github.com/auphof/32f4f22328374d4e13ebc5a28206fb13

# The following gist creates a series of cards to find what permutations work and 
# what if any errors are able to be captured
# see https://gist.github.com/auphof/32f4f22328374d4e13ebc5a28206fb13

# Following work  with modified build for ✅APK but  not in ❌ WSL
lottie_card_b0 = create_lottie_card("B0", src="./assets/B.json")
lottie_card_b1 = create_lottie_card("B1", src="assets/B.json")

# Following Fail  still fail with modified ❌ APK but work in ✅ WSL
lottie_card_b2 = create_lottie_card("B2", src="B.json")
lottie_card_b5 = create_lottie_card("B5", src="/B.json")
lottie_card_b6 = create_lottie_card("B6", src="/images/B.json")

# Following  still fail with modified ❌ APK and  fail in ❌ WSL
lottie_card_b3 = create_lottie_card("B3", src="/assets/B.json")

Additional Context:

Attempts to Resolve:

Any insights or alternative suggestions on how to resolve this issue would be greatly appreciated!

auphof commented 4 months ago

Have just noted this similar issue created at similar time #3082

blackCmd commented 4 months ago

Same problem with Lottie local file.