All about the Python program for Android. A complete Python application for Android. Installing python on android

Narrated by Alexander Taylor, Kivy project developer

Lately it appears a large number of Python development resources for Android. The Kivy framework (and its offshoots) designed for this task is increasingly being mentioned, because it is one of the most time-tested and reliable projects in this area. However, one important detail is unfairly neglected - what can we do after Python starts running on the device? Are there any restrictions? Can all libraries be included? Is it possible to do everything that you can do when writing an application in Java? These questions concern many, and they are considered and resolved within the framework of the Kivy project. In this article I will try to consider the most interesting and important details.

Python-for-android

First, let's take a look at what makes Python work on Android - a tool called, oddly enough, python-for-android. Its main function is to create a distribution - a project folder containing everything needed to run your application. Or rather, the interpreter itself, Kivy and the libraries on which it depends: Pygame, SDL and several others. The distribution also includes a Java loader that renders OpenGL and acts as an intermediary between Kivy and operating system. Then you add your scripts to all this, settings like icon and name, compile with using Android NDK and voila - the APK with your application is ready!

And this is just the basic procedure, in fact the generated batch file can (and does) include much more. Along with everything else, most of the APK is embedded standard library, and any third-party module written in Python can be easily added - just like when developing desktop applications. Adding modules with compiled components is also easy; you just need to specify how they should be assembled. As a rule, this is not difficult; you just need to check a couple of boxes before starting the build procedure, although in rare individual cases additional steps may be required. Python-for-android already includes instructions for compiling popular modules such as: numpy, sqlite3, twisted and even django!

The principles described above only explain in general terms how python-for-android works. You can get more information on this topic at any time by looking at the Kivy documentation. I recommend Buildozer, a python-for-android add-on that provides a user-friendly interface and automatic resolution of some dependencies. We are trying to make sure that the chain of actions written above is used not only in Kivy, but also in other projects. The basic build process will remain the same, but there will be no need for a Java loader, as it is only needed to support some specific needs of the framework.

Calling Android API with PyJNIus

Interacting with the Android API: receiving information from sensors, creating notifications, vibrating, pausing and restarting, whatever - is an important part of your application. Kivy will take care of the basics for you, but you'll want to manage a lot of things yourself. For this purpose, PyJNIus was created - a tool that automatically wraps Java code in a Python interface.

As a simple example, here is a program that will make the phone vibrate for 10 seconds:

From jnius import autoclass # First, we need a link to the Java Activity in which the # application is running, it is stored in the Kivy PythonActivity loader PythonActivity = autoclass("org.renpy.android.PythonActivity") activity = PythonActivity.mActivity Context = autoclass(" android.content.Context") vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) vibrator.vibrate(10000) # argument is in milliseconds

If you're familiar with the Android API, you'll notice that the code above is very similar to the Java code - PyJNIus simply allows us to access the same API, but directly from Python. Most of the Android API can be called in this way, allowing you to achieve the same functionality as when developing in Java.

The main disadvantage of PyJNIus is that it requires a good understanding of the Android API structure, and the code is cumbersome, although its Java equivalent looks exactly the same. To solve this problem, Kivy includes Plyer.

Plyer: cross-platform API for platform-specific tasks

Hint for programmers: if you register for competitions Huawei Honor Cup, get access to the online school for free for participants. You can improve your skills and win prizes in the competition itself. .

– as you might guess from the name of the application, in it you can get the simplest possible training in the Python programming language. So if you have long wanted to start learning this language, then you should start with this wonderful program. After all, it is completely translated into Russian and will help you with tips. Typically, such applications are not translated into Russian, which causes a number of problems.

In this case, everything is convenient and excellent. Many lessons and tests will allow you to get maximum efficiency in your training. That is why many users began to respond extremely positively to the application. Never before has it been so easy and convenient for you to start learning a programming language. The developers tried to make everything as convenient and understandable as possible.


This can be useful in various situations. That is why a huge number of users safely install the application on their mobile devices and remain very pleased with it. After all, you no longer have to rack your brains in search of a quality program that could be. The program will really help you start learning languages ​​and understanding what is happening in the code.

In this article I would like to talk about how to create a full-fledged Python application for Android. No, this is not another manual for creating a script for sl4a, this is a manual for creating a full-fledged application with a UI, the ability to compile an apk and post it on Android Market. At the same time, I would like to brag about my first application on google.play, this is not a hello world, but a useful application for photographers, albeit a highly specialized one.

I’ll probably start the story with the application itself and end with stories about how it was made.

isortViewer

As a photographer, I have to spend a lot of time sorting and selecting photos. You don’t always want to sit at your work computer when you have a laptop, smartphone or tablet at hand, but copy tens of gigabytes of raw or jpeg files, and then synchronizing with a work computer is a dubious pleasure. That's why I created the isortManager and isortViewer programs, which made my job of sorting and selecting photos much easier. Now I can select and sort photos while lying on the beach sofa or while bored in a traffic jam :)

Copy-paste description 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 sessions 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 free program isortManager for the computer, which will save everything into one small project file:
1. Download and run isortManager from the official website;
2. Create a project and add photo folders. Supported formats: jpg and raw (cr2, nef, orf, etc.). As a result, you will end up with a small project file (about 150 MB for several thousand photos, be it jpg or raw format);
3. Copy the project file to your Android device and open it in isortViewer. You can mark photos “for deletion”, as well as give ratings from 1 to 5 stars;
4. After that, copy the project file back and apply the changes to isortManager. In addition to deleting selected photos, you can copy or move tagged photos. For example, photos with “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, but donations are welcome.

Sources are available on the official website!

How it was done

Since python best language I don’t know anything about programming in the world other than python, so it was decided to write an application in python.

isortManager

There were no problems with isortManager for PC; creating a GUI assembly for windows already It has been tested for a long time and it works very simply: using dcraw it rips out jpg previews from raw files, resizes them using PIL and puts them into one file with a fairly simple structure. He invented his own weight bike with a container for storing photos and metadata (full path to the photo, a “to be deleted” mark and a rating mark), since, for example, zip or tar format cannot change one file in the archive (metadata), requiring repacking all files. The files are simply written one after another in a row and in the metadata block (the usual repr of the Python dictionary) at the end the offsets of the beginning of the files are stored, plus the size of the metadata block is written at the end of the file. The GUI is written in Tkinter (I love it for the speed of writing and the small size of the final assembly), here is the actual screenshot:

And yes, all this works on Linux, and even faster than on Windows)

isortViewer for android

In total, I found two ways to make a 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 manually, which would take a lot of time, so the kivy framework was chosen. So:

This is a wonderful framework for writing applications for Windows, Linux, MacOS, Android and iOS. Multi-touch is supported, the UI is drawn via OpenGL, which means hardware acceleration should work. You can see the widgets in action by installing the demo application Kivy Showcase. It is possible to use some platform-specific functions, for example, vibration or accelerometer, using the android module.

Hello World looks like this:

Import kivy kivy.require("1.0.6") # replace with your current kivy version ! from kivy.app import App from kivy.uix.button import Button class MyApp(App): def build(self): return Button(text="Hello World") if __name__ == "__main__": MyApp().run( )

Moreover, this code will work on all declared platforms. There is no need to test the application in the emulator. It is enough to run the script in your favorite IDE and see the result on the computer screen, without delays for compilation, launching the emulator, etc. If you still want to see how it will look directly on the device, just install Kivy Launcher, copy the project files to the card memory and run. You can debug this using adb logcat.
If you are using an android library that is not on a 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 the apk is quite simple and is described on this page. After building the release, all you need to do is sign your application (I used this manual) and upload it to Google Play.

Pros of kivy:

  • Fast development for various platforms, with virtually no code modifications.
  • Wide selection of widgets available
  • High speed. All resource-intensive ones are moved to C modules. The python interpreter itself works natively on Android.
  • The framework includes many tools, such as animation, caching, etc.
  • upd: Access to camera, clipboard, microphone. You can write your own video player in literally 20 lines.

Cons of kivy:

  • Big size apk file. A project with 300 kb of resources (scripts, graphics) is assembled into a 7 mb apk. Although, I think there is an opportunity to somehow optimize this.
  • Impossibility (yet) to restore work after minimizing - the application closes

In my next article, I will talk in more detail about the process of writing an application from scratch, going over every line of code.

I would like to immediately stop the holivars on the topic “for android only java, python is not needed.” I believe that it doesn’t matter what technologies are “under the hood”, the main thing is that the application is of high quality.
The scope of kivy can be huge. Now I have several applications written to order, where python with the kivy framework showed itself to be good. For example, in exactly one hour, an application was written for the network service centers. The application works in kiosk mode and is installed on cheap Chinese tablets, which hang in the hall. The client dials the order number, then the tablet connects to the server via WiFi and reports the order status.

Tags: Add tags

In the process of learning a language, we usually use a PC to work with relevant applications, environments, programs, read books, use popular online courses. Now that smartphones are with us always and everywhere, it would be a sin not to take advantage of their capabilities to acquire programming knowledge or improve it.

In the process of learning a programming language, in particular Python, I believe that you need to immerse yourself in it completely. Slogan: “Not a day without a code!” I’ll add, well, if you don’t code, then at least read about it. In order not to get out of rhythm in conditions when you need to do a lot in life, but there is no time, and you would like to learn Python, mobile applications will help you not waste time in transport, while waiting, etc. I offer a list of applications from Google Play for learning Python on Android devices, which will help not only gain knowledge, but also test your level of Python. I definitely recommend three of the following applications to students when learning Python: Learn Python, Quiz&Learn Python and Python Challenge. Well, now more details.
1. Learn Python Rating 4.8 as of 10/20/2015
One of the best, nice app: I was immediately pleased with the interface. Contains short lessons and tests that control the process of learning Python. There is an element of excitement - you gain points for completing classes and tests. Upon completion of the course you can receive a certificate. The application covers the following topics: Python basics, data types, control structures, functions and modules, working with files, functional programming, object-oriented programming, regular expressions.


2. QPython - Python for Android Rating 4.4 as of 10/19/2015
QPython is a script that runs Python on Android devices, allows you to execute scenarios and projects. QPython contains a Python interpreter, console, editor, and the SL4A library (which does not always allow the creation and execution of scripts written in various scripting languages ​​directly on Android devices. SL4A is intended for developers and is still in alpha testing - en.wikipedia .org/wiki/SL4A) for Android. Thus, the app offers a developer kit that makes it easy to create Python projects and scripts on Android devices. Main characteristics
  • support for Python programming on Android, including web applications, games and SL4A programming, etc.
  • running Python scripts/projects on Android devices
  • you can execute Python code & files with QRCode
  • QEdit makes it easy to create/edit Python scripts/projects
  • includes many useful Python libraries
  • pip support (a package management system used to install and manage software packages written in Python en.wikipedia.org/wiki/Pip_ (package_manager))
  • support SL4A programming to access Android functions: network, Bluetooth, GPS, etc.

There is also an application called QPython3, which, unlike QPython, has python3 support.
3. Python Documentation Rating 4.4 as of 10/19/2015
Convenient and stylish application with documentation for Python 3.5 English language, completely offline. It has improved search, simple navigation, mobile reading format, user-friendly interface. The content of the documentation includes the following sections:

  • What's new in Python?
  • Python Tutorial
  • Python reference library
  • Python language reference
  • Python setup and use
  • Python HOWTOs
  • Extensions and attachments
  • Python/C API
  • Installing Python modules
  • Uninstalling Python modules


4. Quiz&Learn Python Rating 4.1 as of 10/19/2015
Quiz&Learn Python application for testing and improving Python knowledge and concepts (version 2.7). The questions range from the basics of Python programming to very specific, perhaps surprising, ways to write code. Depending on your progress, questions may become more difficult. The faster you answer, the more points you get. You can delete two incorrect answers, skip a question, stop the timer, or debug the code. Developer site mobileicecube.com/quiz-learn-python.


5. Python interview questions Rating 4.0 as of 10/20/2015
Interesting application. Helps test your knowledge of the Python language in the form of an interview.


6. Python Challenge Rating 3.9 as of 10/19/2015
This application is an English test for users who would like to learn and test their Python programming knowledge. The application has two main modes: Challenge Mode and Practice Mode. Challenge Mode consists of 20 questions. Points are awarded based on the time required to solve each question. The test ends when the user selects the wrong answer or completes all 20 questions. In Practice Mode, questions are sorted into different topics. The user can choose them himself. There are 10 questions offered. The user can continue the test even if he gives the wrong answer. All questions and answers will be shown at the end of the training.


7. Python Guide Rating 3.9 as of 10/19/2015
This app is a quick tutorial for Python. The guide will be especially useful for beginners who want to familiarize themselves with the rules of Python syntax. The tutorial covers the basics of Python programming so that users will have enough knowledge to create some simple and productive application. The manual consists of the following topics: variables, conditions, functions, loops, lists, strings, dictionaries. A small list. I wish there was more.


8. Python Programming in a day Rating 3.0 as of 10/20/2015
The app developers advise us to skip the long, complex books on Phyton. To help you learn Phyton 3.0 programming faster, they offer a short and concise app that will teach you everything you need to know about Phyton programming. This book is written for people who have no knowledge of programming or are beginners. It focuses on the most important concepts with examples.


9. Learn Python & Python Django Rating 3.0 as of 10/20/2015
This application finally differs from the previous ones in that it affects not only Python itself, but also Django. This is essentially a tutorial in English on Python and Django on the Udemy platform. Its content includes the following topics: Python and Hello World, overview and history of Python, functions, classes, databases, modules and packages, JSON, installing Django, admin interface, Django template language, etc... The application contains 18 lectures, more than 4- x hours of high-quality content, community, video and audio lectures, presentations, articles, you can save courses to view in offline mode.


10. Python Tutorial Rating 4.0 as of 10/20/2015
A collection of tutorials on Python 2 and 3. It will tell you about the history and philosophy of Python, how to install Python, work with numbers and strings in Python, about data types and variables, functions, etc... I didn’t like it because it’s a little not tailored to the capabilities mobile phones- the fonts are small in some places. The title in Russian inspired me that the tutorial could be in Russian - but no! In general, I did not find any applications in Russian.


You can also name applications such as:
  • Dive Into Python 3 - book by Mark Pilgrim, rated 3.8, has problems with interface stretching.
  • Python For Android - Python IDE, rated 3.3 due to intrusive advertising
  • Python Tutorial - a tutorial on programming in Python 2.6, rating 3.8
  • Python Programming Tutorial – tutorial on Python 2.7, rating 3.6

In the process of learning a language, we usually use a PC to work with relevant applications, environments, programs, read books, and use mass online courses. Now that smartphones are with us always and everywhere, it would be a sin not to take advantage of their capabilities to acquire programming knowledge or improve it.

In the process of learning a programming language, in particular Python, I believe that you need to immerse yourself in it completely. Slogan: “Not a day without a code!” I’ll add, well, if you don’t code, then at least read about it. In order not to get out of rhythm in conditions when you need to do a lot in life, but there is no time, and you would like to learn Python, mobile applications will help you not waste time in transport, while waiting, etc. I offer a list of applications from Google Play for learning Python on Android devices, which will help not only gain knowledge, but also test your level of Python. I definitely recommend three of the following applications to students when learning Python: Learn Python, Quiz&Learn Python and Python Challenge. Well, now more details.
1. Learn Python Rating 4.8 as of 10/20/2015
One of the best, nice app: I was immediately pleased with the interface. Contains short lessons and tests that control the process of learning Python. There is an element of excitement - you gain points for completing classes and tests. Upon completion of the course you can receive a certificate. The application covers the following topics: Python basics, data types, control structures, functions and modules, working with files, functional programming, object-oriented programming, regular expressions.


2. QPython - Python for Android Rating 4.4 as of 10/19/2015
QPython is a script that runs Python on Android devices, allowing you to run scripts and projects. QPython contains a Python interpreter, console, editor, and the SL4A library (which does not always allow the creation and execution of scripts written in various scripting languages ​​directly on Android devices. SL4A is intended for developers and is still in alpha testing - en.wikipedia .org/wiki/SL4A) for Android. Thus, the app offers a developer kit that makes it easy to create Python projects and scripts on Android devices. Main characteristics
  • support for Python programming on Android, including web applications, games and SL4A programming, etc.
  • running Python scripts/projects on Android devices
  • you can execute Python code & files with QRCode
  • QEdit makes it easy to create/edit Python scripts/projects
  • includes many useful Python libraries
  • pip support (a package management system used to install and manage software packages written in Python en.wikipedia.org/wiki/Pip_ (package_manager))
  • Support SL4A programming to access Android functions: network, Bluetooth, GPS, etc.

There is also an application called QPython3, which, unlike QPython, has python3 support.
3. Python Documentation Rating 4.4 as of 10/19/2015
Convenient and stylish application with Python 3.5 documentation in English, completely Offline. It has improved search, simple navigation, mobile reading format, user-friendly interface. The content of the documentation includes the following sections:

  • What's new in Python?
  • Python Tutorial
  • Python reference library
  • Python language reference
  • Python setup and use
  • Python HOWTOs
  • Extensions and attachments
  • Python/C API
  • Installing Python modules
  • Uninstalling Python modules


4. Quiz&Learn Python Rating 4.1 as of 10/19/2015
Quiz&Learn Python application for testing and improving Python knowledge and concepts (version 2.7). The questions range from the basics of Python programming to very specific, perhaps surprising, ways to write code. Depending on your progress, questions may become more difficult. The faster you answer, the more points you get. You can delete two incorrect answers, skip a question, stop the timer, or debug the code. Developer site mobileicecube.com/quiz-learn-python.


5. Python interview questions Rating 4.0 as of 10/20/2015
Interesting application. Helps test your knowledge of the Python language in the form of an interview.


6. Python Challenge Rating 3.9 as of 10/19/2015
This application is an English test for users who would like to learn and test their Python programming knowledge. The application has two main modes: Challenge Mode and Practice Mode. Challenge Mode consists of 20 questions. Points are awarded based on the time required to solve each question. The test ends when the user selects the wrong answer or completes all 20 questions. In Practice Mode, questions are sorted into different topics. The user can choose them himself. There are 10 questions offered. The user can continue the test even if he gives the wrong answer. All questions and answers will be shown at the end of the training.


7. Python Guide Rating 3.9 as of 10/19/2015
This app is a quick tutorial for Python. The guide will be especially useful for beginners who want to familiarize themselves with the rules of Python syntax. The tutorial covers the basics of Python programming so that users will have enough knowledge to create some simple and productive application. The manual consists of the following topics: variables, conditions, functions, loops, lists, strings, dictionaries. A small list. I wish there was more.


8. Python Programming in a day Rating 3.0 as of 10/20/2015
The app developers advise us to skip the long, complex books on Phyton. To help you learn Phyton 3.0 programming faster, they offer a short and concise app that will teach you everything you need to know about Phyton programming. This book is written for people who have no knowledge of programming or are beginners. It focuses on the most important concepts with examples.


9. Learn Python & Python Django Rating 3.0 as of 10/20/2015
This application finally differs from the previous ones in that it affects not only Python itself, but also Django. This is essentially a tutorial in English on Python and Django on the Udemy platform. Its content includes the following topics: Python and Hello World, overview and history of Python, functions, classes, databases, modules and packages, JSON, installing Django, admin interface, Django template language, etc... The application contains 18 lectures, more than 4- x hours of high-quality content, community, video and audio lectures, presentations, articles, you can save courses for offline viewing.


10. Python Tutorial Rating 4.0 as of 10/20/2015
A collection of tutorials on Python 2 and 3. It will tell you about the history and philosophy of Python, how to install Python, work with numbers and strings in Python, about data types and variables, functions, etc... I didn’t like it because it’s a little not tailored to the capabilities mobile phones - the fonts are small in some places. The title in Russian inspired me that the tutorial could be in Russian - but no! In general, I did not find applications in Russian.


You can also name applications such as:
  • Dive Into Python 3 - book by Mark Pilgrim, rated 3.8, has problems with interface stretching.
  • Python For Android - Python IDE, rated 3.3 due to intrusive advertising
  • Python Tutorial - a tutorial on programming in Python 2.6, rating 3.8
  • Python Programming Tutorial – tutorial on Python 2.7, rating 3.6