当前位置:   article > 正文

6.3 Android 打包_使用Buildozer打包成apk_buildozer打包apk

buildozer打包apk

        在 Linux 环境下通过 Buildozer、 Python-for-Android 和 Kivy Launcher 等方面绍如写好的程序打包为在 Android 环境下可执行的文件。

6.3.1 使用 Buildozer 软件打包为 apk【我用的方式】

        使用 Buildozer 可以自动构建整个打包的过程,该软件会自动下载并设置 python-for-android的必备组件,包括 Android SDK 和 NDK,然后构建一个可自动推送到设备的 apk,这也是官方首推新使用的一种方式。
        安装 命令:

  1. git clone https://github.com/kivy/buildozer.git
  2. cd buildozer
  3. sudo python setup.py install

pip3 install --user --upgrade buildozer

        完成后还需要按安装Buildozer的依赖环境。如:Ubuntu18(64bit)可以执行一下命令

  1. sudo apt update
  2. sudo apt install -y git zip unzip openjdk-8-jdk python3-pip autoconf libtool pkg-configzlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev
  3. pip3 install --user --upgrade cython virtualenv # 如果在虚拟环境中把 --user删除
  4. # 把下面这一行添加到 ~/.bashrc文件最后一行
  5. export PATH=$PATH:~/.local/bin/

        安装成功后,通过cd命令进入到需要打包项目的同级目录中,执行以下命令生成buildozer.spec配置文件:

buildozer init

参考以下参数编辑 buildozer.spec 配置文件:

【由于使用文字翻译的照片,可能有符号是中文输入法的情况,仅供参考】

  1. [app]
  2. # 以下为通用配置
  3. # 应用名称
  4. title = 51斩百词
  5. # 包名
  6. package.name = myapp
  7. #域名
  8. package.domain = com.cocpy
  9. # main.py文件所在的位置,由于该配置文件是在项目内生成的(即同级目录),故设置为“.”
  10. source.dir = .
  11. # 需要打包进来的文件的扩展名
  12. source,include_exts = py,kv,atlas,db,sql,ttf,png,jpg,docx,xml,mp3
  13. # 匹配列表
  14. # source,include_patterns = files/*,image/*
  15. # 不需要打包进来的文件的扩展名
  16. #source.exclude_exts = spec
  17. # 排除的目录
  18. #source.exclude_dirs = tests, bin
  19. #排除的列表
  20. #source.exclude_patterns = license,images/*/*.jpg
  21. # 应用版本(写法一)
  22. version = 1.0.0
  23. # 应用版本(写法二)
  24. # version.regex = __version__ = ['"](.*)['"]
  25. # version.filename = %(source.dir)s/main.py
  26. # 应用依赖包,很重要,大部分闪退都是因为具体的依赖未打包进来
  27. # comma separated e.g. requirements = sqlite3,kivy
  28. requirements = python3,kivy
  29. # 自定义源文件
  30. # Sets custom source for any requirements with recipes
  31. # requirements.source.kivy = ../../kivy
  32. # 通过garden安装的包
  33. garden_requirements = navigationdrawer,matplotlib
  34. # 预加载图片文件
  35. #presplash.filename = %(source.dir)s/data/presplash.png
  36. # 应用图标
  37. icon.filename = %(source.dir)s/image/CCWord.png
  38. #应用支持的方向(landscape, sensorLandscape,portrait or all)
  39. orientation = portrait
  40. # 声明的服务列表
  41. #services = NAME:ENTRYPOINT_TO_PYNAME2:ENTRYPOINT2_TO_PY
  42. #
  43. # OSX Specific
  44. #
  45. # OSX配智
  46. # author = @ Copyright Info
  47. # change the major version of python used by the app
  48. osx.python_version = 3
  49. # kivy version to use
  50. osx.kivy_version = 1.9.1
  51. #
  52. #Android specific
  53. #
  54. # Android配置
  55. #全屏显示1,非全屏0
  56. fullscreen = 1
  57. # (string) Presplash background color (for new android toolchain)
  58. # Supported formats are: #RRGGBB #AARRGGBB or one of the following names:
  59. # red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray
  60. # darkgray, grey,lightgrey, darkgrey,aqua, fuchsia, lime, maroon, navy
  61. # olive,purple,silver,teal。预加载的颜色
  62. # android.presplash_color = white
  63. #申请的权限
  64. android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE
  65. #支持的Android API版本,越高兼容性越好
  66. #android.api = 27
  67. # 支持最低的Android API版本,越低越好
  68. #android.minapi = 21
  69. # 使用的Android SDK版本
  70. #android.sdk = 20
  71. #使用的Androld NDK版本
  72. #androld.ndk = 17c
  73. #支持最低的NDK版本
  74. #android.ndk_ap1 = 21
  75. # 使用私有存储(True)or公共存储(False)
  76. #android.private_storage = True
  77. # Android NDK路径
  78. #android.ndk_path =
  79. # Android SDK路径
  80. #android.sdk_path =
  81. # ANT路径,为空会自动下载
  82. #android.ant_path =
  83. #是否跳过更新SDK
  84. # android.skip_update = False
  85. #是否自动接受SDK协议
  86. # android.accept_sdk_license = False
  87. #默认入口
  88. #android.entrypoint = org.renpy.android.PythonActivity
  89. # 项目白名单
  90. #android,whitelist =
  91. # 白名单路径
  92. #android.whitelist_src =
  93. # 里名单路径
  94. #android.blacklist_src =
  95. # (list) List of Java .jar files to add to the libs so that pyjnius can access
  96. # their classes. Don't add jars that you do not need, since extra jars can slow
  97. # down the build process. Allows wildcards matching, for example:
  98. # OUYA-ODK/libs/*.jar添加的Android jar包
  99. #android.add_jars = foo.jar,bar.jar,path/to/more/*.jar
  100. # 使用的Java文件
  101. #android.add_src =
  102. #添加AndroidAAR文档(目前只适用于sd12_gradle bootstrap)
  103. #android.add aars =
  104. # 添加Gradle依赖
  105. #android.gradle_dependencies =
  106. # 添加的Java类
  107. #android.add_activites = com,example.ExampleActivity
  108. #使用p4a的版本,默认master
  109. p4a.branch = master
  110. #控制台类别 应用或者游戏
  111. #android.ouya.category = GAME
  112. #控制台图标文件名称。必须是732x412的PNG图片
  113. #android.ouya.icon.filename = %(source.dir)s/data/ouya_icon.png
  114. #XML文件过滤器
  115. #android.manifest.intent_filters =
  116. #设置启动模式为主活动
  117. #android.manifest.launch_mode = standard
  118. # Android附加库复制进libs/armeabi
  119. #android.add_libs_armeabi = libs/android/*.so
  120. #android.add_libsarmeabi_v7a= ibs/android-v7/*.so
  121. #android.add_libs_x86 = libs/android-x86/*.so
  122. #android.add_libs_mips = libs/android-mips/*.so
  123. # 屏幕是否常亮
  124. # Don't forget to add the WAKE_LOCK permission if you set this to True
  125. #android.wakelock = False
  126. # (list) Android应用程序元数据设置 (key=value format)
  127. #android.meta_data =
  128. #要添加的Android库
  129. #android.library_references =
  130. # Android日志过滤
  131. #android.logcat_filters = *:S python:D
  132. # 用复制库取代生成libpymodules.so
  133. #android.copy_libs = 1
  134. #主更构建的Android架构:armeabi-v7a,arm64-v8a,x86
  135. android.arch = armeabi-v7a
  136. #
  137. # Python for androld (p4a) specific
  138. #
  139. #p4a特性
  140. #干载python-for-android路径,为空默认从git下载
  141. #p4a.sourcedire
  142. #本地的构建方法
  143. #p4a.local_recipes =
  144. #p4a相关文件
  145. #p4a.hook =
  146. # 用于构建Android的Bootstrap
  147. # p4a.bootstrap = sdl2
  148. #p4a端口号
  149. #p4a.port =
  150. #
  151. # i0S specific
  152. #
  153. #iOS特性
  154. # (str) Path to a custom kivy-ios folder
  155. #ios.kivy_ios_dir= ../kivy-ios
  156. # Alternately, specify the URL and branch of a git checkout:
  157. ios.kivy_ios_url= https://github.com/kivy/kivy-ios
  158. ios,kivy_ios_branch = master
  159. #Another platform dependency: ios-deploy
  160. # Uncomment to use a custom checkout
  161. #ios.ios_deploydir = ../ios_deploy
  162. # Or specify URL and branch
  163. ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
  164. ios.ios_deploy_branch = 1.7.0
  165. # (str) Name of the certificate to use for signing the debug version
  166. # Get a list of available identities: buildozer ios list_identities
  167. #ios,codesign.debug = "iPhone Developer: <lastname> <firstname> (<hexstring>)"
  168. # (str) Name of the certificate to use for signing the release version
  169. #ios.codesign.release = %(ios,codesign.debug)s
  170. [buildozer]
  171. # (int) Log level ( 0 = error only, 1 = info, 2 = debug (with command output))
  172. log_level = 2
  173. # (int) Display warning if buildozer is run as root (0 = False, 1 = True)
  174. warn_on_root = 1
  175. # (str) Path to build artifact storage, absolute or relative to spec file
  176. # build dir = ./.buildozer
  177. #(str) Path to build output (i.e..apk,.ipa) storage
  178. #bindir=./bin
  179. # ---------------------------------------------------------------------------
  180. # List as sections
  181. #
  182. # You can define all the "list” as [section:key].
  183. # Each line will be considered as a option to the list.
  184. # Let's take [app] / source.exclude_patterns.
  185. # Instead of doing:
  186. #
  187. # [app]
  188. # source.exclude_patterns = license,data/audio/*.wav,data/images/original/*
  189. # This can be translated into:
  190. #
  191. #[app:source.exclude_patterns]
  192. #license
  193. #data/audio/*.wav
  194. #data/images/original/*
  195. #
  196. # ----------------------------------------------------------------------------
  197. Profiles
  198. #
  199. #
  200. # You can extend section / key with a profile
  201. # For example, you want to deploy a demo version of your application without
  202. # HD content. You could first change the title to add "(demo)" in the name
  203. # and extend the excluded directories to remove the HD content.
  204. #
  205. #[app@demo]
  206. #title = My Application (demo)
  207. #
  208. # [app:source.exclude_patterns@demo]
  209. # images/hd/*
  210. #
  211. # Then, invoke the command line with the "demo" profile:
  212. #buildozer --profile demo android debug

在项目中先添加的依赖包一定要写到requirements中去,再通过以下命令生成一个调试版本的apk

buildozer -v android debug

首次打包过程会很慢,需要耐心等待。打包完成后会在项目的bin目录下生成一个apk文件。

参考官方链接:https://buildozer.readthedocs.io/en/latest

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/664900
推荐阅读
相关标签
  

闽ICP备14008679号