Python Forum
Kivy Flask Buildozer compile apk
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kivy Flask Buildozer compile apk
#1
Hi, I have simple code for Flask local server:

import sys
import io

sys.stdout = io.StringIO()
sys.stderr = io.StringIO()

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from threading import Thread
from flask import Flask

# Flask server definition
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Flask Server!'

def run_flask_app():
    app.run(host='0.0.0.0', port=8000, threaded=True)

class ServerApp(App):

    def build(self):
        self.server_state_label = Label(text="Server is not running")
        self.start_server_button = Button(text="Start Server")
        self.start_server_button.bind(on_press=self.start_server)

        layout = BoxLayout(orientation="vertical")
        layout.add_widget(self.server_state_label)
        layout.add_widget(self.start_server_button)
        return layout

    def start_server(self, instance):
        try:
            # Start the Flask server in a thread
            server_thread = Thread(target=run_flask_app)
            server_thread.start()
            self.server_state_label.text = "Server is running"
        except Exception as e:
            self.server_state_label.text = "Error starting the server"

if __name__ == '__main__':
    ServerApp().run()
I want to compile it to apk for anodrid. I have Win11, installed Oracle VirtualBox with Ubuntu. I installed Kivy, Cython, Buildozer, Flask, etc.
I tried start main.py on Ubuntu, Windows, Anodroid mobile with Pydroid and it works well.
If I compile to apk and install on mobile phone, I see just loading and then app closes itself.
After Buildozer init I changed requirements to python3,flask,kivy,cython and Permissions=INTERNET.
I tried lot of configurations but always the same result = app closes itself.
If I used only gui in kivy, it works well. Other apps also works well - all without Flask.
Any idea how to do it?
Thanks.
Reply
#2
without even reading the blog, just a quick scan, perhaps this will help.
Reply
#3
I tried compile many apps without any issues. But if I add:
from flask import Flask
For example
from kivy.app import App
from kivy.uix.button import Button
from flask import Flask

class MyApp(App):

    def build(self):
        flask_installed = self.check_flask_import()
        if flask_installed:
            return Button(text='Flask is installed!')
        else:
            return Button(text='Flask is not installed!')

    def check_flask_import(self):
        try:
            import flask
            return True
        except ImportError:
            return False

if __name__ == '__main__':
    MyApp().run()
I can start python code on Pydroid, Linux OK. After compile app did not start. If I remove Flask import, it works OK.

Buildozer.spec (part)
[app]

# (str) Title of your application
title = VGServis

# (str) Package name
package.name = VGServis

# (str) Package domain (needed for android/ios packaging)
package.domain = vibroguard.cz

# (str) Source code where the main.py live
source.dir = .

# (list) Source files to include (let empty to include all the files)
source.include_exts = py,png,jpg,kv,atlas

# (list) List of inclusions using pattern matching
#source.include_patterns = assets/*,images/*.png

# (list) Source files to exclude (let empty to not exclude anything)
#source.exclude_exts = spec

# (list) List of directory to exclude (let empty to not exclude anything)
#source.exclude_dirs = tests, bin, venv

# (list) List of exclusions using pattern matching
# Do not prefix with './'
#source.exclude_patterns = license,images/*/*.jpg

# (str) Application versioning (method 1)
version = 0.1

# (str) Application versioning (method 2)
# version.regex = __version__ = ['"](.*)['"]
# version.filename = %(source.dir)s/main.py

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy,numpy,serial,pyserial,usb4a,usbserial4a,flask,flask_cors

# (str) Custom source folders for requirements
# Sets custom source for any requirements with recipes
# requirements.source.kivy = ../../kivy

# (str) Presplash of the application
#presplash.filename = %(source.dir)s/data/presplash.png

# (str) Icon of the application
#icon.filename = %(source.dir)s/data/icon.png

# (list) Supported orientations
# Valid options are: landscape, portrait, portrait-reverse or landscape-reverse
orientation = portrait,landscape

# (list) List of service to declare
#services = NAME:ENTRYPOINT_TO_PY,NAME2:ENTRYPOINT2_TO_PY

#
# OSX Specific
#

#
# author = © Copyright Info

# change the major version of python used by the app
osx.python_version = 3

# Kivy version to use
osx.kivy_version = 1.9.1

#
# Android specific
#

# (bool) Indicate if the application should be fullscreen or not
fullscreen = 0

# (string) Presplash background color (for android toolchain)
# Supported formats are: #RRGGBB #AARRGGBB or one of the following names:
# red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray,
# darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy,
# olive, purple, silver, teal.
#android.presplash_color = #FFFFFF

# (string) Presplash animation using Lottie format.
# see https://lottiefiles.com/ for examples and https://airbnb.design/lottie/
# for general documentation.
# Lottie files can be created using various tools, like Adobe After Effect or Synfig.
#android.presplash_lottie = "path/to/lottie/file.json"

# (str) Adaptive icon of the application (used if Android API level is 26+ at runtime)
#icon.adaptive_foreground.filename = %(source.dir)s/data/icon_fg.png
#icon.adaptive_background.filename = %(source.dir)s/data/icon_bg.png

# (list) Permissions
# (See https://python-for-android.readthedocs.io/en/latest/buildoptions/#build-options-1 for all the supported syntaxes and properties)
android.permissions = android.permission.INTERNET,ACCESS_NETWORK_STATE (name=android.permission.WRITE_EXTERNAL_STORAGE;maxSdkVersion=18)
Any suggestion?
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Buildozer fails building Kivy app for android Kolterdyx 0 3,134 Apr-30-2020, 06:31 PM
Last Post: Kolterdyx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020