Mobile application in Python with kivy / buildozer. Lecture in Yandex
Not the fact that you need to write a serious application in Python. But to quickly assemble a working service in order to “sell” it to a customer — why not? Python is universal, and the experience of creating mobile software in this language can be useful. Vladislav Shashkov from Sberbank talked about how to develop development using the kivy framework.
- Good day. My name is Vladislav Shashkov, I work at Sberbank and in fact I am a product developer, not a developer. This is exactly what my report can be interesting for, because it will graphically show that making a mobile application in Python is quite simple.
Kivy is still “Kivi”, because it is figuratively depicted on the covers of books in the form of fruit, and the pronunciation of “Qiwi” will be closer in Russian. The framework has been developing since 2011. In fact, this is a graphical UI-library for creating cross-platform applications, not only on mobile platforms. Its feature is KV, it is a markup language. If you figuratively try to describe what the KV language is, it turns out that it would look like HTML if it were written in Python.
We will also talk about the library KivyMD, which is a set of widgets in the style of Material Design. KivyMD allows you to create a user-friendly Google interface with which you can work on user experience.
')
Here the video shows how to scroll through all the widgets that are in the library. As you can see, this is a fairly rich set of items. These are bookmarks, buttons, progress bars and pop-ups. In principle, there is everything you need to realize the client experience you need. If you need to build an assembly on iOS, then it will look the same. Same Material Design. In principle, such an application can be published in the App Store, although it will be similar to Google.
Kivy without KivyMD has a very austere design. Its even customers do not show. Similar to Ubunt simple buttons, simple dialogs that are not adapted for mobile experience, for mobile users. And KivyMD great, everything is beautifully done.
There is bad news. Executable application packages are built by a special utility, buildozer. And the bad news is that it is not easy to start using the first build with it. Like, probably, with any open source software, in order for everything to work out, in order to finally reach the goal and get the APK, you need to go through a certain “path” without turning around.
This slide describes in detail what you need to do, how to walk along the “path” and not to do too much. Do not try to update the kivy itself or any other libraries in the image. All that is needed is indicated on the slide, the rest will tighten itself and will work.
Then talk a little about the structure of the project Kivy. The slides contain QR codes where GitHub links are encoded so that you can read them now and directly trace the examples I’m going to talk about. ( The first link - Ed.)
The screen shows the buildozer spec file, this is the project specification. The most important thing is: there is a line that lists the requirements, that is, the libraries that are needed for the assembly, and you need to specify Kivy, KivyMD in it, there is an entry there, and it is also available on GitHub.
The important section, it is commented out, concerns the Androids NDK, SDK, it is not necessary to touch it. Default values will work.
A little about UI and UX. There are two concepts in the KV markup language: widgets and layouts. Widgets - visible elements, buttons, input fields and the like. Widgets receive events and can process them. And layouts are objects that allow you to position widgets, there are several types of them to build widgets in a row or vice versa, somehow arbitrarily position. In general, this approach is standard for visual UI, UX, and in other libraries, it is also used.
Here is an example of the KV language ( link - ed.). As we can see, the libraries are first imported behind bars, then in angle brackets ... It is wrong to say “root”, just the initial layout to which the class will be associated in the code. One to one names must match. Thus, the markup is compared to the class in the code. Further through the indents go the embedded widgets, inside the widgets, again indent go the property and events, they are in the same list. Here is a sample code.
At the top of the class is the layout class. Next comes the application class, and below there are two lines for loading the markup constant, and running the application. I know that the audience is probably not very familiar with mobile development, so I’ll tell you about the next steps on what exactly needs to be done on the phone in order to launch and debug your APK.
First of all, you need to enable the developer on the phone. It is quite cleverly included. Usually this is the secret sequence of pressing the menu items in the phone settings mode. The presentation shows one of the most common sequences, but it may not work on your phone. After completing this sequence of actions, a mode will appear for the developer, and you can already install the APK, debug, and continue to act.
Buildozer runs in a virtual machine, the virtual machine needs to be updated so that it is fresher. After that, integration with USB will appear, then you can connect the phone to the virtual machine.
A sign that you have connected - the phone will ask permission to debug. The project directory is created in the / BUILD folder. To build a project, you need to run the buildozer android debug command in the project directory. Installing the APK on the phone is performed by the utility from Android Studio, which is already in the image, with the adb install command.
And the phrase success says that while everything is fine, the download is completed on the phone.
Debugging code is as follows. It is necessary to start collecting debug information with the adb logcat command so that the collector works in parallel with the application. After the first launch, your application “with a high probability” will fall, after which you can stop debugging and go read the log file.
How to search for an error message? By keyword python. The most recent entry with the python keyword will usually tell you what went wrong.
About what can be done on the phone. The mobile platform allows you to do not just buttons that go to the API, but work with a wide variety of gadget sensors. To work with mobile peripherals there is a standard library plyer. It is platform-independent, that is, everything that is written on it will be re-compiled from Android to iOS. The slide lists all available options. There both GPS, and battery, and camera ... But with a plyer camera, you can only take a static photo.
And the following example expands this opportunity. Link to GitHub , an example that allows you to receive a video stream in an application. I had a business problem to recognize the QR code from the image. Also, this example can work with video recognition, augmented reality, etc.
Also, kivy has space for custom addons - Garden. Anyone can make widgets and such. A useful widget that I used is to work with maps. It is active, you can scale, twist the map. Here is an example of how to connect it. There are many other widgets in the Garden.
When I was working on my project, I conducted a corridor testing with my colleagues in order to collect feedback on the use of the application. And I received such wonderful objections as “I will not put the left APK”, “mom does not allow”, “I just bought a phone”. In order to continue testing customer experience in essence, I had to go the way of creating a mobile application to the end, namely, to make a release on Google Play. It is described on the slide. The main points are that it is necessary to tinker with the keys, and publication on Google Play is not free. To register as a developer, you need to make $ 25. And this is still very democratic, because Apple asks $ 99.
This slide contains a link to the sample application, you can see my project. This is still an alpha prototype, so please do not judge strictly if the application will fall. The link to the current documentation is fresh, it seems to me that it is good. There is a community, a group of VKontakte , its admin on Habré periodically publishes articles.