DEV Community

How to use Google Colab to package KivyMD applications with buildozer.

Ngonidzashe Nzenze on August 14, 2021

Let's face it, packaging your kivy application for android can be pretty rough. First you create the app, then you install buildozer only to realiz...
Collapse
 
rhandel profile image
RAH • Edited

NN,
Completed the build but had to change the version of Cython from 0.29.19 to 0.29.21 to complete compile.
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tables 3.8.0 requires cython>=0.29.21, but you have cython 0.29.19 which is incompatible.

!pip install --upgrade Cython==0.29.21 virtualenv

I suspect that's why the resulting APK builds but bombs after the splash screen.
Do you have any suggestions?

RAHRAH

!sudo apt update
!sudo apt install -y git zip unzip openjdk-17-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev
!pip install --upgrade Cython==0.29.21 virtualenv
!git clone https://github.com/kivy/buildozer
%cd buildozer
!python setup.py build
!pip install -e .
%cd ..
!buildozer -v android debug
Enter fullscreen mode Exit fullscreen mode
# (str) Title of your application
#RAH#
title = Nzeneie test

# (str) Package name
#RAH#
package.name = NzenzieTest
# (str) Application versioning (method 1)
#RAH#
version = 0.100
# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
#RAH#
requirements = python3,kivy==2.1.0,https://github.com/kivymd/KivyMD/archive/master.zip,sdl2_ttf==2.0.15,pillow

# (int) Target Android API, should be as high as possible.
#RAH#
android.api = 31
# (bool) If True, then automatically accept SDK license
# agreements. This is intended for automation only. If set to False,
# the default, you will be shown the license when first running
# buildozer.
#RAH#
# android.accept_sdk_license = False
android.accept_sdk_license = True

# (list) The Android archs to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64
# In past, was `android.arch` as we weren't supporting builds for multiple archs at the same time.
#RAH#
android.archs = arm64-v8a
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hi,

So a few things have changed since I've written the article. I have updated it. You can fix the issues you are facing by changing the requirements to: requirements = python3, kivy==2.1.0, kivymd==1.1.1, sdl2_ttf==2.0.15, pillow

And for installing the initial build packages, instead of using the commands with Cython, use the following:

!sudo dpkg --add-architecture i386
!sudo apt-get update
!sudo apt-get install -y build-essential ccache git zlib1g-dev python3 python3-dev libncurses5:i386 libstdc++6:i386 zlib1g:i386 openjdk-8-jdk unzip ant ccache autoconf libtool libssl-dev
Enter fullscreen mode Exit fullscreen mode

This should fix the errors your were encountering. Hope that helps!

Collapse
 
kingdonny profile image
kingdonny • Edited

i have been trying to follow your tutorial on the above project and there
I have tried to follow the required steps from the tutorial including this update but google colab is not creating the apk file as the bin folder is empty.

Please help

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hey. Are you getting any errors during execution?

Thread Thread
 
kingdonny profile image
kingdonny • Edited

No errors but it creates an empty bin folder

Thread Thread
 
kingdonny profile image
kingdonny

have you tried running the code from your end again?

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze • Edited

I can't run it at the moment but could you try this notebook.

Thread Thread
 
kingdonny profile image
kingdonny

thank you very much the apk file was created but the application doesn't on my mobile after installation. i tries to open then shuts down

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze

Could you try installing your app with adb, then running adb logcat, before opening it. The error should be printed within the console.

Thread Thread
 
kingdonny profile image
kingdonny • Edited

Thank you very much

all working good now i think i missed something along the way

you're are the best keep doing the good job

My question is what did you change in the code to make it work this time

Collapse
 
pythonkivyuser profile image
PythonKivyuser

Hi, I have a question about an error which appears while running the App on my Android Phone (Huawei P8lite).
The adb logcat log shows me the two Errors [ E libEGL : validate_display:255 error 3008 (EGL_BAD_DISPLAY) ] and [ ModuleNotFoundError: No module named 'PIL' ] and than crashes.

Do you have an idea how to solve this Error?

I have already done some research and tried to get some information but PIL is dead and the requirement for KivyMD is Pillow, like the devolopers published in Github.

I would appreciate your help and thank you in advance.
If you need some logs to look at I will provide it to you.

Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hello. Did you try adding pillow to the requirements in the buildozer.spec file?

Collapse
 
pythonkivyuser profile image
PythonKivyuser

I've tried it with
[requirements = kivy==2.0.0, github.com/kivymd/KivyMD/archive/m...]
as the developers published it on Github(github.com/kivymd/KivyMD)
and with
[requirements = python3, kivy==2.0.0, kivymd==0.104.2, sdl2_ttf == 2.0.15, pillow]

In both cases the adb logcat log showed me the two Errors.
Have i forgotten something?

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze

Have you tried running:

buildozer android clean

between the changes? If not, try running it and using the following requirements:

requirements = python3,kivy==2.0.0,github.com/kivymd/KivyMD/archive/m..., sdl2_ttf==2.0.15,pillow

If you have tried this and still gotten the error, could you please provide the logs.
I'm pretty sure that adding pillow to the requirements was supposed to work.

Thread Thread
 
pythonkivyuser profile image
PythonKivyuser

I've tried running buildozer with your mentioned requirements and it worked finally. Thank you very much for taking your time and for your help. 🥳
I'm so happy that the "compilation" is working.

A new small problem accoured with a more advanced kivy code. The code runs at the PC but not on my phone. [ TypeError: 'int' object is not iterable ]

Is this a typical error? And is the KivyMD/Python code the problem?

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze

Awesome, I'm glad that worked.

I think the TypeError is probably a bug in your code. It means somewhere your code is trying to iterate over an int object. Try to look through your code for possible causes if the log is not returning the line causing the error.

Collapse
 
jhosuaromero30 profile image
jhosua romero

Hey, could you help me with my app?, I've been trying several ways in builderdozer and I still can't get the app to work, it closes when I open it, I use kivymd, pillow in the requirements.

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hello.
Are you developing the application described in this article or another?

Collapse
 
jhosuaromero30 profile image
jhosua romero

Hey, could you help me with my app?, I've been trying several ways in builderdozer and I still can't get the app to work, it closes when I open it, I use kivymd, pillow in the requirements.

Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Are there no errors appearing in the log?

Collapse
 
islamimtiaz profile image
IslamImtiaz

Greetings, I am currently attempting to transform my python code into an APK, however, after converting it and installing the file, it unfortunately crashes right after the kivy logo appears. I am using google collab to convert my code into APK.
Spec file
Image description

main.py

from kivymd.uix.screen import MDScreen
from kivymd.app import MDApp
from kivy.uix.image import Image
from kivymd.uix.button import MDFillRoundFlatButton
from kivymd.uix.textfield import MDTextField
from kivymd.uix.label import MDLabel
from kivymd.uix.toolbar import MDTopAppBar
from kivymd.uix.dialog import MDDialog

class ConverterApp(MDApp):

    def flip(self):
        if self.state == 0:
            self.state = 1
            self.toolbar.title = 'CGPA calculator'
            self.GP.hint_text = 'Enter your current GPA'
            self.CH.hint_text = 'Enter your previous GPA'
        else:
            self.state = 0
            self.toolbar.title = 'GPA calculator'
            self.GP.hint_text = 'Enter your GP'
            self.CH.hint_text = 'Enter subject CH'

    def gpa(self, obj):
        gp_text = self.GP.text.strip()
        ch_text = self.CH.text.strip()
        # Check if GP and CH fields are not empty
        if not gp_text or not ch_text:
            # Show an error when GP and CH fields are empty
            dialog = MDDialog(title='Error',text='Both GP and CH fields are required',size_hint=(0.7, 1))
            dialog.open()
            return
        # Check if entered data is valid
        try:
            gp_values = [float(gp) for gp in gp_text.split(',')]
            ch_values = [float(ch) for ch in ch_text.split(',')]
        except ValueError:
            # Show an error when non-numeric value is entered
            dialog = MDDialog(title='Error',text='Invalid input! Please enter comma-separated numbers only',size_hint=(0.7, 1))
            dialog.open()
            return
        # Calculate GPA or CGPA
        if self.state == 0:
            x = sum(gp_values)
            y = sum(ch_values)
            if y == 0:
                # Show an error
                dialog = MDDialog(title='Error',text='Zero division error',size_hint=(0.7, 1))
                dialog.open()
            else:    
                c = x / y
                self.label1.text = str(c)
                self.label.text = "Your GPA is: "
        else:
            x = sum(gp_values)
            len1 = len(gp_values)
            y = sum(ch_values)
            len2 = len(ch_values)
            b = len1 + len2
            z = (x + y) / b
            self.label1.text = str(z)
            self.label.text = 'Your CGPA is: '

    def build(self):
        self.state = 0
        screen = MDScreen()
        # Top toolbar
        self.toolbar = MDTopAppBar(title="GPA Calculator")
        self.toolbar.pos_hint = {'top':1}
        self.toolbar.right_action_items = [['rotate-3d-variant', lambda x: self.flip()]]
        screen.add_widget(self.toolbar)
        # Logo
        screen.add_widget(Image(source ='gpr.png',size_hint=(0.8,1),pos_hint ={'center_x':0.5,'center_y':0.7}))
        # Collect user input
        self.GP = MDTextField(hint_text='Enter your GP',halign='center',size_hint=(0.8,1),pos_hint ={'center_x':0.5,'center_y':0.48},font_size=22)
        screen.add_widget(self.GP)
        self.CH = MDTextField(hint_text='Enter your CH',halign='center',size_hint=(0.8,1),pos_hint ={'center_x':0.5,'center_y':0.4},font_size=22)
        screen.add_widget(self.CH)
        # Secondary + Primary Label
        self.label = MDLabel(halign='center',pos_hint ={'center_x':0.5,'center_y':0.32},theme_text_color='Secondary')
        self.label1 = MDLabel(halign='center',pos_hint ={'center_x':0.5,'center_y':0.28},theme_text_color='Primary',font_style='H5')
        screen.add_widget(self.label)
        screen.add_widget(self.label1)
        # Convert Button
        self.button= MDFillRoundFlatButton(text='Result',font_size='17',pos_hint ={'center_x':0.5,'center_y':0.15})
        self.button.bind(on_press = self.gpa)
        screen.add_widget(self.button)

        return screen

ConverterApp().run()
Enter fullscreen mode Exit fullscreen mode

please help me bro

Collapse
 
vjrdnti profile image
vjrdnti

hello, im making an app with pandas, kivymd. colab just doesnt work, on research i found i have to edit spec file to p4a.branch=develop but it still doesnt work, i did put kivymd and pandas in requirements with their versions, please help i have put a lot of efforts in this app and its frustrating to see it crash after presplash

Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hi.

Did you try to locate the error with adb logcat?

Collapse
 
jaumeebusquets profile image
Jaumee
Comment hidden by post author
Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hello.

Honestly I have no idea where the problem could be. I haven't worked with tello before but I'm probably guessing it could be something to do with cv2 or how the video is being loaded with get_frame_read. Check out this link which has a demo on using opencv with kivy, it might help.

Collapse
 
fidelio1 profile image
fidelio1

Hi. I have an issue where after building app (kivy,kivymd), it didn't work. I looked up for the logcat and noticed that there is an issue with kivymd2.0.1.dev0
it doesnt recognize MDFAButton. This is declared requirements:
requirements = python3,pynmea2,kivy_garden==0.1.5,kivy==2.3.0,kivymd==2.0.1.dev0
And another question is how to make a permission for apk app, for socket comm, gps (location) ?

Collapse
 
alexndegwa profile image
Alexndegwa

ModuleNotFoundError: No module named '_ctypes'
The above error keeps getting raised after following the steps you have outlined. Kindly assist

Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hey

I've updated the article. I believe one of the required packages was missing. Try to run your program with the latest notebook.

Collapse
 
alexndegwa profile image
Alexndegwa

Thank you! This worked out for me.

Collapse
 
alexndegwa profile image
Alexndegwa • Edited

Am also using pyrebase in my app. Is there a way to know all the dependencies required?

Thread Thread
 
ngonidzashe profile image
Ngonidzashe Nzenze

To use pyrebase in your application, you just need to add it to the requirements in the spec file. When you run buildozer, it will automatically download any dependencies that pyrebase needs.

However, if you encounter an error with pyrebase, it could be because it's a Python package that requires compilation, and there is no specific support for it in Python-for-android. In such cases, the package won't work as expected.

If you want to make pyrebase work with Python-for-android, you can contribute to the Python-for-android project by creating a recipe specifically for pyrebase. By creating a recipe, you provide the necessary instructions for Python-for-android to properly compile and include pyrebase in the final application package.

Thread Thread
 
alexndegwa profile image
Alexndegwa

Okay

Collapse
 
airan_250 profile image
Airan250

app open and close direct
I/python (32368): [Clang 12.0.8 (android.googlesource.com/toolchain... c935d99d
I/python (32368): [INFO ] [Python ] Interpreter at ""
I/python (32368): [INFO ] [KivyMD ] 1.1.0.dev0, git-Unknown, 2022-09-24 (installed at "/data/data/org.test.eiranstudio/files/app/python_bundle/site-packages/kivymd/init.pyc")
I/python (32368): [INFO ] [Factory ] 186 symbols loaded
I/python (32368): Traceback (most recent call last):
I/python (32368): File "/content/.buildozer/android/app/main.py", line 1, in
I/python (32368): File "/content/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/EiranStudio/armeabi-v7a/kivymd/
init.py", line 66, in
I/python (32368): File "/content/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/EiranStudio/armeabi-v7a/kivymd/font_definitions.py", line 10, in
I/python (32368): File "/content/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/EiranStudio/armeabi-v7a/kivy/core/text/
init.py", line 85, in
I/python (32368): File "/content/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/python-installs/EiranStudio/armeabi-v7a/kivy/graphics/
init_.py", line 89, in
I/python (32368): ModuleNotFoundError: No module named 'kivy.graphics.instructions'

Collapse
 
grimycan profile image
Vadim

I managed to create an apk using Coollaboratory and it works, but the Android device cannot install subsequent versions to the previous one, even if nothing changes in the code. How can I organize the apk update on the device?

Collapse
 
abren profile image
abren

Hi, I get this error: No module named 'kivy.graphics.instructions' Thank you for any suggestions

Collapse
 
aqulline profile image
aqulline

did you manage to solve the problem

Collapse
 
ngonidzashe profile image
Ngonidzashe Nzenze

Hello,

You can try changing the version of kivy in the buildozer spec file from kivy==2.0.0 to kivy==2.1.0

Collapse
 
subalmurugan profile image
subal

When i try with my own main.py and main.kv and while getting apk then your application is getting...??????

Collapse
 
kattaramana profile image
kattaRamana

Pandas does not work

Some comments have been hidden by the post's author - find out more