Developing Python based Android Apps Using Kivy

1
64578

Android with KivY

Kivy is a cross-platform Python library, which can be used for the rapid development of apps that make use of innovative interfaces. This tutorial describes the installation of Kivy on various platforms and gives the code for building a few simple apps.

Nowadays, every business organisation strives to leverage its services with mobile apps. This permits wide coverage as well as global accessibility to the business services being offered. A number of software frameworks and libraries are distributed under different licences including proprietary, free and open source software. These software frameworks provide easy-to-use APIs (application programming interface) to the programmers, so that rapid and effective apps can be developed. With the use of mobile apps escalating each day, there is huge scope in the technology market to develop new apps for multiple uses and devices.

This area is being explored by the research and development teams of leading software development companies because of the dynamic way in which new products and services are rapidly being developed in the global market.

Fig 1
Figure 1: Official page of Kivy

Kivy — a Python based cross-platform framework
Kivy is a powerful library based on Python for the development of mobile apps including the natural user interface (NUI). It is one of the effective cross-platform libraries that can run on iOS, Android, Raspberry Pi, Linux, Windows, MacOS X with the distribution protocol under free and open source software. It has been developed and is distributed by the Kivy organisation.
The features and elements of Kivy include:

  • Support for multiple inputs including the mouse, TUIO (Tangible User Interface Objects), the keyboard and multi-touch
  • Powerful APIs for most mobile devices
  • One application for multiple operating systems
  • Support for networking protocols and remote login
  • Support for a lot of widgets with multi-touch KV to design custom widgets

Installation and configuration of Kivy
Kivy can be installed on any operating system without any compatibility issues.
It can be downloaded from http://kivy.org/#download, and once this is done, the installation instructions are specified on the download page itself. For R&D purposes, the development version of Kivy for the latest releases and additions in the framework can be downloaded from GitHub using the following command:

$ git clone http://github.com/kivy/kivy

Classically, the development version of any software is used by programmers and testing engineers so that the latest release and updates can be analysed in real-time.

Installation of Kivy on Linux (Ubuntu/Debian based)
First, add the PPAs (Personal Package Archives)
as follows:

stable builds: $ sudo add-apt-repository ppa:kivy-team/kivy
nightly builds: $ sudo add-apt-repository ppa:kivy-team/kivy-daily

Next, update the package list using a package manager. Now, install Kivy as follows.
Python2 Compatible version:

$ sudo apt-get install python-kivy

Python3 Compatible version:

$ sudo apt-get install python3-kivy

The pre-installed kivy examples can be installed using the following instruction on the command line:

$ sudo apt-get install kivy-examples

Installation on Android
Being a Python framework, Kivy can be installed on any Android device in the same way as on a classical desktop machine and there’s nothing special to be done. Kivy applications can be compiled to standard Android based APKs like a Java app on the device.

Fig 2
Figure 2: Download page of Kivy

Installation on Raspberry Pi
Kivy can be installed on Raspberry Pi manually. Alternatively, after downloading, KivyPie can be booted on the Raspberry Pi itself.

Manual installation on Raspbian
To install the dependencies, use the following code:

$ sudo apt-get update
$ sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev \
gstreamer1.0-plugins-{bad,base,good,ugly} \
python-setuptools libgstreamer1.0-dev git-core \
pkg-config libgl1-mesa-dev libgles2-mesa-dev \
gstreamer1.0-{omx,alsa} python-dev cython

Install Kivy globally on your system, as follows:

$ sudo pip install git+https://github.com/kivy/kivy.git@master

Or build and use Kivy inplace (best for development), using the following code:

$ git clone https://github.com/kivy/kivy
$ cd kivy
$ make
$ echo “export MY-PYTHON-PATH=$(pwd):\$ MY-PYTHON-PATH “ >> ~/.profile
source ~/.profile

Configuring Kivy
Config.ini is the configuration file of Kivy, which is in the standard INI format. The path of the configuration file config.ini is managed by the environment variable KIVY_HOME. On the desktop, this defaults to:
<User-Home-Directory>/.kivy/config.ini
If the user name is ‘MyUser’, the file location will be:
Windows: C:\Users\ MyUser\.kivy\config.ini
OS X: /Users/ MyUser /.kivy/config.ini
Linux: /home/ MyUser /.kivy/config.ini
Android: <My-Android-App-Path>/.kivy/config.ini
If the app name is ‘my.kivy.app’, the file will be in the following location:
/data/data/ my.kivy.app /files/.kivy/config.ini
iOS: < User-Home-Directory >/Documents/.kivy/config.ini

Architecture of Kivy
Kivy has a number of building blocks that work together for the development and execution of the mobile apps.

Creating a basic app
Use the basic app in Kivy to print a message on the mobile device. This is code that can be implemented by beginners:

from kivy.app import App
from kivy.uix.button import Button
class GoodMorning(App):
def build(self):
return Button(text=’Good Morning to All of You’)
GoodMorning ().run()

Fig 3
Figure 3: Kivy architecture [Source: Official Kivy documentation from Kivy Docs, URL- kivy.org]
Creating app code using Python Kivy – accessing the Web camera
This example demonstrates using the camera of the mobile device. The code will generate the window with the button ‘Play’ so that the camera can be moved to either the ON or OFF state.

from kivy.app import App
from kivy.lang import Builder
kv = ‘’’
BoxLayout:
orientation: ‘vertical’
Camera:
id: camera
resolution: (640, 480)
play: False
ToggleButton:
text: ‘Play’
on_press: camera.play = not camera.play
size_hint_y: None
height: ‘48dp’
class MobileCamera(App):
def build(self):
return Builder.load_string(kv)
MobileCamera ().run()

Creating code for a weather app
In this example, the code makes use of inheritance to create the subclass of WeatherApp.

from kivy.app import App
class MyWeatherAnalysisApp(App):
pass
if __name__ == ‘__main__’:
MyWeatherAnalysisApp ().run()
Creating a timer clock in Kivy

In this code, a very simple timer clock app is created. This is similar to the clock that is in-built with Android, iOS and other mobile platforms.

from kivy.app import App 
class MyClockTimerApp(App): 
pass 
if __name__ == ‘__main__’: 
MyClockTimerApp ().run() 
# File: clock.kv 
BoxLayout: 
orientation: ‘vertical’ 
Label: 
text: ‘00:00:00’
Figure4
Figure 4: Timer clock app in Kivy

Kivy Garden
Garden is the specialised project, where all the add-ons and modules of Kivy can be centralised on a common point (http://kivy-garden.github.io/). Garden packages of Kivy are on the centralised GitHub repository.
Kivy Garden can be installed using the following command:

$ pip install kivy-garden

The Garden module does not have any package at the initial level. Every package or module is required to be downloaded at runtime using Pip, as follows:

# Installation of kivy-garden package
$ garden install <PackageName>

# Upgrading any package of garden
$ garden install --upgrade <PackageName>

# Uninstalling any garden package
$ garden uninstall <PackageName>

# Listing of all the garden packages already installed
$ garden list

# Searching new packages
$ garden search

# Searching packages containing “media”
$ garden search media

# Getting the Help
$ garden --help

Scope for R&D
Research scholars and engineers can perform a number of experiments for their research assignments and dissertations using multiple mobile platforms. One of the classic examples is the pragmatic analysis of the performance of different mobile frameworks. In the course of doing this work, the same application can be developed on different platforms including Android, Kivy and Codename One. After execution of the app, the different parameters including execution time, delay and related cost factors can be compared with graphs and tables. Using this approach, the actual difference between the performance levels of multiple platforms can be evaluated with empirical results.

Previous articleH2O.ai: Learn to Use the KMeans Algorithm
Next articleCreating Graphs Using PHP
The author is the managing director of Magma Research and Consultancy Pvt Ltd, Ambala Cantonment, Haryana. He has 16 years experience in teaching, in industry and in research. He is a projects contributor for the Web-based source code repository SourceForge.net. He is associated with various central, state and deemed universities in India as a research guide and consultant. He is also an author and consultant reviewer/member of advisory panels for various journals, magazines and periodicals. The author can be reached at kumargaurav.in@gmail.com.

1 COMMENT

  1. Dear Author, the Indentations for the code is all messed up. Could you please update the code with correct Indentations?

LEAVE A REPLY

Please enter your comment!
Please enter your name here