How to write an Android APP in Python

Source: Internet
Author: User
Tags gstreamer

How to write an Android APP in Python
Writing Android apps using Python is definitely not the best choice, but it must be a very lazy choice and I really don't want to learn Java. Furthermore, as far as programming is concerned, there will already be Python and Golang (Note: Python and Golang are both at the normal level). After a long time, Google will be able to write Android apps in Python.

Since you want to write an APP, you have to have an idea.

In fact, I want to make two apps, one of which is fun to write and the other is about O & M.

The O & m app should be designed as follows:

This may be long

 

 

Then the design should be like this.

If you think it is feasible, leave a comment and you think it should be written into the common O & m commands of this APP ^ _ ^. What I think of now is top, free-m, df-h, uptime, iftop and iotop. If you have any good ideas, I will write this project into github. If you use it together, open source is king, haha.

 

Okay, go to the topic.

We use kivy to develop Android apps. Kivy is an open-source framework specially designed for cross-platform Rapid Application Development. It is written in Python and Cython and has excellent support for multi-touch, it not only allows developers to quickly complete simple interaction prototype design, but also supports code reuse and deployment. It is definitely an amazing NUI framework.

Because it is cross-platform, it is cool to write code once and generate apps for Android and IOS at the same time.

This article will show you how to write a Hello world and take a look at Python version 2048 code.

 

Kivy Installation

Environment Description: I am using Python2.7.10

This document only describes how to install windows.

All platform reference: https://kivy.org/#download

 

Update pip and setuptools

python-mpipinstall--upgradepipwheelsetuptools

Then install the required dependencies.

python-mpipinstalldocutilspygmentspypiwin32kivy.deps.sdl2kivy.deps.glew\kivy.deps.gstreamer--extra-index-urlhttps://kivy.org/downloads/packages/simple/

It is worth noting that the above Android needs to access Google, so please bring your own ladder, and kivy. deps. gstreamer this package is relatively large (95 MB), can be installed locally, http://pan.baidu.com/s/1o7mlxNk

Then install kivy.

python-mpipinstallkivy

 

Now, the installation is complete. It is worth noting that the 64-bit system does not enable virtualization support and an error will be reported when importing kivy. If it is a 64-bit system, set the BIOS of the machine, enable virtualization support.

Note: This is only the running environment of kivy, so that I can directly debug the code on a windows machine. We will discuss how to compile the code into an APK file later.

 

If kivy is in python, it should be able to be imported.

According to World Conventions, let's "hello.

Create a. py file

fromkivy.appimportAppfromkivy.uix.buttonimportButtonclassTestApp(App):defbuild(self):returnButton(text='Hello,kivy')TestApp().run()

 

Run

A box will pop up, like below. Click "hello, kivy" to change the color.

Click the window and Press F1 to display some properties of the window.

Then let's look back at the code.

# Import the App, and then let the TestApp class inherit fromkivy. appimportApp # import a Button, which is available for O & M. When you click it, fromkivy is returned. uix. buttonimportButton ### define the class. The class name must be xxxAppclassTestApp (App): ### build a Buttondefbuild (self): ### return a Button with the text "Hello, kivy "returnButton (text = 'hello, kivy') # run the TestApp () method, which inherits the App (). run ()

The above is our Hello

Running on windows doesn't make much sense. How can we run on Android phones is what we want,

In this case, we need a compilation environment.

The environment officially stated is as follows:

You'll need:

A linux computer or avirtual machine

Java

Python 2.7 (not 2.6 .)

Jinja2 (python module)

Apache ant

Android SDK

 

 

Although the official website provides a virtual machine image that seems to be good, there is still a lot of content to be turned over. Therefore, the author provides a more comprehensive image here.

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

Note: virtualbox and vmware must download it by themselves

Root Password: kivy

The default account kivy is used, and the password is kivy123.

 

Of course, you can also download official images, because the first compilation requires a lot of stuff abroad, so please bring your own ladder.

Virtual Machine

A Virtual Machine with Android SDK and NDK and all otherpre-requisites pre installed to export apk generation:

  • Kivy Buildozer VM

    Or select theTorrent

     

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

    cdDesktop/dev_and/

    # During initialization, A buildozer. spec file is generated in the current directory to configure the generated apk information.

    buildozerinit

    ### Modify the buildozer. spec File

    vibuildozer.spec

    Modify at least the following three items

    #(str)Titleofyourapplicationtitle=helloworld#(str)Packagenamepackage.name=helloapp#(str)Packagedomain(neededforandroid/iospackaging)package.domain=youer.com

    Then comment

    #(str)Applicationversioning(method1)#version.regex=__version__=['"](.*)['"]#version.filename=%(source.dir)s/main.py
    The following line is changed to non-Annotated version = 1.2.0

    Finally, we generate the apk file we need.

    buildozer-vandroiddebug

     

    For more detailed parameter configurations of buildozer. spec, refer:

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

     

    The buildozer command will create a bin in the current folder, which contains the desired apk File

    helloapp-1.2.0-debug.apk

     

    This should be the case after installation

    In other words, there may be insufficient space during compilation. Expand the capacity based on different virtual machines (vmware or virtualbox.

     

    Finally, let's take a look at the source code of the 2048 game developed by Python.

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

    First look

     

     

     

    After a try, it is still quite smooth. If you are interested, you can download and play it.

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

    Although this game code is not long enough, it is still quite space-consuming, so a brief description of the process.

    It consists of three parts: clips, images, audios, and other files, Python code, and kv files. This kv file is a bit like css in html.

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

    Then there must be a class named XXXApp and inherit the App.

    For example, if this class is called GameApp, The kv file in this directory must be Game, as shown in. If not, some settings in the kv file will not take effect.

    For example, set a tag

    Okay, click it until now, but it seems that nothing has been reached ~~~

     

    I think I will write another article from scratch.

    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.