[TUTORIAL] How to make kivy with buildozer and optionally kivent to work with python3 in virtualenv

999 views
Skip to first unread message

Kaido Ristmägi

unread,
May 10, 2018, 8:48:00 AM5/10/18
to kivy-...@googlegroups.com
Ok so i spent some time to help out anyone who struggles to get this to work.
Buildozer fixes thanks to:
kowsar89
https://github.com/kowsar89/buildozer/commit/4c1a2e3a6b15c27c15fe81b1b22b02cc14d6b051
cdyangbo
https://github.com/kivy/buildozer/issues/613

1. Tested with:
Xubuntu LTS release: 16.04, Xenial Xerus (64bit)
Ubuntu LTS release: 16.04, Xenial Xerus (64bit)
 
2. Python 3.5.2
3. Works under virtualenv
4. Preinstalled android sdk, crystax ndk, apache ant
5. Cython 0.25.2
6. Kivy 1.10.0 with sdl2
7. Buildozer 0.35.dev0

Optional
8. Cymunk
9. Kivent

Im using Pycharm Community Edition in this tutorial (im using it anyway) to create virtualenv.
You can do this whatever way you are comfortable with.

Download xubuntu LTS release: 16.04, Xenial Xerus from https://xubuntu.org/download/
Install it in your virtualbox

# Sidenotes
YOUR_USERNAME = username you choosed when you installed xubuntu

# Update system
sudo apt-get update
sudo apt-get upgrade -y


# Install guest additions! Click devices on top of screen and choose Insert Guest Additions CD image
# Open up terminal and type (assuming that guest additions are "VBox_GAs_5.2.8")
cd /media/YOUR_USERNAME/VBox_GAs_5.2.8
sudo ./VBoxLinuxAdditions.run


RESTART your VM

# You can skip this part if you dont need to access shared folders.
# Add user to vboxsf group to access shared folders
sudo adduser YOUR_USERNAME vboxsf

Log out and back in
Install Pycharm Community Edition (easiest way through software center)
Create an project in PyCharm with New environment using Virtualenv and Base interpreter as /usr/bin/python3.5

# Install kivy deps
sudo apt-get install -y \
    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 \

  
Open up terminal!

# Sidenotes
YOUR_PROJECT_FOLDER = your project name you chose when created new project in PyCharm

# Activate virtualenv
source ~/PycharmProjects/YOUR_PROJECT_FOLDER/venv/bin/activate

# Upgrade pip and setuptools
pip install --upgrade pip virtualenv setuptools

# Install cython
pip install cython==0.25.2

# Install kivy
pip install kivy==1.10.0

# (optional) Install cymunk
git clone https://github.com/kivy/cymunk
cd ~/cymunk
python3 setup.py install


# (optional) Install kivent
git clone https://github.com/kivy/kivent
cd ~/kivent/modules/core
python3 setup.py install
cd ~/kivent/modules/cymunk
python3 setup.py install
cd ~/kivent/modules/particles
python3 setup.py install
cd ~/kivent/modules/maps
python3 setup.py install
cd ~/kivent/modules/projectiles
python3 setup.py install


Now to the Buildozer!

# Needed by buildozer (install and upgrade colorama, sh, appdirs, jinja2, six)
pip install --upgrade colorama sh appdirs jinja2 six

# Install openjdk
sudo apt-get install -y openjdk-8-jdk

# Install 32bit libraries for buildozer
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install -y autoconf automake lib32z1 libtool ccache libncurses5:i386 libstdc++6:i386 libgtk2.0-0:i386 libpangox-1.0-0:i386 libpangoxft-1.0-0:i386 libidn11:i386 unzip zlib1g-dev zlib1g:i386 pkg-config


# Install buildozer
git clone https://github.com/kivy/buildozer
cd ~/buildozer
python3 setup.py install


# Apply buildozer fixes!
# 1 and 2 will make your .apk file to be in /home/YOUR_USERNAME/PycharmProjects/YOUR_PROJECT_FOLDERYOUR_PROJECT_FOLDER/bin/
# 3 will make buildozer to work in virtualenv
# Open android.py file found in /home/YOUR_USERNAME/PycharmProjects/YOUR_PROJECT_FOLDER/venv/lib64/python3.5/site-packages/buildozer-0.35.dev0-py3.5.egg/buildozer/targets/
# Edit it as follows (line numbers may not be 100% accurate couple lines up or downwards)

1. At very top insert:
from distutils.version import LooseVersion


2. Line 800 replace code between: "XXX found how the apk name is really built from the title" and "if is_gradle_build:"
        __sdk_dir = self.android_sdk_dir
        build_tools_versions
= os.listdir(join(__sdk_dir, 'build-tools'))
        build_tools_versions
= sorted(build_tools_versions, key=LooseVersion)
        build_tools_version
= build_tools_versions[-1]
        gradle_files
= ["build.gradle", "gradle", "gradlew"]
        is_gradle_build
= any((exists(join(dist_dir, x)) for x in gradle_files)) and build_tools_version >= '25.0'


3. Line 532 replace code between: "pip_deps.append('"{}"'.format(dep))" and "def get_available_packages(self):"
        if(hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)):
            cmd
('pip install -q {}'.format(" ".join(pip_deps)))
       
else:
            cmd
('pip install -q --user {}'.format(" ".join(pip_deps)))


# Missing GL/gl.h when building with buildozer
# Copy folder GL from /usr/include/ to /home/YOUR_USERNAME/PycharmProjects/YOUR_PROJECT_FOLDER/venv/include/
# Or use command for that
cp -r /usr/include/GL/ ~/PycharmProjects/YOUR_PROJECT_FOLDER/venv/include/

# Next part for android-sdk, crystax ndk and apache ant.
Everything will be downloaded into /home/YOUR_USERNAME/

# Download android-sdk r25.2.5 and unzip it
wget http://dl-ssl.google.com/android/repository/tools_r25.2.5-linux.zip
unzip ~/tools_r25.2.5-linux.zip -d ~/android-sdk


# Install necessary tools (notice that yes in front of command auto accepts licenses there may be delay so it seems like your stuck with license waiting for answer just wait your not really stuck)
cd ~/android-sdk/tools/bin
yes | ./sdkmanager "build-tools;27.0.3"
yes | ./sdkmanager "platforms;android-19"
yes | ./sdkmanager "platform-tools"


# Download CrystaX NDK 10.3.2 (64bit) and extract it
wget https://www.crystax.net/download/crystax-ndk-10.3.2-linux-x86_64.tar.xz
tar -xJf ~/crystax-ndk-10.3.2-linux-x86_64.tar.xz


# Download apache ant 1.10.3 and extract it
wget http://mirror.linux-ia64.org/apache//ant/binaries/apache-ant-1.10.3-bin.tar.gz
tar -xvzf ~/apache-ant-1.10.3-bin.tar.gz


Basically you should be all set now
Remember this is virtualenv buildozer works only in this virtualenv this is so that you can keep your main python clean and basically untouched.

# To start building your app just do (in terminal):
source ~/PycharmProjects/YOUR_PROJECT_FOLDER/venv/bin/activate
cd ~/PycharmProjects/YOUR_PROJECT_FOLDER
buildozer android init


Don't close terminal meanwhile!

# Now its time to edit your buildozer.spec file in /home/YOUR_USERNAME/PycharmProjects/YOUR_PROJECT_FOLDER/

# Notice that here are only parts that you should change. There are more optional options like app name, version, an so on.


# (list) List of directory to exclude (let empty to not exclude anything)
source
.exclude_dirs = venv,bin

# comma seperated e.g. requirements = sqlite3,kivy
requirements
= python3crystax,kivy

# (int) Android API to use
android
.api = 19

# (int) Minimum API required
adroid
.minapi = 13

# (int) Android SDK version to use
android
.sdk = 23

# (str) Android NDK version to use
android
.ndk = 10.3.2

# (str) Android NDK directory (if empty, it will be automatically downloaded.)
android
.ndk_path = ~/crystax-ndk-10.3.2

# (str) Android SDK directory (if empty, it will be automatically downloaded.)
android.sdk_path = ~/
android-sdk

# (str) ANT directory (if empty, it will be automatically downloaded.)
android
.ant_path = ~/apache-ant-1.10.3

This is optional but useful if you want to see whats going on (any errors if it fails and so on)
# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level
= 2


# Well after changing and saving it you should run (in terminal):
buildozer android debug

# Additionally you can build and send it directly to your device and run it with
buildozer android debug deploy run

# If everything goes well you should have your built .apk in /home/YOUR_USERNAME/PycharmProjects/YOUR_PROJECT_FOLDER/bin/

All the best and happy coding :)

Reply all
Reply to author
Forward
0 new messages