{"id":933170,"date":"2024-12-26T18:11:45","date_gmt":"2024-12-26T10:11:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933170.html"},"modified":"2024-12-26T18:11:46","modified_gmt":"2024-12-26T10:11:46","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90android","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933170.html","title":{"rendered":"Python\u5982\u4f55\u751f\u6210android"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070724\/86f5e535-1e4e-4f45-a2bd-da7db79403b3.webp\" alt=\"Python\u5982\u4f55\u751f\u6210android\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210Android\u5e94\u7528\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\u4f7f\u7528Kivy\u6846\u67b6\u3001BeeWare\u5de5\u5177\u5305\u4ee5\u53caSL4A\uff08Scripting Layer for Android\uff09\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u4f18\u7f3a\u70b9\uff0cKivy\u9002\u5408\u8de8\u5e73\u53f0\u5e94\u7528\u3001BeeWare\u4e13\u6ce8\u4e8e\u539f\u751f\u5e94\u7528\u5f00\u53d1\u3001SL4A\u5219\u9002\u7528\u4e8e\u811a\u672c\u7ea7\u522b\u7684\u4efb\u52a1\u3002<\/strong>\u5728\u8fd9\u4e9b\u5de5\u5177\u4e2d\uff0cKivy\u56e0\u5176\u5f3a\u5927\u7684\u8de8\u5e73\u53f0\u80fd\u529b\u548c\u793e\u533a\u652f\u6301\u6210\u4e3a\u5f00\u53d1\u8005\u7684\u70ed\u95e8\u9009\u62e9\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u751f\u6210Android\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001KIVY\u6846\u67b6<\/p>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u652f\u6301Windows\u3001Linux\u3001iOS\u3001Android\u7b49\u591a\u4e2a\u5e73\u53f0\uff0c\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u5f00\u53d1\u7684\u597d\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u914d\u7f6eKivy<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5\u4e86Python\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u5b89\u88c5Kivy\u548c\u4e00\u4e9b\u4f9d\u8d56\u9879\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5efa\u8bae\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u5e94\u7528\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\u6587\u4ef6\uff0c\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Label(text=&#39;Hello, Kivy!&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Buildozer\u751f\u6210Android APK<\/li>\n<\/ol>\n<p><p>Kivy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aBuildozer\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5c06Kivy\u5e94\u7528\u6253\u5305\u6210Android APK\u6587\u4ef6\u3002\u9996\u5148\uff0c\u5b89\u88c5Buildozer\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install buildozer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u521d\u59cb\u5316Buildozer\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">buildozer init<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a<code>buildozer.spec<\/code>\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5728\u5176\u4e2d\u914d\u7f6e\u4f60\u7684\u5e94\u7528\u3002\u4e4b\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210APK\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">buildozer -v android debug<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u7684APK\u6587\u4ef6\u4f1a\u5728<code>bin<\/code>\u76ee\u5f55\u4e0b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001BEEWARE\u5de5\u5177\u5305<\/p>\n<\/p>\n<p><p>BeeWare\u662f\u4e00\u7ec4\u5de5\u5177\u548c\u5e93\uff0c\u65e8\u5728\u5e2e\u52a9\u5f00\u53d1\u8005\u4f7f\u7528Python\u7f16\u5199\u539f\u751f\u7528\u6237\u754c\u9762\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5BeeWare<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5BeeWare\u5de5\u5177\u5305\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beeware<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u548c\u8fd0\u884c\u9879\u76ee<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528BeeWare\u7684<code>briefcase<\/code>\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">briefcase new<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6839\u636e\u63d0\u793a\u8f93\u5165\u9879\u76ee\u4fe1\u606f\uff0c\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd MyApplication<\/p>\n<p>briefcase dev<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u542f\u52a8\u4e00\u4e2a\u5f00\u53d1\u670d\u52a1\u5668\uff0c\u4f60\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u67e5\u770b\u5e94\u7528\u6548\u679c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6253\u5305\u4e3aAndroid\u5e94\u7528<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5e94\u7528\u6253\u5305\u4e3aAndroid\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">briefcase build android<\/p>\n<p>briefcase run android<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u53ef\u5b89\u88c5\u7684APK\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001SL4A\uff08Scripting Layer for Android\uff09<\/p>\n<\/p>\n<p><p>SL4A\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528Python\u811a\u672c\u76f4\u63a5\u5728Android\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5f00\u53d1\u548c\u6d4b\u8bd5\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u914d\u7f6eSL4A<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4e0b\u8f7d\u5e76\u5b89\u88c5SL4A\u548cPython for Android\u3002SL4A\u63d0\u4f9b\u4e86\u4e00\u4e2a\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7\u811a\u672c\u8bbf\u95eeAndroid\u8bbe\u5907\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u7f16\u5199\u548c\u8fd0\u884c\u811a\u672c<\/li>\n<\/ol>\n<p><p>\u4e00\u65e6\u5b89\u88c5\u5b8c\u6210\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199Python\u811a\u672c\uff0c\u5e76\u901a\u8fc7SL4A\u5728\u8bbe\u5907\u4e0a\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>hello.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import androidhelper<\/p>\n<p>droid = androidhelper.Android()<\/p>\n<p>droid.makeToast(&#39;Hello from Python!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u811a\u672c\u4e0a\u4f20\u5230\u8bbe\u5907\uff0c\u5e76\u901a\u8fc7SL4A\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u751f\u6210Android\u5e94\u7528\u5177\u6709\u4e00\u5b9a\u7684\u6311\u6218\u6027\uff0c\u4f46\u901a\u8fc7Kivy\u3001BeeWare\u548cSL4A\u7b49\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6848\u3002<strong>Kivy\u9002\u5408\u8de8\u5e73\u53f0\u5e94\u7528\u5f00\u53d1\uff0cBeeWare\u5219\u662f\u539f\u751f\u5e94\u7528\u7684\u597d\u9009\u62e9\uff0cSL4A\u9002\u5408\u5feb\u901f\u539f\u578b\u548c\u811a\u672c\u5f00\u53d1\u3002<\/strong>\u5728\u9009\u62e9\u5de5\u5177\u65f6\uff0c\u5f00\u53d1\u8005\u5e94\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u81ea\u8eab\u6280\u80fd\u6c34\u5e73\u505a\u51fa\u51b3\u5b9a\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u4e2a\u5de5\u5177\uff0cPython\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u5e93\u652f\u6301\u90fd\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9e\u73b0\u60f3\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1Android\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5f00\u53d1Android\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6846\u67b6\u548c\u5de5\u5177\u5b9e\u73b0\uff0c\u4f8b\u5982Kivy\u3001BeeWare\u548cPySide\u3002Kivy\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5f00\u6e90Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\uff0c\u652f\u6301\u89e6\u6478\u4e8b\u4ef6\u548c\u591a\u70b9\u89e6\u63a7\u3002BeeWare\u5219\u63d0\u4f9b\u4e86\u4e00\u5957\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u6253\u5305\u4e3a\u539f\u751fAndroid\u5e94\u7528\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5229\u7528Python\u7684\u7b80\u6d01\u8bed\u6cd5\u548c\u4e30\u5bcc\u7684\u5e93\u751f\u6001\u6765\u521b\u5efaAndroid\u5e94\u7528\u3002<\/p>\n<p><strong>Python\u751f\u6210Android\u5e94\u7528\u7684\u6027\u80fd\u5982\u4f55\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5f00\u53d1\u7684Android\u5e94\u7528\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u539f\u751fJava\u6216Kotlin\u5e94\u7528\u3002\u7531\u4e8ePython\u662f\u89e3\u91ca\u578b\u8bed\u8a00\uff0c\u67d0\u4e9b\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u53ef\u80fd\u4f1a\u8f83\u6162\u3002\u7136\u800c\uff0cKivy\u548c\u5176\u4ed6\u6846\u67b6\u5728\u6027\u80fd\u4f18\u5316\u4e0a\u63d0\u4f9b\u4e86\u8bb8\u591a\u652f\u6301\uff0c\u6bd4\u5982\u4f7f\u7528Cython\u7f16\u8bd1\u90e8\u5206\u4ee3\u7801\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u5bf9\u4e8e\u4e0d\u9700\u8981\u6781\u9ad8\u6027\u80fd\u8981\u6c42\u7684\u5e94\u7528\uff0cPython\u4ecd\u7136\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5f00\u53d1Android\u5e94\u7528\u65f6\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u548c\u73af\u5883\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728Android\u5e73\u53f0\u4e0a\u4f7f\u7528Python\u8fdb\u884c\u5f00\u53d1\uff0c\u5f00\u53d1\u8005\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5173\u952e\u5de5\u5177\u548c\u73af\u5883\u3002\u5e38\u89c1\u7684\u5305\u62ecPython\u73af\u5883\uff08\u5efa\u8bae\u4f7f\u75283.x\u7248\u672c\uff09\u3001Kivy\u6216BeeWare\u6846\u67b6\u3001Android SDK\u4ee5\u53ca\u76f8\u5e94\u7684\u6784\u5efa\u5de5\u5177\u3002\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u914d\u7f6e\u597d\u8fd9\u4e9b\u7ec4\u4ef6\uff0c\u80fd\u5e2e\u52a9\u987a\u5229\u8fdb\u884c\u5f00\u53d1\u548c\u8c03\u8bd5\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u73b0\u6709\u7684Python\u9879\u76ee\u8f6c\u6362\u4e3aAndroid\u5e94\u7528\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5c06\u73b0\u6709\u7684Python\u9879\u76ee\u8f6c\u6362\u4e3aAndroid\u5e94\u7528\u3002\u4f7f\u7528Kivy\u6216BeeWare\u7b49\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u6253\u5305\u4e3aAndroid APK\u6587\u4ef6\u3002\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u7528\u6237\u754c\u9762\u548c\u67d0\u4e9b\u529f\u80fd\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u786e\u4fdd\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u7684\u826f\u597d\u4f53\u9a8c\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u76f8\u5bf9\u8f7b\u677e\u5730\u5c06\u684c\u9762\u5e94\u7528\u8fc1\u79fb\u5230Android\u5e73\u53f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210Android\u5e94\u7528\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\u4f7f\u7528Kivy\u6846\u67b6\u3001BeeWare\u5de5\u5177\u5305\u4ee5\u53caSL4A\uff08Script [&hellip;]","protected":false},"author":3,"featured_media":933175,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933170"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=933170"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933170\/revisions"}],"predecessor-version":[{"id":933177,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933170\/revisions\/933177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933175"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}