用python写Android应用

安装kivy模块

pip install kivy
pip install Cython

python代码

from kivy.app import App
from kivy.uix.button import Button

class helloworld(App):
    def build(self):
        return Button(text='hello world')
helloworld().run()

可能的错误

在这里插入图片描述

解决办法

重新安装

pip install --upgrade pip wheel setuptools
pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
pip install kivy.deps.gstreamer
pip install kivy.deps.angle
pip install kivy

参考:https://www.e-learn.cn/content/wangluowenzhang/186622

到此已可以创建一个最简单的窗口程序,但是它是windows程序,要打包为安卓应用,还需要buildozer模块

编译打包为APK文件

有两种方法,一种是buildozer,还有一种是python-for-android

buildozer

buildozer在windows平台下可能会出现错误,最好在win10内置的ubuntu子系统内编译。
每次都要复制已经打包成功的项目目录下的.buildozer到要打包的项目目录下,buildozer才不会重复下载sdk和ndk等。而.buildozer目录通常在1G以上。

win10系统下

打开cmd输入下面的命令

# 安装buildozr
pip install buildozer
# 切换到main.py文件所在目录
cd cd /mnt/c/Users/用户名/Documents/GitHub/工作文件夹
#生成一个.spec文件,里面是app的各种信息
buildozer init 
# 编译
buildozer android debug deploy run

ubuntu子系统下

使用python3和pip3 应把数字带上。

#安装kivy
pip3 install kivy
pip3 install Cython
#安装 buildozer
pip3 install buildozer
# 切换到main.py文件所在目录
cd cd /mnt/c/Users/用户名/Documents/GitHub/工作文件夹
#生成一个.spec文件,里面是app的各种信息
buildozer init 
# 编译
buildozer android debug deploy run

python-for-android(p4a)

You can also package directly with python-for-android, which can give you more control but requires you to manually download parts of the Android toolchain.

# 安装p4a
pip install python-for-android

p4a has several dependencies that must be installed:

  1. git
  2. ant
  3. python2
  4. cython (can be installed via pip)
  5. a Java JDK (e.g. openjdk-8)
  6. zlib (including 32 bit)
  7. libncurses (including 32 bit)
  8. unzip
  9. virtualenv (can be installed via pip)
  10. ccache (optional)
  11. autoconf (for ffpyplayer_codecs recipe)
  12. libtool (for ffpyplayer_codecs recipe)
  13. cmake (required for some native code recipes like jpeg’s recipe)

kivy编译官方教程https://kivy.org/doc/stable/guide/packaging-android.html

  1. 参考:https://blog.csdn.net/davied9/article/details/93361566
  2. 参考:https://blog.csdn.net/xj032w2j4ccjhow8s8/article/details/78737207
  3. 参考:https://www.jianshu.com/p/0e75e737ee72
  4. 参考:kivy教程
  5. 参考:https://www.cnblogs.com/CoXieLearnPython/p/9231949.html
  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值