How to write an Android app in Python

Source: Internet
Author: User
Tags gstreamer

 Preface: UsingPythonWrite AndroidAPPcertainly not the best choice, but it must be a very lazy choice, and really do not want to learnJava, and, as far as programming is concerned,Pythonwith theGolang (Note:Python,Golanglevel is General), so longGooglea bitPythonfor Android.APPpossibility, but also a very well.

since we're going to write a APP, then you have to have an idea.

In fact, I want to do two APP , one is to write their own fun, one is about operation and maintenance.

about operation and maintenance APP, design should be as follows

It might look like this.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/79/77/wKioL1aSO-mz5WNsAABnZ8Z82EA670.png "title=" Image 2016-01-10-19 10.png "width=" 258 "height=" 486 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:258px;height:486px; "alt= "Wkiol1aso-mz5wnsaabnz8z82ea670.png"/>


Then the design should be like this.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/79/79/wKiom1aSPDGAUGiCAAA-BVCeTpE757.png "title=" Image 2016-01-10-19 11.png "alt=" Wkiom1aspdgaugicaaa-bvcetpe757.png "/>

If you think it's possible, comment on the message you think you should write it in . the operation of the APP is commonly used command bar ^_^, the author temporarily think of is top,free-m,df–h,uptime,iftop,iotop, if there are any good ideas on the hard to hit it, The author should also put this into a project to put on GitHub, we use together, open source is the king, haha.

All right, get to the chase.

we use Kivy to develop the Android app,Kivy is a set of open-source frameworks dedicated to cross-platform rapid application development using Python and Cython Written, very good support for multitouch, not only allows developers to quickly complete a concise interactive prototyping, but also support code reuse and deployment, is a rather impressive NUI framework.

because the cross-platform, so write only once code, you can simultaneously generate Android and IOS APP, it's cool.

This article will take you to write a Hello World and take a look at the Python version of the 2048 code

Kivy Installation

Environment Description: The author is using Python2.7.10

Only the Windows platform installation is described here

All platforms Reference : https://kivy.org/#download

Update Pip,setuptools

python-m pip install--upgrade pip wheel Setuptools

Then the dependencies required for the installation

python-m pip install docutils pygmentspypiwin32 kivy.deps.sdl2 kivy.deps.glew kivy.deps.gstreamer--extra-index-url https://kivy.org/downloads/packages/simple/

It is important to note that the above Android needs to access Google, so please bring your own ladder, and kivy.deps.gstreamer This package is relatively large (95MB), can be installed locally,/HTTP/ Pan.baidu.com/s/1o7mlxnk

and then it's installing kivy .

python-m pip Install Kivy

At this point , the installation has been completed, it is worth noting that the system does not open the virtualization support, in the import of Kivy will be error, if it is a system set up the machine BIOS, Turn on virtualization support.

Note: This is just the kivy environment, so I can directly debug on the Windows machine directly, how to compile the code into an APK file we will talk about later.

If Kivy should be able to import in Python.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/79/77/wKioL1aSPUOCRCPMAAAhlWpoOH8332.png "title=" Image 1.png "alt=" Wkiol1aspuocrcpmaaahlwpooh8332.png "/>

in accordance with world practice, we "Hello" a bit.

Create a new . py File

From Kivy.app import appfrom Kivy.uix.button Importbutton class TestApp (APP): def build (self): Return button (Tex t= ' Hello,kivy ') TestApp (). Run ()

Run

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/79/79/wKiom1aSPWez8sqnAAC28WhQqpA034.png "title=" Image 2.png "alt=" Wkiom1aspwez8sqnaac28whqqpa034.png "/>

then a box pops up, probably as follows, and clicking "Hello,kivy" will change color

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/79/79/wKiom1aSPaHzKAEbAADI4wuPDXw775.png "title=" Image 2016-01-10-17 4.png "alt=" Wkiom1aspahzkaebaadi4wupdxw775.png "/>

Click on the window and press " F1"will be some properties of this window

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/79/79/wKiom1aSPZaDt-hvAABzhM7sjMI004.png "title=" Image 2016-01-10-17 5.png "alt=" Wkiom1aspzadt-hvaabzhm7sjmi004.png "/>

Then we look back at the code.

# #导入App, then let TestApp this class inherit from the Kivy.app import app# #导入一个Button, OPS has this button, when you click on the time will react from the Kivy.uix.button Importbutton # # #定义类, the name must be Xxxappclass TestApp (App): # # #build一个Button def build (self): # # #返回一个Button, the text content is "Hell O,kivy "return button (text= ' Hello,kivy ') # #运行, because the app is inherited, so the run this method TestApp (). Run ()

the top is ours. Hello .

running on Windows of course doesn't make much sense, how to run on an Android phone is what we want,

At this point we need a compilation environment.

The official description of the environment is as follows:

You ' ll need:

    • a Linux computer or a Virtual Machine

    • Java

    • Python 2.7 (not 2.6.)

    • JINJA2 (Python module)

    • Apache Ant

    • Android SDK


Although the official provides a seemingly good virtual machine image, but there is still a lot of content to be flipped out, so I am here to provide a relatively more complete image

:http://pan.baidu.com/s/1geyAY7x

Note : VirtualBox, vmware needs to download it byitself

Root Password:kivy

default Use account Kivy, password:kivy123

Of course, you can also download the official image, because the first compilation needs to go abroad for a lot of things, so please bring your own ladder.

Virtual Machine

A Virtual machine with the Android SDK and NDK and all otherpre-requisites the pre installed to ease apk generation:

    • Kivy Buildozer VM

    • Or Select the Torrent

in the image provided by the author, there is a dev_and on the desktop , as long as the above-written code, put in this folder can ( of course, in other directories, will be mentioned later ).

CD desktop/dev_and/

# # initialization generates a BUILDOZER.SPEC file in the current directory to configure the generated APK related information

Buildozer Init

# # # Modify buildozer.spec file

VI Buildozer.spec

Modify at least three of the following

# (STR) Title of your applicationtitle = HelloWorld # (str) Package namepackage.name = HelloApp # (str) package domain (NE eded for Android/ios packaging) Package.domain = youer.com

Then comment

# (STR) Application versioning (Method 1) #version. Regex = __version__ = [' "] (. *) ['"] #version. FileName =% (source.dir) s/ma in.py
The following line is changed to non-annotated version = 1.2.0

finally we generate the apk file we need

Buildozer-v Android Debug

Buildozer.spec More detailed configuration reference for the relevant parameters:

Http://buildozer.readthedocs.org/en/latest/specifications.html

The buildozer command creates a bin in the current folder with the apk file we want

helloapp-1.2.0-debug.apk


this should be the case after installation

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/79/78/wKioL1aSRn6zEn4PAAAtivIdfGs250.png "title=" Image 2016-01-10-19 17.png "alt=" Wkiol1asrn6zen4paaatividfgs250.png "/>

There may be a lack of space at compile time, depending on the virtual machine (vmware or VirtualBox) Expand your own capacity.


Finally, let's look at the source code of the 2048 version of this game developed by the starter Python.

Code from: https://github.com/mvasilkov/kb/tree/master/6_2048

Look first.


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/79/78/wKioL1aSQTbgkXMhAACo5blBAto943.png "title=" Image 2016-01-10-19 13.png "alt=" Wkiol1asqtbgkxmhaaco5blbato943.png "/>650) this.width=650;" Src= "http://s5.51cto.com/ Wyfs02/m00/79/79/wkiom1asqroa5ix_aabuwz0tid4354.png "title=" Image 2016-01-10-19 14.png "alt=" wkiom1asqroa5ix_ Aabuwz0tid4354.png "/>650) this.width=650;" Src= "http://s2.51cto.com/wyfs02/M01/79/79/ Wkiom1asqr7xixa5aab2uiymegk878.png "title=" Image 2016-01-10-19 15.png "alt=" wkiom1asqr7xixa5aab2uiymegk878.png "/ >


demo, or quite smooth, interested can download play

: Http://pan.baidu.com/s/1eQZACDW

This game code is not long, but it is quite a bit of space, so briefly explain the process.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/79/78/wKioL1aSQ5_zW_KxAAAfzmXNyLk842.png "title=" Image 2016-01-10-19 16.png "alt=" Wkiol1asq5_zw_kxaaafzmxnylk842.png "/>

Mainly consists of three parts, one is material, picture audio and other files, two is the Python code, three kv files, this kv file is a bit like CSS in HTML.

The file name of the Python code is generally named main.py

Then there must be a class called Xxxapp, and inherit the app.

For example, this class is called Gameapp, then the kv file in this directory must be game, as shown, if not, some of the settings in the KV file will not take effect.

Like setting a label

All right, donuts, but nothing seems to have been ordered.


To make an app from scratch, I think I'll start another article.



This article is from the "Ear Notebook" blog, please make sure to keep this source http://youerning.blog.51cto.com/10513771/1733534

How to write an Android app in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.