How to use Python Kivy ④ ~ Execution on Android ~

Summary

The articles created so far are as follows.

This time, I will explain the Android output among the mobile (iOS, Android) output, which is an advantage when Kivy compares with the Qt library. From the conclusion first, it is subtle personally. It's short on the obi and long on the tasuki, or it's hard to stand behind.

What to create

Let's execute the previous Linkage with WebAPI (from sending and receiving requests to displaying results) on an Android device.

Code to use

The content is almost the same as the previous code. We will introduce the changed parts as appropriate.

How to run Kivy on an Android device

There are three methods, but the execution method differs depending on the purpose.

① Run on your own Android device (2) Create an APK and distribute it on Google Play etc. so that it can be used by an unspecified number of people. ③ Execute Python on android terminal using python-for-android

In case of ①, it is realized by using an Android application called Kivy Launcher. This time, I will explain using this method.

In case of (2), install a package called Buildozer on Ubuntu to realize it. In the case of windows, VM is distributed, so please use that. You can download it from the Virtual Machine section at https://kivy.org/#download.

In case of actual use, it is better to actually execute it with the post Create # 2 APK to publish Kivy application on Goole Play Store. Please refer to it.

In case of ③, the method is different from ① and ②, and the method is to actually execute the Python script on the Android device. If you search with Qpython, you will find various things.

Regarding the contents of this area, [“Using Python with android” as of 2016](http://hhsprings.pinoko.jp/site-hhs/2016/08/2016%E5%B9%B4%E7%8F % BE% E5% 9C% A8% E3% 81% AE% E3% 80% 8Cpython-% E3% 82% 92-android-% E3% 81% A7% E4% BD% BF% E3% 81% 86% E3 It is explained in detail in the article% 80% 8D /), so please read it if you are interested.

reference

About the Python environment to run

Although it is a verification environment, Kivy will be 1.9.1. On the other hand, Python is ** Python 2.7 **. Looking at Kivy's official website, it is said that Android is provisionally compatible with Python 3, but as of February 2017, it is better to think that it is practically compatible with Python 2 series and not compatible with Python 3 series. The reason will be described later In addition, iOS is compatible with Python 2.7, and it seems that support for Python 3 series is currently being supported.

About the Android device actually used for verification

The models I tried are the following two types.

Android itself has unique functions for each manufacturer and model, so it may not work depending on the model and OS.

Actually run Kivy on Android

It will be the actual operation.

About Kivy Launcher

Kivy Launcher is an app distributed on Google Play, and by using it, Kivy on Android You can easily use a Python script that uses. The following is a simple usage.

In order to explain how to use it, we will use "touchtracer", which is a demo program distributed on the Kivy official website.

reference

How to use Kivy Lancher

Please install Kivy Lancher on Android from Play Store in advance

① Connect your Android device to your PC via USB.

0.png

Select file transfer.

② Create a Kivy folder and place the files

PC.jpg

Create a directory called "Kivy". Create a folder under the directory and place the program.

Place the following files.

It is android.txt, but the contents are as follows

title=Touchtracer
author=Kivy team
orientation=landscape

Enter the name of the app in "title" and the author's name in "author". "Orientation" is whether to display the portrait or landscape orientation at startup. "Landscape" is landscape orientation, and "portrait" is portrait orientation.

Reference: About the background process of Kivy Launcher

It is a program started with Kivy Lanucher, but it seems that it can be run in the background. If you use it, you can also create alarm apps.

reference

③ Start and run the app

I will actually move it.

1. 1. Run Kivy Lancher

Run Kivy Lancher on your Android device. 1.png

The waiting screen is displayed. 3.png

2. Select an app

A list of apps will be displayed, so select the app. 2.png

3. 3. App launch screen

After selecting, the waiting screen is displayed, and after standing for a while, the selected application can be operated on the terminal.

4.png

Try running the previous app

Let's actually run the previous app. I try to move it like "Touch tracer", but when I select it from Kivy Lancher and try to start it, it does not work well and the app itself crashes. I tried to collect the Log inside .kivy of the Android main unit. Below is an excerpt of the contents of the log.

[INFO              ] Kivy: v1.9.1
[INFO              ] Python: v2.7.2 (default, Mar 20 2016, 23:30:13) 

It was running on Python 2.7 ... Therefore, in order to run the program I was writing last time, I had to convert from Pytho3 to Python2. Specifically, the following conversions were performed. I started Python from the 3rd system, so I didn't do the 2nd system.

Of these, the last modification of UrlRequest is explained.

How to use Kivy's UrlRequest

The corrected part in the source is excerpted below.

main.py



from kivy.network.urlrequest import UrlRequest
import urllib

class AddSearchBookForm(BoxLayout):
    def search_book(self):
        set_title = urllib.quote((self.search_input.text).encode('utf-8'))
        req_url = "http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&query=title%3d%22{0}%22%20AND%20from=%22{1}%22&maximumRecords={2}&mediatype=1".format(set_title, set_fromdate, set_maximum_records)

        res = UrlRequest(req_url, self.gotBooklists)
        
    def gotBooklists(self, req, results):
        result_records = result_parse(results)

Regarding UrlRequest, there are two features associated with this modification.

Looking at the API reference of UrlRequest, the specifications are as follows.

req = UrlRequest(url, on_success, on_redirect, on_failure, on_error,
                 on_progress, req_body, req_headers, chunk_size,
                 timeout, method, decode, debug, file_path, ca_file,
                 verify)

First, the URL needs to be a% string, so I converted it to a% string with urllib and put it in the first argument. Also, the req of the value returned at runtime stores the POST return result from the url, but due to asynchronous communication, the result is not returned from the server and the value does not come in immediately, so the program advances. It will be. Therefore, if it is left as it was last time, there is a problem that the processing of the POST result does not go well. It is necessary to set the process (method) when the POST result from the server side is returned in the second argument on_success, or use wait () for the return value and wait for the return result to be returned. .. This time, using the former method of using on_success, gotBooklists () is set to on_sucess, and the process of parsing the result to gotBooklists () and performing formatting is executed.

reference

Execution result

The program could be executed on Kivy Lancher when the operation was performed with the support of Pytho2 system.

1_40.jpg

Regarding Japanese input, IME did not open when inputting full-width on Windows OS, but IME opens when inputting Japanese on Android devices and you can input comfortably.

ideapad.jpg

Reference: How to execute Android-specific API

I won't use it this time, but Kivy can do Android-specific JAVA APIs by using Plyer and Pyjnius. By using this, you can start the camera in the app, take a picture or send an email. Standard functionality in Android apps can be achieved using Plyer or Pyjnius.

I will give the article of the person who is actually used in Japan and the link of the official website

Reference: Support for iOS

I haven't tried iOS, but it is possible to develop iOS apps. The code is only compatible with Python 2 series, not Pytho 3 series. It seems that you can create a project for Xcode using a function called toolchain and build from that project.

reference

Regarding iOS specific API, it seems that this can also be used in the application by using Pyobjus.

reference

In Japan, Professor Haraguchi of Otaru University of Commerce creates a Kivy app and puts it on the App Store. Dr. Haraguchi is also one of the key members of the translation of Kivy's official manual (https://pyky.github.io/kivy-doc-ja/).

reference Otaru University of Commerce Haraguchi Laboratory

Reference To use an external Python library

To use a non-standard Python library such as Matplotlib on mobile, you need to use an add-on function called Kivy garden.

reference

Reference About apps created with Kivy

If you look at the Wiki on the official website, you will find a list of app projects created using Kivy. If you look at this, you can see what kind of apps you can actually create.

reference

Summary

I was able to actually run an application that is displayed in cooperation with the server side on an Android device. Regarding Android apps, if it is a 2D image-based app, I think it is possible to develop most things such as card-based social games and search apps.

Personally, whether or not to use Kivy when creating an Android app is a delicate point. I think that the mainstream of releasing an Android application is to consider the development of the iOS version as well. In that case, development with Pytho2 system is a prerequisite, but it takes time to handle Japanese (multibyte characters). Since there is no community in Japan, I think it would be better at this point to develop multi-platforms using C # such as Xamarin and Unity, or to develop with Andori native. At least in combination with the number of Python engineers in Japan, I don't think it will be actively adopted.

I think that the story will change again if Python3 support is officially released on iOS and a simple preview application like Kivym Lancher is made, but at this stage it will be evaluated like this.

Overall summary

I explained how to use Kivy four times. Animation is a major feature that I haven't discussed yet, but I'll take the opportunity to touch it. If you read the explanation so far, you can develop a general application, or you can get started. I also think that Kivy has a vague idea of what libraries can and cannot do.

I think the advantages and disadvantages of Kivy are as follows.

advantage

Disadvantage

Based on this, I hope you will consider whether to use Kivy or not. Also, since bugs are open source, they will be improved by raising issues and requests, improving and committing, so I think that it depends on the user.

reference

Kivy is still actively interacting on github and I think it has potential as open source. I hope you read this article and would like to use Kivy.

Recommended Posts

How to use Python Kivy ④ ~ Execution on Android ~
[Kivy] How to install Kivy on Windows [Python]
Memorandum on how to use gremlin python
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use Python Kivy ① ~ Basics of Kv Language ~
How to use Django on Google App Engine / Python
How to enjoy Python on Android !! Programming on the go !!
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
Notes on how to use pywinauto
Notes on how to use featuretools
How to use homebrew on Debian
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
Notes on how to use doctest
[Python] How to use Typetalk API
[Hyperledger Iroha] Notes on how to use the Python SDK
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to read pydoc on python interpreter
How to install and use pandas_datareader [Python]
How to use mecab, neologd-ipadic on colab
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
How to use Google Assistant on Windows 10
How to erase Python 2.x on Mac.
[Python] Organizing how to use for statements
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
Install python on xserver to use pip
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use Map in Android ViewPager
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
How to use python put in pyenv on macOS with PyCall