详解如何把.py文件转.apk
前言
Kivy是一套Python下的跨平台开源应用开发框架,官网,我们可以用它来将Python程序打包为安卓的apk安装文件。以下是在win7 64位环境中的具体配置步骤(安装和配置的过程中会下载很多东西,确保你能够稳定地访问外网--即能访问YouTuBe等网站)
一、环境配置
1、安装python,我本地安装的是【python3.6.1】
2、按照kivy官网操作,
>1、Ensure you have the latest pip, wheel, and virtualenv
python36 -m pip install --upgrade pip wheel setuptools
![](https://img.haomeiwen.com/i6424670/bd73d65cc0cdb542.jpg)
>2、Install the dependencies (skip gstreamer (~120MB) if not needed, see Kivy’s dependencies)
python36 -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
![](https://img.haomeiwen.com/i6424670/401fc87d61481827.jpg)
python36 -m pip install kivy.deps.gstreamer
![](https://img.haomeiwen.com/i6424670/0d9437a7758ac083.jpg)
>3、For Python 3.5+, you can also use the angle backend instead of glew. This can be installed with
python36 -m pip install kivy.deps.angle
![](https://img.haomeiwen.com/i6424670/cec6f0c8de551244.jpg)
>4、Install kivy
python36 -m pip install kivy
![](https://img.haomeiwen.com/i6424670/dfecadf839a19993.jpg)
>5、(Optionally) Install the kivy examples
python36 -m pip install kivy_examples
![](https://img.haomeiwen.com/i6424670/8f7d94979199e6c7.jpg)
>6、The examples are installed in the share directory under the root directory where python is installed.
That’s it. You should now be able to import kivy in python or run a basic example if you installed the kivy examples:
pythonkivy_venv\share\kivy-examples\demo\showcase\main.py,写一个简单的程序main.py测试一下:
![](https://img.haomeiwen.com/i6424670/eb66fa93865b9319.png)
运行结果
![](https://img.haomeiwen.com/i6424670/b9eea82c85dd0b2f.png)
运行成功,那开发环境基本没问题了。然后下面就是怎么将这个程序打包成apk。
首先需要安装虚拟机 VirtualBox,这个没问题,很快就装好了,然后Kivy官方制作了该虚拟机的映像(很体贴),下载后就直接能运行了,省去不少麻烦。(kivy虚拟机映像下载地址:https://kivy.org/#download),或者直接去(kivy官网地址:https://kivy.org/doc/stable/installation/installation-windows.html#install-win-dist)下载:
![](https://img.haomeiwen.com/i6424670/23f04a280e9c0eb3.png)
下载并解压之后,打开VB并载入该文件夹下的虚拟机文件,然后运行(登录密码kivy):
![](https://img.haomeiwen.com/i6424670/37050998acbfdd75.png)
![](https://img.haomeiwen.com/i6424670/25b3c0192f1d6d9c.png)
进入到该系统中之后,就需要开始配置环境,具体查看官网文档:https://kivy.org/doc/stable/guide/packaging-android.html#packaging-your-application-into-apk,首先我们先在该系统中添加一个共享文件夹,这样可以和windows共享文件,在VB的设备->共享文件夹选项中有配置:
![](https://img.haomeiwen.com/i6424670/760a88d60eea57a5.png)
然后我们使用固定分配,在windows下某个路径新建一个文件夹后将其选择为共享文件夹,并选择自动挂载:
![](https://img.haomeiwen.com/i6424670/6ba35214acdbfc0c.png)
然后重新启动虚拟机就可以访问该共享文件夹了:
![](https://img.haomeiwen.com/i6424670/52d2cc563eb20d00.png)
设置好了共享文件夹后,可以将之前写的测试程序main.py拷贝到该文件夹,然后在虚拟机中拷贝到桌面备用。然后打开终端(cd到桌面),执行buildozer init命令,之后会在桌面上生成buildozer.spec的配置文件:
![](https://img.haomeiwen.com/i6424670/124077c2e406e6e3.png)
然后使用命令gedit buildozer.spec来编辑该文件,(如果没有gedit可以sudo apt-get install gedit来安装,记得虚拟机要能上网哦)
![](https://img.haomeiwen.com/i6424670/67815f58fb4364db.png)
基本不用改什么,那个source.dir 需要注意,这是刚刚我们放在桌面上的main.py的路径。
然后运行buildozer android_new debug,接着就开始下载很多东西了,例如Android SDK,Android NDK之类,并且是需要访问外网的,自搭梯子。这个过程要很久,请耐心等待
![](https://img.haomeiwen.com/i6424670/838c59e3595b0846.png)
然后在它自动下载配置之后就会在桌面的bin文件夹下面生成了apk文件,可以安装在手机上试一下。
然后基本流程大概如此^^,详细可以去官网了解。
环境配置下载网址:
1、kivy虚拟机映像下载地址:https://kivy.org/#download
2、kivy官网地址:https://kivy.org/doc/stable/installation/installation-windows.html#install-win-dist
遇到的问题点:
1、apt-get update 出现404
解决方法:https://blog.csdn.net/qq_37591986/article/details/79840074