U
U
uvv992022-03-05 17:35:42
Python
uvv99, 2022-03-05 17:35:42

When building apk KIVY for android, the program starts and resets, why?

I'm trying to build an apk application for android on KIVY. When you start the program on the phone, it opens for 3-4 seconds and disappears. The essence of the program: scan qr and send signals to the server (website): start work, pause, finish, etc. I build the application in google colab with the following commands:

!pip install buildozer

!pip install cython==0.29.19

!sudo apt-get install -y \
    python3-pip \
    build-essential \
    git \
    python3 \
    python3-dev \
    ffmpeg \
    libsdl2-dev \
    libsdl2-image-dev \
    libsdl2-mixer-dev \
    libsdl2-ttf-dev \
    libportmidi-dev \
    libswscale-dev \
    libavformat-dev \
    libavcodec-dev \
    zlib1g-dev

!sudo apt-get install -y \
    libgstreamer1.0 \
    gstreamer1.0-plugins-base \
    gstreamer1.0-plugins-good

!sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev openssl libgdbm-dev libgdbm-compat-dev liblzma-dev libreadline-dev libncursesw5-dev libffi-dev uuid-dev libffi6

!sudo apt-get install libffi-dev

!sudo pip3 install kivy
!sudo pip3 install kivymd
!sudo pip install zbarcam
!sudo pip install urllib3
!sudo pip install requests
!sudo pip install chardet
!sudo pip install idna
!sudo pip install opencv-python
!sudo pip install Pillow
!sudo pip install kivy-garden.xcamera
!sudo pip install pyzbar
!sudo pip install python-android

Then !buildozer init get the spec file and !buildozer -v android debug run it.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.clock import Clock
from requests import get, post


ENDPOINT = 'https://code-factory.site/'

Window.size = (560, 780)
Window.clearcolor = (0, 0.17, 0.55, 1)


notice_list = []
try:
    with open('f.txt', 'r') as f:
        file = f.readlines()
        department = file[0].replace('\n', '')
        otk = file[1].replace('\n', '')
except:
    department = None
    otk = None

buttons = """
        Button:
            id: back
            text: 'ВОЗВРАЩЕНИЕ ЗАКАЗА НА ДОРАБОТКУ'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.server('back', False)
""" if otk == "otk" or otk == None else ''

Builder.load_string(f"""
<MenuScreen>:
    BoxLayout:
        orientation: 'vertical'
        padding: 5, 5
        spacing: 5

        Button:
            text: 'УВЕДОМЛЕНИЯ'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.clear()
            Image:
                source: 'bell-ring.png'
                id: bell
                opacity: 0
                size_hint: None, None
                size: 30, 30
                center_x: 330
                center_y: 450
        Button:
            text: 'ЗАКАЗЫ В РАБОТЕ'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.manager.current = 'foto'
        Button:
            text: 'СКАНИРОВАНИЕ QR'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.manager.current = 'scan'

<AllScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'KM'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('km')
        Button:
            text: 'KMД'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('kmd')
        Button:
            text: 'ТМЦ'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('tmc')
        Button:
            text: 'ЗАГОТОВКА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('zagatovka')
        Button:
            text: 'СБОРКА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('sborka')
        Button:
            text: 'СВАРКА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('svarka')
        Button:
            text: 'УПАКОВКА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('upakovka')

<All2Screen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Производство'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('not_otk')
        Button:
            text: 'ОТК'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.save('otk')

<FotoScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            background_color: (0, 0.17, 0.55, 1)
            on_press: root.manager.current = 'menu'
            Image:
                source: 'demo_01.png'
                size: self.parent.size

<ScanScreen>:
    BoxLayout:
    #:import ZBarCam kivy_garden.zbarcam.ZBarCam
        id: box
        orientation: 'vertical'
        padding: 5, 5
        spacing: 5
        ZBarCam:
            id: qrcodecam
        Label:
            size_hint: None, None
            size: self.texture_size[0], 50
            id: qr_text
            text: ' '

        Button:
            text: 'РАБОТА НАЧАТА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.server('start', True)
        Button:
            text: 'РАБОТА ПРИОСТАНОВЛЕНА/ВОЗОБНОВЛЕНА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.server('stop', True)
        Button:
            text: 'ОБРАТНО В МЕНЮ'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.manager.current = 'menu'
        Button:
            text: 'ОТСКАНИРОВАТЬ'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.get_qr()
        Button:
            text: 'РАБОТА ЗАКОНЧЕНА'
            background_color: (0, 0.12, 0.52, 1)
            on_press: root.server('end', True)
""" + buttons)


class MenuScreen(Screen):
    def clear(self):
        global notice_list
        notice_list.clear()


class AllScreen(Screen):
    def save(self, select):
        global department
        department = select

        with open('f.txt', 'w') as f:
            f.write(f'{department}\n')

        App.get_running_app().root.current = 'select_otk'

class All2Screen(Screen):
    def save(self, select):
        global otk
        otk = select

        with open('f.txt', 'a') as f:
            f.write(f'{otk}\n')

        if otk != 'otk':
            scan_screen = App.get_running_app().root.get_screen('scan')

            scan_screen.ids['box'].remove_widget(scan_screen.ids['back'])

        App.get_running_app().root.current = 'menu'

class FotoScreen(Screen):
    pass

class ScanScreen(Screen):
    def server(self, command, check_otk):
        r = get(f'https://code-factory.site/api/give_csrf')
        csrf = r.text.split('=')[-1][1:-2]
        cookies = r.cookies.get_dict()

        add = ''
        if check_otk and otk == 'otk':
            add = '_otk'

        post(f'https://code-factory.site/api/{command}{add}/{department}',
             data = {'id': self.ids['qr_text'].text.split('\n')[0], 'csrfmiddlewaretoken': csrf},
             cookies = cookies)

        self.ids['qr_text'].text = ''

    def get_qr(self):
        qr = ''.join([symbol.data.decode('utf-8') for symbol in self.ids['qrcodecam'].symbols])

        self.ids['qr_text'].text = qr


def get_notice(_):
    for i in get(f'https://code-factory.site/api/notice/{department}').text.split(','):
        if i != '':
            notice_list.append(i)

    if len(notice_list) > 0:
        App.get_running_app().root.get_screen('menu').ids['bell'].opacity = 1
    else:
        App.get_running_app().root.get_screen('menu').ids['bell'].opacity = 0


class MainApp(App):
    def build(self):
        sm = ScreenManager()

        sm.add_widget(MenuScreen(name='menu'))
        sm.add_widget(ScanScreen(name='scan'))
        sm.add_widget(FotoScreen(name='foto'))
        sm.add_widget(AllScreen(name='all'))
        sm.add_widget(All2Screen(name='select_otk'))

        if department == None or department == 'None' or otk == None or otk == 'None':
            sm.current = 'all'
        else:
            sm.current = 'menu'

        return sm


if __name__=='__main__':
    Clock.schedule_interval(get_notice, 3)

    MainApp().run()


Shows an error:

62236bc7f1026989919160.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uvv99, 2022-03-06
@uvv99

Victory! It was necessary to add permission to access the camera and the Internet in buildozer.spec

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question