Creating and Distributing a Kivy Python App (.apk) for Android using Buildozer on Windows and Linux

Posted by


Building and deploying a Kivy Python app to Android using Buildozer is a relatively straightforward process once you have all the necessary tools installed. In this tutorial, we will walk you through the steps to build and deploy a Kivy Python app as an .apk file on both Windows and Linux operating systems.

Prerequisites:
Before we begin, make sure you have the following prerequisites installed on your system:

  1. Python (version 3.6 or later recommended)
  2. Pip (Python package installer)
  3. Virtualenv (Python virtual environment)
  4. Buildozer (Python package for packaging and deploying Python apps)
  5. Android SDK
  6. Java Development Kit (JDK)

Step 1: Install Buildozer
First, you need to install Buildozer on your system. You can do this by running the following pip command:

pip install buildozer

Step 2: Create a Kivy Python App
Next, create a Kivy Python app that you want to build and deploy to Android. You can write your app code in a file named main.py. Here is a simple Kivy Python app code that you can use as an example:

from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self):
        return Button(text='Hello, World!')

TestApp().run()

Step 3: Create a Buildozer.spec File
Create a Buildozer.spec file in the same directory as your main.py file. This file contains configuration settings for Buildozer. Here is an example of a Buildozer.spec file:

[app]

title = My Kivy App
package.name = mykivyapp
package.domain = org.test
source.dir = .

requirements = python3,kivy

orientation = portrait
fullscreen = 0

[buildozer]
log_level = 2
warn_on_root = 1

Step 4: Build and Deploy the App
Now, navigate to the directory where your main.py and Buildozer.spec files are located and run the following command to build the .apk file:

buildozer -v android debug

This command will start the build process and create an .apk file in the bin directory of your project directory.

Step 5: Deploy the App to Android Device
You can now deploy the built .apk file to your Android device for testing. Connect your Android device to your computer and run the following command to install the .apk file on your device:

buildozer android deploy run

This command will install the .apk file on your connected Android device and launch the app automatically.

Conclusion:
In this tutorial, we have shown you how to build and deploy a Kivy Python app as an .apk file to Android using Buildozer on both Windows and Linux operating systems. By following these steps, you can easily create and test your Kivy Python apps on Android devices. Happy coding!

0 0 votes
Article Rating

Leave a Reply

23 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@fullstacklab
26 days ago

Hey folks 🙂 You can find the most important commands in the description. I also want to mention this tutorial works perfectly fine for Ubuntu.

@leosngo391
26 days ago

Hi please help! Everything works well til command in Ubuntu: adb connect YOUR_IP:5555. the error message is: failed to connect to 'myIP:5555': Connection timed out. Even in cmd i got the same massage like your: restarting in TCP mode port: 5555. All your steps with setting phones I checked. Do you have for me some suggestion?

@postericast
26 days ago

Cython 3.0.11 is not compatible with buildozer, so instead of "sudo pip3 install –upgrade cython" I suggest "sudo pip3 install cython==0.29.37' to avoid possible compatibility errors.

@Mrsmith.
26 days ago

Its error how to fix it

# Check that aidl can be executed
# Search for Aidl
# Run '/home/kali/.buildozer/android/platform/android-sdk/build-tools/35.0.0/aidl'
# Cwd None
Traceback (most recent call last):
File "/usr/local/bin/buildozer", line 8, in <module>
sys.exit(main())
^^^^^^
File "/usr/local/lib/python3.11/dist-packages/buildozer/scripts/client.py", line 13, in main
Buildozer().run_command(sys.argv[1:])
File "/usr/local/lib/python3.11/dist-packages/buildozer/__init__.py", line 1024, in run_command
self.target.run_commands(args)
File "/usr/local/lib/python3.11/dist-packages/buildozer/target.py", line 93, in run_commands
func(args)
File "/usr/local/lib/python3.11/dist-packages/buildozer/target.py", line 103, in cmd_debug
self.buildozer.prepare_for_build()
File "/usr/local/lib/python3.11/dist-packages/buildozer/__init__.py", line 172, in prepare_for_build
self.target.install_platform()
File "/usr/local/lib/python3.11/dist-packages/buildozer/targets/android.py", line 616, in install_platform
self._install_android_packages()
File "/usr/local/lib/python3.11/dist-packages/buildozer/targets/android.py", line 567, in _install_android_packages
self._check_aidl(installed_v_build_tools)
File "/usr/local/lib/python3.11/dist-packages/buildozer/targets/android.py", line 592, in _check_aidl
_, _, returncode = self.buildozer.cmd(aidl_cmd,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/buildozer/__init__.py", line 289, in cmd
process = Popen(command, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/subprocess.py", line 1026, in _init_
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.11/subprocess.py", line 1955, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/kali/.buildozer/android/platform/android-sdk/build-tools/35.0.0/aidl'

┌──(kali㉿localhost)-[~/pacman]
└─$

@vinayak254
26 days ago

a fantastic video. hats off

@notmidhun2365
26 days ago

sorry if this may stupid so should i install all these required libraries into the cloned buildozer folder

@jaygreat4791
26 days ago

Hi! I am trying to build a simple program but kept on encountering this error. Any idea how tp fix this? Thanks!

[WARNING]: ERROR: /mnt/c/Users/Public/PYTHON/main/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/dists/myapp/gradlew failed!

No setup.py/pyproject.toml used, copying full private data into .apk.

Applying Java source code patches…

Applying patch: src/patches/SDLActivity.java.patch

# Command failed: ('/usr/bin/python3', '-m', 'pythonforandroid.toolchain', 'apk', '–bootstrap', 'sdl2', '–dist_name', 'myapp', '–name', 'My Application', '–version', '0.1', '–package', 'org.test.myapp', '–minsdk', '21', '–ndk-api', '21', '–private', '/mnt/c/Users/Public/PYTHON/main/.buildozer/android/app', '–android-entrypoint', 'org.kivy.android.PythonActivity', '–android-apptheme', '@android:style/Theme.NoTitleBar', '–orientation', 'portrait', '–window', '–enable-androidx', '–copy-libs', '–arch', 'arm64-v8a', '–arch', 'armeabi-v7a', '–color=always', '–storage-dir=/mnt/c/Users/Public/PYTHON/main/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a', '–ndk-api=21', '–ignore-setup-py', '–debug')

@hellocodeur
26 days ago

thank you very much. u save my life

@mertmehmetaraz3119
26 days ago

adb logcat | grep "python"

Worked for me probably cuz of different version of adb.

@DrStoCazzo
26 days ago

after finishing it fails with an error and
says, packaging has no attribute version…
what does this mean?

@beep615
26 days ago

linux aint installing

@Martin1jg
26 days ago

Excellent tutorial, I managed to make it work. I ran into a ton of problems but managed to google and use chatgpt for solutions. If you get a failure with gradle, it worked when i switched to java vers 17. Also upgrade to WSL2 to avoid error: C compiler cannot create executables.

@Tecknoc
26 days ago

Vielen Dank!!! Du bist ein life saver

@akhilavissomraju1245
26 days ago

How can i deploy my app if I'm using windows? is it necessary to use linux

@andadong4520
26 days ago

I dont understrand anything, why is there suddenly a .sepc and buildozer folder in the github project, last time these two folders did not exist

@polalonso4808
26 days ago

Nice tutorial! Just one little thing: in order to make this work, WSL needs to be upgraded from WSL1 to WSL2, otherwise you get 'C compiler cannot create executables' error

@drapek99
26 days ago

Thanks Bro 💪

@shoxruxsunnatov
26 days ago

I wish I could like this video twice.

@jitheshkuyyalil
26 days ago

Wonderful! Thank you so much!

@pmc56982
26 days ago

that worked well ! too bad we can't use buildozer directly from cmd and that WSL can't access USB easily. Before watching this video I tried to hack around with WSL by redirecting adb to the cmd one with a symbolic link. It failed miserably but connected through tcpip did the job in the end.

23
0
Would love your thoughts, please comment.x
()
x