
In this article, I would like to talk about how to create a full-fledged Python application for Android. No, this is not just another manual for creating a script for
sl4a , this is a manual for creating a full-fledged application with a UI, the ability to build apk and put it on the Android Market. At the same time, I would like to praise my first application on google.play, this is not hello world, but a useful application for photographers, albeit highly specialized.
I’ll start the story with the application itself and finish the stories about how it was done.
isortViewer
As a photographer, I have to spend a lot of time sorting and selecting photos. You don't always want to sit at a working computer, when you have a laptop, smartphone or tablet at hand, but copying tens of gigabytes of raw or jpeg files there, and then synchronizing with a working computer is a dubious pleasure. That is why I created the programs isortManager and isortViewer, which greatly facilitated my job of sorting and selecting photos. Now I can do the selection and sorting of photos lying on the
beach couch or missing in traffic :)
')
Copy-paste descriptions and a couple of screenshots:
isortViewer is a program for photographers that allows you to easily and quickly select and sort thousands of photos (raw or jpg) from photo shoots on your android smartphones and tablets. At the same time, there is no need to copy tens of gigabytes of jpg or raw files to the device. Just use the free computer program isortManager, which will save everything into one small project file:
1. Download and run isortManager from the official site ;
2. Create a project and add folders with photos. Supported formats: jpg and raw (cr2, nef, orf, etc.). As a result, you will have a small project file (approximately 150 MB for several thousand photos, be it jpg or raw format);
3. Copy the project file to your android device, open it in isortViewer. You can mark photos "for deletion", and also, put ratings from 1 to 5 stars;
4. After that, copy the project file back and apply the changes to the isortManager. In addition to deleting selected photos, you can copy or move marked photos. For example, photos from “5 stars” can be copied to the “masterpieces” folder, and “1 star” can be moved to the “trash” folder.


The application is absolutely free and available on
google.play , however donates are welcome.
Sources are available on the official website!
How was it done
Since
python is the best programming language in the world except python, I do not know anything, it was decided to write an application in python.
isortManager
With isortManager for PC, there were no problems, creating a GUI assembly under windows has long been run in and it works very simply: using dcraw to rip jpg thumbnails from raw files, resize from using PIL and put it into one file with a fairly simple structure. Were invented your vesoliped with a container for storing photos and metadata (full path to the photo, mark for deletion and rating mark), because, for example, zip or tar format cannot change one file in the archive (metadata), I need to repack all files. Files are simply written one after another in a row and in the block of metadata (the usual repr of the Python dictionary) the offset of the beginning of the files is saved at the end, plus the size of the block of metadata is written at the end of the file. The GUI is written on Tkinter (I love it for the speed of writing and for the small size of the final assembly), here’s the actual screenshot:

And yes, it all works on linux, and even faster than on windows)
isortViewer for android
In total, I found two ways to make the python application work as a full-fledged application on android, this is
pygame for android and the
kivy project.
Pygame is lower-level, all the UI would have to be drawn by hand, which would take a lot of time, so the kivy framework was chosen. So:
Kivy
This is a great framework for writing apps for windows, linux, MacOS, android and iOS. Multitouch is supported, UI will be opened via OpenGL, it means that hardware acceleration should work. You can see how widgets work by installing the
Kivy Showcase demo application. It is possible to use some platform-specific functions, for example, vibro or accelerometer, using the android module.
For development, I would recommend using linux, especially since apk is built on this OS.
Hello World looks like this:
import kivy kivy.require('1.0.6')
Moreover, this code will work on all the declared platforms. There is no need to test the application in the emulator. Just run the script for execution in your favorite IDE and see the result on the computer screen, without delays in compiling, running the emulator, etc. If you still want to see how it will look directly on the device, simply install
Kivy Launcher , copy the project files to the card memory and run. You can debug using adb logcat.
If you use the library android, which is not on the PC, but you want to run the application not only on android, use this design:
try: import android except ImportError: android=None ... if android: android.vibrate(0.05)
Building apk is quite simple and is described on
this page. After building the release, it’s enough to sign your application (I used
this manual) and put it into google play.
Pros kivy:- Rapid development for various platforms, with almost no code refinement.
- Wide selection of widgets available
- High speed work. All resource-intensive rendered in C modules. The python interpreter on android works natively.
- The framework includes many tools, such as animation, caching, etc.
- upd: Access to the camera, clipboard, microphone. You can write your video player in literally 20 lines .
Cons kivy:- Large apk file size. A project with 300 kb of resources (scripts, graphics) is collected in 7 MB apk. Although, I think there is an opportunity to optimize this.
- Inability (yet) to restore work after folding - the application closes
In my next article, I will talk in more detail about the process of writing an application from scratch, chewing every line of code.
I would like to immediately stop the holivars on the topic “for android only java, python is not needed”. I think it doesn’t matter what technologies are “under the hood”, the main thing is that the application be of high quality.
The scope of kivy can be huge. Now on my account there are several applications written under the order, where python with the kivy framework has shown itself from the good side. For example, in exactly one hour, an application was written for a network of service centers. The application works as a kiosk and is installed on cheap Chinese tablets that hang in the lobby. The client dials the order number, then the tablet connects to the server via WiFi and reports the status of the order.