Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crash at startup #1184

Closed
brinata opened this issue Dec 29, 2022 · 11 comments · Fixed by #1398
Closed

App crash at startup #1184

brinata opened this issue Dec 29, 2022 · 11 comments · Fixed by #1398
Labels
Backend: Android bug Something isn't working

Comments

@brinata
Copy link

brinata commented Dec 29, 2022

if I insert the following line in my Android App:

"import bleak"

the app crashes at start up in my mobile, but it works under windows 10.

On windows I'm using the following versions:

Pythpn 3.10.9
bleak 0.19.5
Kivy 2.1.0
buildozer 1.4.0

Inside the builddozer.spec file I haven't specify the version of the packages

Can Someone Help me ?

@dlech
Copy link
Collaborator

dlech commented Dec 29, 2022

Without details about the crash and steps to reproduce the problem, it will be difficult for anyone to provide any help.

@brinata
Copy link
Author

brinata commented Dec 30, 2022

it seems that at the startup Android gives out this message:

ModuleNotFoundError: No module named 'async_timeout'

The result is that the app remain in backgrod and doesn't start.

Please find the following info:
- Android Log during app starup ( logcat | grep python)

12-30 11:14:37.490 16697 16734 I python  : Android kivy bootstrap done. __name__ is __main__
12-30 11:14:37.490 16697 16734 I python  : AND: Ran string
12-30 11:14:37.490 16697 16734 I python  : Run user program, change dir and execute entrypoint
12-30 11:14:37.494 16697 16697 W SDLThread: type=1400 audit(0.0:44351): avc: granted { execute } for path="/data/user/0/org.test.myapp/files/app/_python_bundle/modules/_heapq.cpython-39.so" dev="dm-11" ino=63491 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file app=org.test.myapp
12-30 11:14:37.550 16697 16697 W SDLThread: type=1400 audit(0.0:44352): avc: granted { execute } for path="/data/user/0/org.test.myapp/files/app/_python_bundle/modules/_socket.cpython-39.so" dev="dm-11" ino=60886 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file app=org.test.myapp
12-30 11:14:37.554 16697 16697 W SDLThread: type=1400 audit(0.0:44353): avc: granted { execute } for path="/data/user/0/org.test.myapp/files/app/_python_bundle/modules/math.cpython-39.so" dev="dm-11" ino=56633 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file app=org.test.myapp
12-30 11:14:37.554 16697 16697 W SDLThread: type=1400 audit(0.0:44354): avc: granted { execute } for path="/data/user/0/org.test.myapp/files/app/_python_bundle/modules/select.cpython-39.so" dev="dm-11" ino=49359 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file app=org.test.myapp
12-30 11:14:37.680 16697 16734 I python  : [INFO   ] [Logger      ] Record log in /data/user/0/org.test.myapp/files/app/.kivy/logs/kivy_22-12-30_1.txt
12-30 11:14:37.680 16697 16734 I python  : [INFO   ] [Kivy        ] v2.1.0
12-30 11:14:37.681 16697 16734 I python  : [INFO   ] [Kivy        ] Installed at "/data/user/0/org.test.myapp/files/app/_python_bundle/site-packages/kivy/__init__.pyc"
12-30 11:14:37.682 16697 16734 I python  : [INFO   ] [Python      ] v3.9.9 (main, Dec 29 2022, 13:07:01)
12-30 11:14:37.682 16697 16734 I python  : [Clang 14.0.6 (https://android.googlesource.com/toolchain/llvm-project 4c603efb
12-30 11:14:37.682 16697 16734 I python  : [INFO   ] [Python      ] Interpreter at ""
12-30 11:14:37.683 16697 16734 I python  : [INFO   ] [Logger      ] Purge log fired. Processing...
12-30 11:14:37.684 16697 16734 I python  : [INFO   ] [Logger      ] Purge finished!
12-30 11:14:37.687 16697 16734 I python  : [INFO   ] [KivyMD      ] 1.1.1, git-Unknown, 2022-12-27 (installed at "/data/user/0/org.test.myapp/files/app/_python_bundle/site-packages/kivymd/__init__.pyc")
12-30 11:14:37.692 16697 16734 I python  : [INFO   ] [Factory     ] 189 symbols loaded
12-30 11:14:38.223 16697 16734 I python  : [INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2 (img_pil, img_ffpyplayer ignored)
12-30 11:14:38.239 16697 16734 I python  : [INFO   ] [Text        ] Provider: sdl2
12-30 11:14:38.402 16697 16734 I python  : [INFO   ] [Window      ] Provider: sdl2
12-30 11:14:38.423 16697 16734 I python  : [INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
12-30 11:14:38.424 16697 16734 I python  : [INFO   ] [GL          ] Backend used <sdl2>
12-30 11:14:38.424 16697 16734 I python  : [INFO   ] [GL          ] OpenGL version <b'OpenGL ES 3.2 v1.r32p1-01eac0.3d0f892e790e4319a235910ce1559a8a'>
12-30 11:14:38.425 16697 16734 I python  : [INFO   ] [GL          ] OpenGL vendor <b'ARM'>
12-30 11:14:38.425 16697 16734 I python  : [INFO   ] [GL          ] OpenGL renderer <b'Mali-G57 MC2'>
12-30 11:14:38.425 16697 16734 I python  : [INFO   ] [GL          ] OpenGL parsed version: 3, 2
12-30 11:14:38.425 16697 16734 I python  : [INFO   ] [GL          ] Texture max size <16383>
12-30 11:14:38.426 16697 16734 I python  : [INFO   ] [GL          ] Texture max units <16>
12-30 11:14:38.445 16697 16734 I python  : [INFO   ] [Window      ] auto add sdl2 input provider
12-30 11:14:38.446 16697 16734 I python  : [INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
12-30 11:14:38.496 16697 16734 I python  :  Traceback (most recent call last):
12-30 11:14:38.496 16697 16734 I python  :    File "/home/ronni/app/.buildozer/android/app/main.py", line 8, in <module>
12-30 11:14:38.496 16697 16734 I python  :    File "/home/ronni/app/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/__init__.py", line 31, in <module>
**12-30 11:14:38.496 16697 16734 I python  :  ModuleNotFoundError: No module named 'async_timeout**'
12-30 11:14:38.496 16697 16734 I python  : Python for android ended.
12-30 11:14:38.684 16697 16734 D RefBase : #02 pc 0000000000262c40  /data/app/~~Aj8HUdnRrCCyYKRBBOoAUg==/org.test.myapp-VXt7yHROWkpRlag0erB_kQ==/lib/arm64/libpython3.9.so (Py_Exit+32)
12-30 11:14:38.684 16697 16734 D RefBase : #03 pc 0000000000267e24  /data/app/~~Aj8HUdnRrCCyYKRBBOoAUg==/org.test.myapp-VXt7yHROWkpRlag0erB_kQ==/lib/arm64/libpython3.9.so
12-30 11:14:38.684 16697 16734 D RefBase : #04 pc 0000000000267694  /data/app/~~Aj8HUdnRrCCyYKRBBOoAUg==/org.test.myapp-VXt7yHROWkpRlag0erB_kQ==/lib/arm64/libpython3.9.so (PyRun_SimpleStringFlags+136)

- main.py

import asyncio
import os
from kivymd.app import MDApp
from kivy.lang import Builder


import bleak

KV = """
Screen:
    GridLayout:
        rows:2

        ScrollView:
            MDLabel:
                id:mdlab
                size_hint_y: None
                text: "Attesa per scansione BLE"
                height: self.texture_size[1]

        MDRaisedButton:
            id:mdbut
            text: "premi per scansione BLE"
            size_hint_x:1
            on_press: app.premuto_bottone()     
"""

class MainApp(MDApp):

    event = asyncio.Event()

    def build(self):
        self.title = "Antibrina"
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Red"
        self.theme_cls.primary_hue = "400"
        return Builder.load_string(KV)
    


    
    async def avvia_scansione(self):
        
        devices = await bleak.BleakScanner.discover(1)
        if len(devices) == 0:
            self.root.ids["mdlab"].text = "No Device Found!"    
       stringa = ""
       for d in devices:
            stringa += d.address + " " +  str(d.rssi)
            if d.name != None : 
                stringa += " " + d.name
            stringa += "\n"
        self.root.ids["mdlab"].text = stringa





    def premuto_bottone(self):
        self.root.ids["mdlab"].text = "Scanning for 5 seconds...."
        if os.environ.get("P4A_BOOTSTRAP") is not None:
            self.root.ids["mdlab"].text = "Android Ok !"
        else:
            self.root.ids["mdlab"].text = "Android KO !"
        asyncio.create_task(self.avvia_scansione())


app = MainApp()
async def main(app):
    await app.async_run("asyncio")


asyncio.run(main(app))

@dlech
Copy link
Collaborator

dlech commented Dec 30, 2022

ModuleNotFoundError: No module named 'async_timeout'

This is a 3rd-party module and likely needs to be added to some depedency list somewhere. Maybe here?

@dlech dlech added bug Something isn't working Backend: Android labels Dec 30, 2022
@brinata
Copy link
Author

brinata commented Dec 30, 2022

Maybe, but the Android Log Stream write that there is the following import error:

"/home/ronni/app/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/init.py", line 31, in
12-30 11:14:38.496 16697 16734 I python : ModuleNotFoundError: No module named 'async_timeout'

at the line 31 of the file init.py , there is the followinfg code:

sys.version_info < (3, 11):
from async_timeout import timeout as async_timeout
else:
from asyncio import timeout as async_timeout

it seems that the script recognize a python version older that 3.11 and so it import the timeout library drom the external async_timeout package instead of from the asyncio .

I think that Android doesn't find the "async_timeout" lib to import. On the contrary, on Windows, where the this library is installed as dependecy of the bleak package, the python finds the lib to import.

do I have to declare something special inside my buildozer.spec file ?

@dlech
Copy link
Collaborator

dlech commented Dec 30, 2022

I don't know much about how buildozer works. The line of code I linked to above was my best guess of where something needs to be changed.

@brinata
Copy link
Author

brinata commented Dec 30, 2022

I solver the problem adding "async-timeout" in the requirements( the lib name was different from label "async_timeout" used in the import declaration ...)

Now the app starts, but when I try to scan for ble devices, i give back an exception. It desn'pt find the class "com.github.hbldh.bleak.PythonScanCallback

12-30 19:20:36.594  6669  6728 I python  :  Task exception was never retrieved
12-30 19:20:36.595  6669  6728 I python  :  future: <Task finished name='Task-2' coro=<MainApp.avvia_scansione() done, defined at /home/ronni/progetti/app_antibrina/.buildozer/android/app/main.py:44> exception=JavaException('JVM exception occurred: Didn\'t find class "com.github.hbldh.bleak.PythonScanCallback" on path: DexPathList[[zip file "/data/app/-vXBgDmvIyucUqS1PalQ1A==/org.test.myapp-YSxHHXnrmEfi1WXd1BB06A==/base.apk"],nativeLibraryDirectories=[/data/app/~~-vXBgDmvIyucUqS1PalQ1A==/org.test.myapp-YSxHHXnrmEfi1WXd1BB06A==/lib/arm64, /data/app/~~-vXBgDmvIyucUqS1PalQ1A==/org.test.myapp-YSxHHXnrmEfi1WXd1BB06A==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]] java.lang.ClassNotFoundException')>
12-30 19:20:36.595  6669  6728 I python  :  Traceback (most recent call last):
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/app/main.py", line 46, in avvia_scansione
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/__init__.py", line 216, in discover
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/__init__.py", line 118, in __init__
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/backends/scanner.py", line 240, in get_platform_scanner_backend_type
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/backends/p4android/scanner.py", line 21, in <module>
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/bleak/backends/p4android/defs.py", line 31, in <module>
12-30 19:20:36.595  6669  6728 I python  :    File "/home/ronni/progetti/app_antibrina/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/myapp/arm64-v8a/jnius/reflect.py", line 211, in autoclass
12-30 19:20:36.595  6669  6728 I python  :    File "jnius/jnius_export_func.pxi", line 26, in jnius.jnius.find_javaclass
12-30 19:20:36.595  6669  6728 I python  :    File "jnius/jnius_utils.pxi", line 91, in jnius.jnius.check_exception
12-30 19:20:36.596  6669  6728 I python  :  jnius.jnius.JavaException: JVM exception occurred: Didn't find class "com.github.hbldh.bleak.PythonScanCallback" on path: DexPathList[[zip file "/data/app/~~ vXBgDmvIyucUqS1PalQ1A==/org.test.myapp-YSxHHXnrmEfi1WXd1BB06A==/base.apk"],nativeLibraryDirectories=[/data/app/~~-vXBgDmvIyucUqS1PalQ1A==/org.test.myapp-YSxHHXnrmEfi1WXd1BB06A==/lib/arm64, /data/app/~~-vXBgDmvIyucUqS1PalQ1A==/org.test.myapp-YSxHHXnrmEfi1WXd1BB06A==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]] java.lang.ClassNotFoundException

Can you help me ?

Of course it works On windows.

@dlech
Copy link
Collaborator

dlech commented Dec 30, 2022

I solver the problem adding "async-timeout" in the requirements( the lib name was different from label "async_timeout" used in the import declaration ...)

Can you make a pull request to fix this?

Now the app starts, but when I try to scan for ble devices, i give back an exception. It desn'pt find the class "com.github.hbldh.bleak.PythonScanCallback

Sounds like #756

@brinata
Copy link
Author

brinata commented Jan 3, 2023

Sounds Good !

I've included the java files in the buildozer.spec, using the keywords "android.add_src" and it works for the moment, I've tested only the scan function of the library.

is it possible to make some change to load the java files automatically ??

@Prakyathkantharaju
Copy link

If someone else is facing a similar issue with the example app. Along with these fixes mentioned (This issue and #756).

Specify your python3 version in requirements eg. python3==3.10.6

@fenzeldan
Copy link

I had similar problems with the example app. With those fixes, mentioned above, i was able to run it. But i have a different problem now:
I get this exeption thrown:
['android.permission.ACCESS_FINE_LOCATION', 'android.permission.ACCESS_COARSE_LOCATION', 'android.permission.ACCESS_BACKGROUND_LOCATION']

The app has no permissions to access the BLE Module of the phone i think. I already tried different fixes but im not able to fix it.

This is the error log from the phone:
06-29 22:59:00.070 19163 19209 I python : [ERROR ] [Task exception was never retrieved
06-29 22:59:00.070 19163 19209 I python : future] <Task finished name='Task-2' coro=<MainScreen.search_ble() done, defined at /home/daniel/projects/bleapp1/.buildozer/android/app/main.py:32> exception=BleakError("User denied access to ['android.permission.ACCESS_FINE_LOCATION', 'android.permission.ACCESS_COARSE_LOCATION', 'android.permission.ACCESS_BACKGROUND_LOCATION']")>

Im new to coding so im sorry if i missed important information you may need to help.
I would be thankful for help and can provide more information if needed

@obzerving
Copy link

It's August 2023 and brinata's solution of adding async-timeout to the requirements line in buildozer still worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Android bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants