APK Crashes immediately on android while clicking on the app icon, Kivymd

Question:

I’m making an android app project using the Kivy framework. I have created 50% of my project. It works on Windows perfectly then I think to first try and check is it easily converting to APK file and runs on android? I have created 4 screens SignIn, Signup, HomeScreen and > CameraUI.

Using an opencv library feeding from the webcam. I don’t know what should we put in the cv2.VideoCapture() paraenthesis that it should open the android phone camera for now running on the windows i put 0 in the paranthesis

Now I’m stuck in APK it crashes immediately while clicking on its icon. As there is no error showing while creating an APK using > Buildozer. I search a lot to solve the problem but didn’t make it. I found a lot of questions related to the same problem but I can’t find the solution.

The main.py code is below

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import cv2

Window.size = (350, 600)

class LoginScreen(Screen):
    pass


class RegistrationScreen(Screen):
    pass


class HomeScreen(Screen):
    pass


class WebCamScreen(Screen):
    flag = None

    def do_start(self):
        flag = False
        self.capture = cv2.VideoCapture(0)
        Clock.schedule_interval(self.load_video, 1.0 / 24.0)

    def load_video(self, *args):
        ret, frame = self.capture.read()
        self.image_frame = frame
        # frame = frame[220:220+250, 400:400+250, :]
        buffer = cv2.flip(frame, 0).tostring()
        image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt="bgr")
        image_texture.blit_buffer(buffer, colorfmt="bgr", bufferfmt="ubyte")
        self.ids.img.texture = image_texture

    def capture_image(self):
        try:
            image_name = "first_pic.jpg"
            cv2.imwrite(image_name, self.image_frame)
        except AttributeError:
            self.flag = MDDialog(
                title="WARNING!",
                text="Please Start the Camera First...!",
                buttons=[
                    MDFlatButton(
                        text="Okay",
                        theme_text_color="Custom",
                        text_color="orange",
                        on_release=self.close_dialog
                    ),
                ],
            )
            self.flag.open()

    def close_dialog(self, obj):
        self.flag.dismiss()


class MainApp(MDApp):

    def build(self):
        screen_manger = ScreenManager()
        screen_manger.add_widget(LoginScreen(name="login"))
        screen_manger.add_widget(RegistrationScreen(name="registration"))
        screen_manger.add_widget(HomeScreen(name="home"))
        screen_manger.add_widget(WebCamScreen(name="camera"))

        return screen_manger


if __name__ == "__main__":
    MainApp().run()

KV code

<LoginScreen>
    MDFloatLayout:               
        MDLabel:
            text: "Sign In"
            pos_hint: {"center_x": 0.35, "center_y": 0.7}
            size_hint: (0.5, 0.5)
            font_style: "H4"
            
        MDTextField
            hint_text: "Username"
            mode: "rectangle"
            pos_hint: {"center_x": 0.5, "center_y": 0.6}
            size_hint_x: 0.8
            icon_left: "account-circle"

        MDTextField
            hint_text: "Password"
            mode: "rectangle"
            size_hint_x: 0.8
            password: True
            icon_left: "key-variant"
            pos_hint: {"center_x": 0.5, "center_y": 0.47}
        
        MDFlatButton:
            text: "Forget Password?"
            theme_text_color: "Custom"
            text_color: "orange"
            pos_hint: {"center_x": 0.74, "center_y": 0.39}
        
        MDRaisedButton:
            text: "Sign In"
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}
            on_release:
                root.manager.transition.direction = "left"
                root.manager.current = "home"
            
        MDLabel:
            text: "Don't have an account?"
            size_hint: (0.5, 0.5)
            font_style: "Caption"
            pos_hint: {"center_x": 0.49, "center_y": 0.15}
        
        MDFlatButton:
            text: "Register"
            theme_text_color: "Custom"
            text_color: "orange"
            pos_hint: {"center_x": 0.7, "center_y": 0.15}
            on_release:
                root.manager.transition.direction = "left"
                root.manager.current = "registration"
            
<RegistrationScreen>
    MDFloatLayout:               
        MDLabel:
            text: "Sign Up"
            pos_hint: {"center_x": 0.35, "center_y": 0.75}
            size_hint: (0.5, 0.5)
            font_style: "H4"
            
        MDTextField
            hint_text: "Username"
            mode: "rectangle"
            pos_hint: {"center_x": 0.5, "center_y": 0.65}
            size_hint_x: 0.8
            icon_left: "account-circle"

        MDTextField
            hint_text: "Phone Number"
            mode: "rectangle"
            size_hint_x: 0.8
            password: True
            icon_left: "dialpad"
            pos_hint: {"center_x": 0.5, "center_y": 0.53}
        
        MDTextField
            hint_text: "Password"
            mode: "rectangle"
            size_hint_x: 0.8
            password: True
            icon_left: "key-variant"
            pos_hint: {"center_x": 0.5, "center_y": 0.41}
               
        MDRaisedButton:
            text: "Sign Up"
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}
            
        MDLabel:
            text: "Already have an account?"
            size_hint: (0.5, 0.5)
            font_style: "Caption"
            pos_hint: {"center_x": 0.49, "center_y": 0.15}
        
        MDFlatButton:
            text: "Sign In"
            theme_text_color: "Custom"
            text_color: "orange"
            pos_hint: {"center_x": 0.75, "center_y": 0.15}
            on_release:
                root.manager.transition.direction = "right"
                root.manager.current = "login"
                
<HomeScreen>
    MDFloatLayout: 
        MDRaisedButton:
            text: "Register Your Face"
            size_hint: (0.7, 0.05)
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.85}
            on_release:
                root.manager.transition.direction = "left" 
                root.manager.current = "camera" 
         
        MDRaisedButton:
            text: "Compare Faces"
            size_hint: (0.7, 0.05)
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.7}
        
        MDRaisedButton:
            text: "Remove Face Data"
            size_hint: (0.7, 0.05)
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.55}
    
<WebCamScreen>
    MDFloatLayout:
        MDRaisedButton:
            text: "Start Camera"
            size_hint_x: None
            size_hint_y: None
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.95}
            on_release:
                root.do_start()
            
        Image:
            id: img
            size_hint_x: 0.85
            size_hint_y: 0.5
            pos_hint: {"center_x": 0.5, "center_y": 0.6}
            
        MDIconButton:
            icon: "camera"
            md_bg_color: "orange"
            pos_hint: {"center_x": .5, "center_y": .2}
            on_release:
                root.capture_image()               

I’m doing it on Google Colab following this blog 2. The Google Colab Way!. I’m running this on Widnows 11.

and my buildozer.spec code is

# (str) Title of your application
title = Face Recognizer

# (str) Package name
package.name = myapp

# (str) Package domain (needed for android/ios packaging)
package.domain = org.test

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

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

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy,kivymd,opencv,Pillow,urllib3,Kivy-Garden,numpy

# (str) Supported orientation (one of landscape, sensorLandscape, portrait or all)
orientation = portrait

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

# Kivy version to use
osx.kivy_version = 1.9.1

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

# (list) Permissions
android.permissions = CAMERA

# (str) Path to a custom kivy-ios folder
#ios.kivy_ios_dir = ../kivy-ios
# Alternately, specify the URL and branch of a git checkout:
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master

# Another platform dependency: ios-deploy
# Uncomment to use a custom checkout
#ios.ios_deploy_dir = ../ios_deploy
# Or specify URL and branch
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.10.0

# (bool) Whether or not to sign the code
ios.codesign.allowed = false

# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level = 2

# (int) Display warning if buildozer is run as root (0 = False, 1 = True)
warn_on_root = 1

And is it mandatory to put the version of the library/package in the buildozer.spec file like (kivymd==this_version).

And thanks in advance 🙂
And i will realy appreciate the answers to solve my this problem <3.

Asked By: Jamal Khan

||

Answers:

You can use logcat to debug your app, find in gooogle how to use it!

This issue is solved by adding the kivy and kivymd versions which I/you used in mine/your project. For my case it was kivy==2.1.0 and kivymd==1.1.1

You can use logcat to debug your app, find in gooogle how to use it!

While the answer of @Сервер Чауш is also useful. the logcat is better for debugging.

Answered By: Jamal Khan