用Python写安卓APP,你怕不怕

我们知道了Python可以开发桌面应用(PyQt、wxPython等),可以开发服务端(twisted等),可以开发web端(Django、Flask等),开发爬虫(pyspider等),开发硬件stm32(PyBoard等),是一个全栈开发语言。那么他能否挑战一下安卓呢?
我们可以使用 kivy 开发安卓 APP,Kivy 是一套专门用于跨平台快速应用开发的开源框架,使用 Python 和 Cython 编写,对于多点触控有着非常良好的支持,不仅能让开发者快速完成简洁的交互原型设计,还支持代码重用和部署,绝对是一款颇让人惊艳的NUI框架。

因为跨平台的,所以只写一遍代码,就可以同时生成安卓及 IOS 的 APP,很酷吧。

1.kivy安装

环境说明:笔者在用的是 Python2.7.10

这里仅介绍 Windows 平台安装

所有平台参考: https://kivy.org/#download

更新 pip,setuptools

python -m pip install --upgrade pip wheel setuptools

然后是安装所需要的依赖

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

值得注意的是,上面的安卓需要访问 Google,所以请自备梯子,而且 kivy.deps.gstreamer这个包比较大(95MB),可以单独本地安装

然后就是安装kivy了

python -m pip install kivy

至此,安装就已经完毕了,值得注意的是64位系统没有开启虚拟化支持,在导入 kivy 的时候会报错,如果是 64 位系统就设置一下机器的 BIOS,开启虚拟化支持吧。

注:这里只是 kivy 的运行环境,这样我就能直接在 Windows 机器上直接调试了,怎么将代码编译成 APK 文件我们会在后面讲到。

2.测试Hello World

新建一个.py文件

from kivy.app import Appfrom kivy.uix.button importButton
class TestApp(App):
   def build(self):
       return Button(text='Hello World')
TestApp().run()

运行如下
在这里插入图片描述弹窗如下
在这里插入图片描述

3.实战

1.环境

官方说明的环境,如下:

You’ll need:

A linux computer or a virtual machine
Java
Python 2.7 (not 2.6.)
Jinja2 (python module)
Apache ant
Android SDK

虽然官方提供了一个似乎还不错的虚拟机镜像,但是还是有很多内容需要F出去,所以笔者在这里提供相对而言更加完善的镜像。

下载地址:http://pan.baidu.com/s/1geyAY7x

注:virtualbox,vmware 需自行下载。

root密码:kivy

默认使用账户kivy,密码:kivy123

当然你也可以下载官方镜像,因为第一次编译需要去国外下一大堆东西,所以请自行去下载。

2.Virtual Machine

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

Kivy Buildozer VM
Or select the Torrent

在笔者提供的镜像里,桌面上有一个dev_and,只要将上面写的代码,放入这个文件夹即可(当然也可以在其他目录,后面会讲到)。

cd Desktop/dev_and/

初始化会在当前目录生成一个buildozer.spec文件 用于配置生成的apk相关信息。

buildozer init

###修改buildozer.spec文件

vi buildozer.spec

至少修改下面三项

# (str) Title of your applicationtitle = helloworld
# (str) Package namepackage.name = helloapp
# (str) Package domain (needed for android/ios packaging)package.domain = youer.com

然后注释

# (str) Application versioning (method 1)#version.regex = __version__ = ['"](.*)['"]#version.filename = %(source.dir)s/main.py

下面这行改为非注释

version = 1.2.0

最后我们生成我们需要的apk文件

buildozer -v android debug

buildozer.spec更详细的相关参数配置参考:

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

buildozer命令会在当前文件夹创建一个bin,该文件夹里面有我们想要的apk文件

helloapp-1.2.0-debug.apk

安装以后是这样:
在这里插入图片描述
使用kivy开发的安卓小游戏
https://github.com/mvasilkov/kb/tree/master/6_2048

原文链接:https://mp.weixin.qq.com/s/NTxNPZRci_b7zhZgU2MrGw

要使用 Python安卓应用程序,你可以使用 Kivy 框架。Kivy 是一个开源的 Python 应用程序开发框架,可以帮助你创建跨平台的移动应用程序,包括 Android、iOS 和 Windows。 以下是使用 Kivy 创建一个简单的安卓应用程序的步骤: 1. 安装 Kivy 使用 pip 命令安装 Kivy: ```bash pip install kivy ``` 2. 创建应用程序 创建一个名为 main.py 的 Python 文件,并在其中编应用程序的代码。例如,以下代码创建一个简单的应用程序,显示一个标签和一个按钮: ```python import kivy from kivy.app import App from kivy.uix.label import Label from kivy.uix.button import Button class MyApp(App): def build(self): label = Label(text="Hello, world!") button = Button(text="Click me!") return label, button if __name__ == "__main__": MyApp().run() ``` 3. 构建 APK 文件 使用 Buildozer 工具将 Python 代码打包成 APK 文件。Buildozer 是一个开源的 Python 工具,可帮助你构建 Android 应用程序。 首先安装 Buildozer: ```bash pip install buildozer ``` 然后在应用程序目录中创建一个名为 buildozer.spec 的文件,其中包含应用程序的配置信息。以下是一个示例文件: ```ini [app] # (str) Title of your application title = My Application # (str) Package name package.name = myapp # (str) Package domain (needed for android/ios packaging) package.domain = org.example.myapp # (str) Source code where the main.py live source.dir = . # (list) Source files to include (let empty to include all the files) source.include_exts = py,png,jpg,kv,atlas # (list) Application requirements # comma separated e.g. requirements = sqlite3,kivy requirements = kivy [buildozer] # (int) Log level (0 = error only, 1 = info, 2 = debug (with command line option -v)) log_level = 1 # (str) Path to build artifact storage, absolute or relative to spec file # build_dir = ./.buildozer # (str) Path to build output (i.e. .apk, .ipa) storage # bin_dir = ./bin # (str) Path to build output for debug builds (.apk/.ipa) # debug = ./bin # (str) Path to build output for release builds (.apk/.ipa) # release = ./bin # (list) List of build dependencies (boost, openssl, etc.) # depends = # (str) Android NDK version to use # android.ndk_version = 19b # (int) Android SDK version to use # android.sdk_version = 24 # (str) Python for android distribution to use # python-for-android.branch = master # (str) python-for-android git clone directory (if not specified, it will be automatically cloned from github) # python-for-android.source_dir = # (str) The Android arch to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64 # arch = armeabi-v7a # (str) Sequence of toolchain to use, chooses the first one that exists. # toolchain = gcc # (str) NDK directory (if empty, it will be automatically downloaded.) # android.ndk_path = # (str) Android SDK directory (if empty, it will be automatically downloaded.) # android.sdk_path = # (str) ANT directory (if empty, it will be automatically downloaded.) # android.ant_path = # (str) android api to use # android.api = 27 # (bool) Use --private data storage (True) or --dir public storage (False) # android.private_storage = True # (str) Android NDK directory (if empty, it will be automatically downloaded) # android.ndk_path = /home/user/android/android-ndk-r19c # (str) Android SDK directory (if empty, it will be automatically downloaded) # android.sdk_path = /home/user/android/android-sdk-24 # (str) Build platform for python-for-android (ios, android, manylinux2010) # p4a.build_platform = android # (str) Path to a custom AndroidManifest.xml # android.manifest = # (str) Path to a custom source.properties # android.source_properties = # (str) Path to a custom ant.properties # android.ant_properties = # (str) Path to a custom androidtool.cfg # android.androidtool_cfg = # (str) Path to the android command to use. # android.cmd = adb ``` 最后,使用 Buildozer 命令打包 APK 文件: ```bash buildozer android debug ``` 4. 运行应用程序 在 Android 设备上安装 APK 文件,并启动应用程序。你应该可以看到一个显示标签和按钮的屏幕。 以上就是使用 Python 创建安卓应用程序的基本步骤。你可以在 Kivy 文档中找到更多相关信息。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值