羊奶 發表於 2020-9-24 21:08:00

新技能:用 Python 写一个安卓 APP

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: rgba(0, 0, 0, 1); padding: 0 10px; line-height: 1.6; word-spacing: 0; letter-spacing: 0; word-wrap: break-word; text-align: left; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif"><blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; border-left: 3px solid rgba(255, 0, 0, 1); background: rgba(0, 0, 0, 0.05); color: rgba(106, 115, 125, 1); padding: 8px; margin-bottom: 20px; margin-top: 20px">
<p style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); line-height: 26px; font-size: 14px; letter-spacing: 0.2px">作者:youerning原文链接:<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">http://youerning.blog.51cto.com/10513771/1733534</code></p>
</blockquote>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="display: none"></span><span class="content">前言</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">用 Python 写安卓 APP 肯定不是最好的选择,目前用Java和 kotlin 写的居多,但是肯定也是一个很偷懒的选择,而且实在不想学习 Java,再者,就编程而言已经会的就 Python与Golang(注:Python,Golang水平都一般),那么久Google了一下Python 写安卓的 APP 的可能性,还真行。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">既然要写个APP,那么总得要有个想法吧。其实想做两个APP来着,一个是自己写着好玩的,一个是关于运维的。关于运维的APP,设计应该如下</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">可能长这样</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599987440976-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">然后设计应该是这样。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599987450853-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">如果觉得可行的话,评论留言一下你觉得应该写进这个APP的运维常用命令吧^_^,笔者暂时想到的是top,free -m,df –h,uptime,iftop,iotop,如果有什么好的想法就狠狠的砸过来吧,笔者到时应该也会把这个写成一个项目放到github上,大家一起用嘛,开源才是王道,哈哈。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="display: none"></span><span class="content">开发安卓APP</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">我们使用kivy开发安卓APP,Kivy是一套专门用于跨平台快速应用开发的开源框架,使用Python和Cython编写,对于多点触控有着非常良好的支持,不仅能让开发者快速完成简洁的交互原型设计,还支持代码重用和部署,绝对是一款颇让人惊艳的NUI框架。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">因为跨平台的,所以只写一遍代码,就可以同时生成安卓及IOS的APP,很酷吧。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">本文会带大家写一个Hello world并瞧一瞧 Python 版的2048的代码</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px"><strong style="font-weight: bold; color: rgba(233, 105, 0, 1)">kivy安装</strong></p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">环境说明:笔者在用的是Python2.7.10
这里仅介绍windows平台安装
所有平台参考: <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">https://kivy.org/#download</code>
更新pip,setuptools</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">python&nbsp;-m&nbsp;pip&nbsp;install&nbsp;--upgrade&nbsp;pip&nbsp;wheel&nbsp;setuptools<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">然后是安装所需要的依赖</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">python&nbsp;-m&nbsp;pip&nbsp;install&nbsp;docutils&nbsp;pygmentspypiwin32&nbsp;kivy.deps.sdl2&nbsp;kivy.deps.glew<br>kivy.deps.gstreamer&nbsp;--extra-index-url&nbsp;https://kivy.org/downloads/packages/simple/<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">值得注意的是,上面的安卓需要访问Google,所以请自备梯子,而且kivy.deps.gstreamer这个包比较大(95MB),可以单独本地安装,http://pan.baidu.com/s/1o7mlxNk</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">然后就是安装kivy了</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">python&nbsp;-m&nbsp;pip&nbsp;install&nbsp;kivy<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">至此,安装就已经完毕了,值得注意的是64位系统没有开启虚拟化支持,在导入kivy的时候会报错,如果是64位系统就设置一下机器的BIOS,开启虚拟化支持吧。
注:这里只是kivy的运行环境,这样我就能直接在windows机器上直接调试了,怎么将代码编译成APK文件我们会在后面讲到。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">如果kivy在python中应该就能导入了。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599987517325-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">按照世界惯例,我们”hello”一下吧。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">新建一个.py文件</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">from</span>&nbsp;kivy.app&nbsp;<span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">import</span>&nbsp;Appfrom&nbsp;kivy.uix.button&nbsp;importButton<br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">class</span>&nbsp;<span class="hljs-title" style="color: rgba(230, 192, 123, 1); line-height: 26px">TestApp</span><span class="hljs-params" style="line-height: 26px">(App)</span>:</span><br>&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">def</span>&nbsp;<span class="hljs-title" style="color: rgba(97, 174, 238, 1); line-height: 26px">build</span><span class="hljs-params" style="line-height: 26px">(self)</span>:</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">return</span>&nbsp;Button(text=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'Hello,kivy'</span>)<br>TestApp().run()<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">运行</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599988022441-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">然后会弹出一个框,大概如下,点击”hello,kivy”会变颜色</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599988036062-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">点击窗口并按“F1”会这个窗口的一些属性</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599988048465-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">然后我们回过头看一看代码。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">##导入App,然后让TestApp这个类继承</span><br>from&nbsp;kivy.app&nbsp;import&nbsp;App<span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">##导入一个Button,运维有这个button,当你点击的时候才会有所反应</span><br>from&nbsp;kivy.uix.button<br>importButton<br><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">###定义类,名字必须是xxxAppclass&nbsp;TestApp(App):</span><br>&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">###build一个Button</span><br>&nbsp;&nbsp;&nbsp;def&nbsp;build(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">###返回一个Button,文字内容是“Hello,kivy”</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: rgba(230, 192, 123, 1); line-height: 26px">return</span>&nbsp;Button(text=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'Hello,kivy'</span>)<br><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">##运行,因为继承了App,所以才有的run这个方法TestApp().run()</span><br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">上面就是我们的Hello了</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">在windows上运行当然没有什么太大的意义,怎么在安卓手机上运行才是我们想要的,</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">这时我们需要一个编译环境。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">官方说明的环境,如下:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">You’ll&nbsp;need:<br>A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linux&nbsp;computer&nbsp;or&nbsp;a&nbsp;virtual&nbsp;machine<br>Java<br>Python&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.7&nbsp;(not&nbsp;2.6.)<br>Jinja2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(python&nbsp;module)<br>Apache&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ant<br>Android&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SDK<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">虽然官方提供了一个似乎还不错的虚拟机镜像,但是还是有很多内容需要翻出去,所以笔者在这里提供相对而言更加完善的镜像</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">下载地址:<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">http://pan.baidu.com/s/1geyAY7x</code>
注:virtualbox,vmware需自行下载
root密码:kivy
默认使用账户kivy,密码:kivy123
当然你也可以下载官方镜像,因为第一次编译需要去国外下一大堆东西,所以请自行去下载。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-8-29/1598671090252-%E6%96%87%E7%AB%A0%E4%B8%AD%E5%B5%8C%E5%85%A5%E5%85%AC%E4%BC%97%E5%8F%B7%E4%BB%8B%E7%BB%8D.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px"><strong style="font-weight: bold; color: rgba(233, 105, 0, 1)">Virtual Machine</strong></p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">A Virtual Machine with Android SDK and NDK and all otherpre-requisites pre installed to ease apk generation:</p>
<ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">Kivy Buildozer VM</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">Or select the Torrent</section></li></ul>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">在笔者提供的镜像里,桌面上有一个dev_and,只要将上面写的代码,放入这个文件夹即可(当然也可以在其他目录,后面会讲到)。
cd Desktop/dev_and/</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">初始化会在当前目录生成一个buildozer.spec文件 用于配置生成的apk相关信息
buildozer init</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">###修改buildozer.spec文件</span><br>vi&nbsp;buildozer.spec<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">至少修改下面三项</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">#&nbsp;(str)&nbsp;Title&nbsp;of&nbsp;your&nbsp;applicationtitle&nbsp;=&nbsp;helloworld</span><br><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">#&nbsp;(str)&nbsp;Package&nbsp;namepackage.name&nbsp;=&nbsp;helloapp</span><br><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">#&nbsp;(str)&nbsp;Package&nbsp;domain&nbsp;(needed&nbsp;for&nbsp;android/ios&nbsp;packaging)package.domain&nbsp;=&nbsp;youer.com</span><br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">然后注释</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">#&nbsp;(str)&nbsp;Application&nbsp;versioning&nbsp;(method&nbsp;1)#version.regex&nbsp;=&nbsp;__version__&nbsp;=&nbsp;['"](.*)['"]#version.filename&nbsp;=&nbsp;%(source.dir)s/main.py</span><br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">下面这行改为非注释</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">version&nbsp;=&nbsp;1.2.0<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">最后我们生成我们需要的apk文件</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">buildozer&nbsp;-v&nbsp;android&nbsp;debug<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">buildozer.spec更详细的相关参数配置参考:</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">http://buildozer.readthedocs.org/en/latest/specifications.html</code></p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">buildozer命令会在当前文件夹创建一个bin,该文件夹里面有我们想要的apk文件
helloapp-1.2.0-debug.apk</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">helloapp-1.2.0-debug.apk</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">安装以后是这样:</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599989349852-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">话说在编译的时候可能出现空间不足的情况,根据虚拟机的不同(vmware或virtualbox)自行扩容吧。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">最后我们来瞧瞧简易版Python开发的2048这个游戏的源代码。
代码:
https://github.com/mvasilkov/kb/tree/master/6_2048</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">先看效果图:</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599989364327-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">试玩了一下,还是蛮流畅的,有兴趣的可以下载玩一下。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">下载地址:http://pan.baidu.com/s/1eQZACDW
这个游戏代码虽然不长,但是还是蛮占篇幅的,所以简要的说明一下流程。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-9-13/1599989375719-image.png"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">主要由三部分组成,一是素材,图片音频之类的文件,二是Python代码,三是kv文件,这个kv文件有点像 html 中的css。
Python代码的文件名一般命名为 main.py
然后一定有一个叫做 XXXApp 的类,并继承 App。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">比如该类叫做GameApp,那么该目录下的kv文件则必须为Game,如上图所示,如果不是,那么kv文件中的一些设定就不会生效。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">比如设定一个标签</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://my-wechat.mdnice.com/point.png&quot;) 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(40, 44, 52, 1); border-radius: 5px">Label:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;time<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text:&nbsp;<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'xxxx'</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font_size:&nbsp;60<br></code></pre>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); letter-spacing: 1px">id为time,text文本内容为'xxxx',然后字体为60
好吧,点到为止吧,不过似乎什么都没点到。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://gitee.com/codexiaosheng/md-pictures/raw/master/2020-8-29/1598715187697-%E5%A4%9A%E5%B9%B3%E5%8F%B0%E5%90%8C%E6%AD%A5%E6%96%87%E6%9C%AB%E5%9B%BE%E7%89%87.png"></figure>
</section>

</div>
<div id="MySignature" role="contentinfo">
    AI视频 AI绘画 AI编程 AI数字人综合学习资料https://pan.quark.cn/s/a43dba5a3b8e<br><br>
来源:https://www.cnblogs.com/codexs/p/13726673.html
頁: [1]
查看完整版本: 新技能:用 Python 写一个安卓 APP