{"id":998872,"date":"2024-12-27T09:36:41","date_gmt":"2024-12-27T01:36:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998872.html"},"modified":"2024-12-27T09:36:45","modified_gmt":"2024-12-27T01:36:45","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8android","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998872.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528android"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074123\/b483f58d-03f0-4930-8329-0806ef5c36a0.webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528android\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528Android\u4e3b\u8981\u901a\u8fc7Kivy\u3001BeeWare\u548cSL4A\u7b49\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u3002<strong>Kivy\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u7684Python\u6846\u67b6\u3001BeeWare\u662f\u4e00\u4e2a\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aAndroid\u5e94\u7528\u7684\u5de5\u5177\u96c6\u3001SL4A\uff08Scripting Layer for Android\uff09\u5141\u8bb8\u5728Android\u8bbe\u5907\u4e0a\u8fd0\u884cPython\u811a\u672c\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Kivy\u521b\u5efaAndroid\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5Kivy\u73af\u5883<\/h4>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u3002\u8981\u5728\u4f60\u7684\u7535\u8111\u4e0a\u5b89\u88c5Kivy\uff0c\u4f60\u9700\u8981\u9996\u5148\u5b89\u88c5Python\uff0c\u7136\u540e\u901a\u8fc7pip\u5b89\u88c5Kivy\u3002\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efaKivy\u5e94\u7528<\/h4>\n<\/p>\n<p><p>Kivy\u5e94\u7528\u7a0b\u5e8f\u7531Python\u811a\u672c\u548cKivy\u8bed\u8a00\u6587\u4ef6\u7ec4\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Hello World\u793a\u4f8b\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, World!&#39;)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u5e94\u7528\u7a0b\u5e8f\uff0c\u663e\u793a\u201cHello, World!\u201d\u3002<\/p>\n<\/p>\n<p><h4>3. \u5c06Kivy\u5e94\u7528\u6253\u5305\u4e3aAPK<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Android\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u4f60\u9700\u8981\u5c06Kivy\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u4e3aAPK\u6587\u4ef6\u3002\u8fd9\u901a\u5e38\u901a\u8fc7Buildozer\u6765\u5b9e\u73b0\u3002\u9996\u5148\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\u521b\u5efa\u4e00\u4e2a<code>buildozer.spec<\/code>\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5728\u5176\u4e2d\u914d\u7f6e\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u63a5\u4e0b\u6765\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u6253\u5305\u4f60\u7684\u5e94\u7528\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>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2aAPK\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5728Android\u8bbe\u5907\u4e0a\u5b89\u88c5\u548c\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeeWare\u521b\u5efaAndroid\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5BeeWare\u5de5\u5177<\/h4>\n<\/p>\n<p><p>BeeWare\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u3001\u90e8\u7f72\u548c\u6253\u5305Python\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u5177\u96c6\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5BeeWare\u7684Toga\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684GUI\u5de5\u5177\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install toga<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efaBeeWare\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528BeeWare\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import toga<\/p>\n<p>from toga.style import Pack<\/p>\n<p>from toga.style.pack import COLUMN, CENTER<\/p>\n<p>def build(app):<\/p>\n<p>    return toga.Box()<\/p>\n<p>def main():<\/p>\n<p>    return toga.App(&#39;Hello World&#39;, &#39;org.beeware.helloworld&#39;, startup=build)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app = main()<\/p>\n<p>    app.main_loop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6253\u5305\u4e3aAndroid\u5e94\u7528<\/h4>\n<\/p>\n<p><p>BeeWare\u63d0\u4f9b\u4e86Briefcase\u5de5\u5177\u6765\u6253\u5305\u5e94\u7528\u3002\u9996\u5148\u5b89\u88c5Briefcase\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install briefcase<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521d\u59cb\u5316\u548c\u6253\u5305\u4f60\u7684\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">briefcase create android<\/p>\n<p>briefcase build android<\/p>\n<p>briefcase run android<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SL4A\u8fd0\u884cPython\u811a\u672c<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5SL4A<\/h4>\n<\/p>\n<p><p>SL4A\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728Android\u8bbe\u5907\u4e0a\u8fd0\u884cPython\u811a\u672c\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u4f60\u9700\u8981\u5728\u8bbe\u5907\u4e0a\u5b89\u88c5SL4A\u5e94\u7528\u548cPython for Android\u63d2\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684Android\u8bbe\u5907\u4e0a\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u4f8b\u5982\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, Android!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7SL4A\u5e94\u7528\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5e76\u8fd0\u884c\u4f60\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528Android\u7684\u6700\u4f73\u65b9\u5f0f\u53d6\u51b3\u4e8e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u548c\u6280\u672f\u504f\u597d\u3002<strong>Kivy\u9002\u5408\u5feb\u901f\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u3001BeeWare\u63d0\u4f9b\u4e86\u66f4\u63a5\u8fd1\u539f\u751f\u7684\u5e94\u7528\u4f53\u9a8c\u3001SL4A\u5219\u9002\u5408\u7b80\u5355\u7684\u811a\u672c\u9700\u6c42\u3002<\/strong>\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u65f6\u9700\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u5de5\u5177\u548c\u6846\u67b6\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u5c06Python\u4e0eAndroid\u7ed3\u5408\uff0c\u5f00\u53d1\u51fa\u529f\u80fd\u4e30\u5bcc\u7684\u79fb\u52a8\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u5982\u4f55\u4e0eAndroid\u8bbe\u5907\u8fdb\u884c\u4ea4\u4e92\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4e0eAndroid\u8bbe\u5907\u8fdb\u884c\u4ea4\u4e92\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Kivy\u6216BeeWare\u7b49\u6846\u67b6\uff0c\u8fd9\u4e9b\u6846\u67b6\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528Python\u7f16\u5199\u8de8\u5e73\u53f0\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u6b64\u5916\uff0cPySide\u548cPyQt\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u4fbf\u4e8e\u4e0eAndroid\u5e94\u7528\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u8bbf\u95eeAndroid\u7684API\uff0c\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u751a\u81f3\u5229\u7528\u8bbe\u5907\u7684\u4f20\u611f\u5668\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5f00\u53d1Android\u5e94\u7528\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u5de5\u5177\uff1f<\/strong><br \/>\u8981\u5728Android\u4e0a\u4f7f\u7528Python\u5f00\u53d1\u5e94\u7528\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u57fa\u672c\u5de5\u5177\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981Python\u89e3\u91ca\u5668\u548c\u76f8\u5173\u7684\u5e93\uff0c\u5982Kivy\u6216BeeWare\u3002\u5176\u6b21\uff0cAndroid SDK\u548cNDK\u4e5f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u4ee5\u4fbf\u80fd\u591f\u7f16\u8bd1\u548c\u6253\u5305\u5e94\u7528\u7a0b\u5e8f\u3002\u6b64\u5916\uff0c\u5b89\u88c5\u4e00\u4e2a\u5408\u9002\u7684IDE\uff08\u5982PyCharm\u6216VS Code\uff09\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u9ad8\u6548\u5730\u7f16\u5199\u4ee3\u7801\u548c\u8c03\u8bd5\u3002<\/p>\n<p><strong>Python\u5728Android\u5f00\u53d1\u4e2d\u7684\u6027\u80fd\u8868\u73b0\u5982\u4f55\uff1f<\/strong><br \/>Python\u5728Android\u5f00\u53d1\u4e2d\u7684\u6027\u80fd\u8868\u73b0\u901a\u5e38\u4e0d\u5982Java\u6216Kotlin\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u8bed\u8a00\u662f\u4e3aAndroid\u5e73\u53f0\u91cf\u8eab\u5b9a\u5236\u7684\u3002\u7136\u800c\uff0c\u4f7f\u7528Kivy\u7b49\u6846\u67b6\u65f6\uff0c\u6027\u80fd\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u548c\u4f7f\u7528Cython\u8fdb\u884c\u63d0\u5347\u3002\u5bf9\u4e8e\u4e0d\u9700\u8981\u6781\u9ad8\u6027\u80fd\u7684\u5e94\u7528\uff0cPython\u4ecd\u7136\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5c24\u5176\u9002\u5408\u5feb\u901f\u539f\u578b\u5f00\u53d1\u548c\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528Android\u4e3b\u8981\u901a\u8fc7Kivy\u3001BeeWare\u548cSL4A\u7b49\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u3002Kivy\u662f\u4e00\u79cd\u7528 [&hellip;]","protected":false},"author":3,"featured_media":998876,"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\/998872"}],"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=998872"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998872\/revisions"}],"predecessor-version":[{"id":998877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998872\/revisions\/998877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998876"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}