Kivy is a multi-touch python framework which works on Windows, OS X, Linux, iOS and Android. Getting Kivy to run on Android, however, is a difficult task because the code needs to be first packaged in an App. Here is a sure-fire way to make an Android App out of your python code.
On the Kivy website there are instructions on how to create an android package, but those instructions don’t work in Windows or OS X, and they skip lots of steps.
My solution is simple, get everything working in a virtual machine and do the packaging there. For the record, the Kivy team did this first but:
(1) they used the Ubuntu operating system in the virtual machine which was very slow when I tried to use it and
(2) what I had to do to package my code was not made clear to me.
I did my best to create a virtual machine image which is as fast and as straightforward to use as possible.
LET’S GET STARTED
The first thing you will need is my linux virtual disk image. It’s huge but it’s worth the wait. The other thing you will need to do is to download Oracle VM Virtualbox and install it on your computer. I won’t be giving detailed instructions on how to do this because it is easy to find out. Just get it running.
HERE IS THE IMPORTANT PART: Once you have Virtualbox running create a new VM using my vdi file as the hard disk. The only settings you need to have right is the operating system must be Linux and the version must be Ubuntu 32 bit. If you want guidance you can check out this youtube video. The video shows someone installing Virtualbox and setting up a VM using an existing vdi, which is exactly what you want to do.
Once the VM is booted up log in. THE USERNAME AND PASSWORD IS osboxes.org
By the way, thanks osboxes.org
Once logged in you should see the desktop:I put all the instructions to build the APK on the desktop wallpaper but I will go over them here with lots of screenshots.
1. Launch the Command Prompt (it’s actually called ROXTerm) by clicking on the icon.
2. In the Command Prompt type the following (or Press the up key until you see it):
Once you have that typed in, press the [enter] key. This just takes you into the ‘kivycode’ directory. You have to be in this directory for the command in step 4 to work.
3. In the Command Prompt type the following (or Press the up key until you see it):
cp ~/Desktop/buildozer.spec ~Desktop/kivycode
Once you have that typed in, press the [enter] key. This just copies the buildozer.spec file into the ‘kivycode’ directory for use. If you prefer you can copy the file using the GUI.
4. In the Command Prompt type the following (or Press the up key until you see it):
buildozer android debug
Once you have that typed in, press the [enter] key. This step actually makes the APK file. If everything went well, your APK should be created on the desktop. Get the APK onto your android device, install and launch!
That’s it! You just built your first Android Installer Package!
NOW LET’S BUILD YOUR CODE!
Sure you just built an APK but the APK isn’t much use on the desktop of a VM. Furthermore it’s not your code. Let’s sort that out.
Firstly we have to get files in and out of the VM. My chosen solution was to use an external drive. I just plugged one in and copied whatever I need onto it. To then move the USB from the real computer and the VM or vise versa, click ‘Devices > USBDevices’ in the virtualbox menu and click on the drive.
The drive then appears in the VM
If you don’t have an external drive you could just launch a browser and upload files to a service like Google Drive from within the VM.
Now VirtualBox has a system by which you can set up a shared folder which can be seen by both the OS in VM and the Host OS but I couldn’t get it to work. If you can get it to work post me a comment telling me how. I’d appreciate it.
Now that we can get files in and out of the VM we can compile whatever code you have, once you know where to put it. Put it in the kivycode folder on the desktop. To package your program just replace everything in the kivycode folder with your code. Some things like Videos need special packaging settings and you should use the kivy framework but, for the most part, python 2.7 code would work just fine.
The last thing you want to do before you compile your code is to copy the buildozer.spec file from the desktop into the kivycode folder and edit the copy as necessary. The options are well described and there is more information about it online
Maybe you would like to edit the loading.png and icon.png but that’s all there is to it. Build your python code into an app, collect the APK from the VM’s desktop and install it on your device.