pythonandroidkivybuildozer

Android Kivy app crashes when i try to change screens


When i press "continue" button on screen or "to menu" on app crashes. It happens only on android and on PC it works just fine (if gps function is disabled). The final screen is there jsut to show what need and will be writen in Json file. I don't now why some buttons do crash the app and the other don't while they all use one method.

is there some way to see error code on android to debug my app.

Thank you in advance

main.py file

from kivy.lang import Builder
from plyer import gps
from kivy.app import App
from kivy.properties import StringProperty
from kivy.clock import mainthread
from kivy.utils import platform
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
import json
import time


Builder.load_file(filename='main.kv')

class GpsMixin():
    def start_gps(self) -> None:
        gps.start(0,Trailz().MinDistanceSetting)
    
    def stop_gps(self) -> None:
        gps.stop()
    
    def Pass(self) -> None:
        pass
    
    
class GpsRecordingScreen(Screen,GpsMixin):
    pass


class GpsPausedScreen(Screen,GpsMixin):
    pass


class GpsInfoScreen(Screen,GpsMixin):
    pass


class MenuScreen(Screen):
    pass


class TestScreen(Screen):
    pass


MainScreenManager = ScreenManager()


class Trailz(App):
    
    gps_location = StringProperty()
    
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)
        self._GPSJsonDict = {'GPSData':{}}
        self._jsonTrailPath = "saved_trails/"
        self._MinDistancePar = 1
        
    def switch_toSTR(self, screenSTR : str) -> None:
        MainScreenManager.switch_to(MainScreenManager.get_screen(screenSTR))
    
    def request_android_permissions(self):
        from android.permissions import request_permissions, Permission
        def callback(permissions, results):
            if all([res for res in results]):
                print("callback. All permissions granted.")
            else:
                print("callback. Some permissions refused.")
        request_permissions([Permission.ACCESS_COARSE_LOCATION,Permission.ACCESS_FINE_LOCATION], callback)
    
    def build(self):
        global MainScreenManager
        
        MainScreenManager.add_widget(GpsInfoScreen(name='GpsInfoScreen'))
        MainScreenManager.add_widget(MenuScreen(name='MenuScreen'))
        MainScreenManager.add_widget(GpsRecordingScreen(name='GpsRecordingScreen'))
        MainScreenManager.add_widget(GpsPausedScreen(name='GpsPausedScreen'))
        MainScreenManager.add_widget(TestScreen(name='TestScreen'))
        try:
            gps.configure(on_location=self.on_location)
        except NotImplementedError:
            import traceback
            traceback.print_exc()
            
        if platform == "android":
            
            print("gps.py: Android detected. Requesting permissions")
            self.request_android_permissions()
        return MainScreenManager
        
    def open_gps_recording(self) -> None:
        if platform == "ios" or platform == "android":
            MainScreenManager.switch_to(MainScreenManager.get_screen('GpsRecordingScreen'))
        else:
            pass
        
    @mainthread   
    def on_location(self, **kwargs) -> None:
        self._GPSJsonDict['GPSData'][int(time.time()) % 2592000] = kwargs
        self.gps_location = '\n'.join([
            '{}={}'.format(k, v) for k, v in kwargs.items()])
        
    def Save_GPS_to_json(self) -> None:
        if MainScreenManager.get_screen('GpsInfoScreen').ids.GPSNameInput.text != "" and MainScreenManager.get_screen('GpsInfoScreen').ids.GPSDescriptionInput.text != "" and MainScreenManager.get_screen('GpsInfoScreen').ids.GPSMinDistanceInput.text != "":
            self._GPSJsonDict['name'] = MainScreenManager.get_screen('GpsInfoScreen').ids.GPSNameInput.text
            self._GPSJsonDict['discription'] = MainScreenManager.get_screen('GpsInfoScreen').ids.GPSDescriptionInput.text
            self._GPSJsonDict['MinDistance'] = MainScreenManager.get_screen('GpsInfoScreen').ids.GPSMinDistanceInput.text
            #with open(str(self._jsonTrailPath + "_".join(MainScreenManager.get_screen('GpsInfoScreen').ids.GPSNameInput.text.split(" ")) + ".json"), "w") as File:
                #json.dump(self._GPSJsonDict, File)
            MainScreenManager.get_screen('TestScreen').ids.TestLabel.text = str(self._GPSJsonDict)
            MainScreenManager.switch_to(MainScreenManager.get_screen('TestScreen'))
            
    @property
    def MinDistanceSetting(self) -> int:
        return self._MinDistancePar
    
    @property
    def GPSJsonDict(self) -> dict:
        return self._GPSJsonDict
    
    
if __name__ == '__main__':
    Trailz().run()
    

main.kv file

<MenuScreen>
    BoxLayout:
        Button:
            text: "start trail recording"
            on_press: app.open_gps_recording()

<GpsRecordingScreen>:
    BoxLayout:
        Label:
            text: app.gps_location
        ToggleButton:
            id: StartRecording
            text: "start" if self.state == 'normal' else "stop"
            on_press:
                root.stop_gps() if self.state == 'normal' else root.Pass()
                app.switch_toSTR('GpsPausedScreen') if self.state == 'normal' else root.start_gps() 
                

<GpsPausedScreen>
    BoxLayout:
        Button:
            text: "continue"
            on_press:
                app.switch_toSTR('GpsRecordingScreen')
                
        Button:
            text: "stop"
            on_press:
                app.switch_toSTR('GpsInfoScreen')


<GpsInfoScreen>
    GridLayout:
        cols: 1
        Image:
            size_hint: 2, 2
            source: 'map.jpeg'
            size: self.texture_size
        TextInput:
            size_hint: 0.3, 0.3
            id: GPSNameInput
            hint_text: "name"
        TextInput:
            size_hint: 0.3, 0.3
            id: GPSDescriptionInput
            hint_text: "description"
        TextInput:
            size_hint: 0.3, 0.3
            id: GPSMinDistanceInput
            hint_text: "delta"
        Button:
            size_hint: 0.3, 0.3
            text: "Сохранить"
            on_press:app.Save_GPS_to_json()

<TestScreen>
    GridLayout:
        cols: 1
        Label:
            id: TestLabel
            text: "None"
        Button:
            size_hint: 0.3, 0.3
            text: "to menu"
            on_press: app.switch_toSTR('MenuScreen')


buildozer.spec file

[app]

title = Trailz
package.name = trailz
package.domain = org.gpstest
source.dir = .
source.include_exts = py,png,jpg,kv,atlas,jpeg
version = 0.1
requirements = python3, kivy, android, https://github.com/HyTurtle/plyer/archive/master.zip
orientation = portrait
osx.python_version = 3
osx.kivy_version = 1.9.1
fullscreen = 0
android.permissions = INTERNET,ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION
android.archs = arm64-v8a, armeabi-v7a
android.allow_backup = True
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.10.0
ios.codesign.allowed = false
[buildozer]
log_level = 2
warn_on_root = 1

I've tried using 'app.root.switch_to(app.root.get_screen('MenuScreen'))'


Solution

  • Not well documented, but when you use the switch_to() method of ScreenManager it actually removes the current Screen from the ScreenManager. In your switch_toSTR() method, try changing:

    MainScreenManager.switch_to(MainScreenManager.get_screen(screenSTR))
    

    to:

    MainScreenManager.current = screenSTR
    

    See the documentation.

    Concerning the error message on your android, you can connect your android to your PC and run:

    buildozer -v android debug deploy run logcat > my_log.txt
    

    The my_log.txt file should contain any error messages. See the documentation.