赞
踩
在 Linux 环境下通过 Buildozer、 Python-for-Android 和 Kivy Launcher 等方面绍如写好的程序打包为在 Android 环境下可执行的文件。
6.3.1 使用 Buildozer 软件打包为 apk【我用的方式】
使用 Buildozer 可以自动构建整个打包的过程,该软件会自动下载并设置 python-for-android的必备组件,包括 Android SDK 和 NDK,然后构建一个可自动推送到设备的 apk,这也是官方首推新使用的一种方式。
安装 命令:
- git clone https://github.com/kivy/buildozer.git
- cd buildozer
- sudo python setup.py install
或
pip3 install --user --upgrade buildozer
完成后还需要按安装Buildozer的依赖环境。如:Ubuntu18(64bit)可以执行一下命令
- sudo apt update
- 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
- pip3 install --user --upgrade cython virtualenv # 如果在虚拟环境中把 --user删除
- # 把下面这一行添加到 ~/.bashrc文件最后一行
- export PATH=$PATH:~/.local/bin/
安装成功后,通过cd命令进入到需要打包项目的同级目录中,执行以下命令生成buildozer.spec配置文件:
buildozer init
参考以下参数编辑 buildozer.spec 配置文件:
【由于使用文字翻译的照片,可能有符号是中文输入法的情况,仅供参考】
- [app]
- # 以下为通用配置
- # 应用名称
- title = 51斩百词
- # 包名
- package.name = myapp
-
- #域名
- package.domain = com.cocpy
-
- # main.py文件所在的位置,由于该配置文件是在项目内生成的(即同级目录),故设置为“.”
- source.dir = .
-
- # 需要打包进来的文件的扩展名
- source,include_exts = py,kv,atlas,db,sql,ttf,png,jpg,docx,xml,mp3
-
- # 匹配列表
- # source,include_patterns = files/*,image/*
-
- # 不需要打包进来的文件的扩展名
- #source.exclude_exts = spec
-
- # 排除的目录
- #source.exclude_dirs = tests, bin
-
- #排除的列表
- #source.exclude_patterns = license,images/*/*.jpg
-
-
-
- # 应用版本(写法一)
- version = 1.0.0
-
- # 应用版本(写法二)
- # version.regex = __version__ = ['"](.*)['"]
- # version.filename = %(source.dir)s/main.py
-
- # 应用依赖包,很重要,大部分闪退都是因为具体的依赖未打包进来
- # comma separated e.g. requirements = sqlite3,kivy
- requirements = python3,kivy
-
- # 自定义源文件
- # Sets custom source for any requirements with recipes
- # requirements.source.kivy = ../../kivy
-
- # 通过garden安装的包
- garden_requirements = navigationdrawer,matplotlib
-
- # 预加载图片文件
- #presplash.filename = %(source.dir)s/data/presplash.png
- # 应用图标
- icon.filename = %(source.dir)s/image/CCWord.png
-
-
- #应用支持的方向(landscape, sensorLandscape,portrait or all)
- orientation = portrait
-
- # 声明的服务列表
- #services = NAME:ENTRYPOINT_TO_PYNAME2:ENTRYPOINT2_TO_PY
-
- #
- # OSX Specific
- #
-
- # OSX配智
- # author = @ Copyright Info
-
- # change the major version of python used by the app
- osx.python_version = 3
-
- # kivy version to use
- osx.kivy_version = 1.9.1
-
- #
- #Android specific
- #
- # Android配置
- #全屏显示1,非全屏0
- fullscreen = 1
-
- # (string) Presplash background color (for new android toolchain)
- # Supported formats are: #RRGGBB #AARRGGBB or one of the following names:
- # red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray
- # darkgray, grey,lightgrey, darkgrey,aqua, fuchsia, lime, maroon, navy
- # olive,purple,silver,teal。预加载的颜色
- # android.presplash_color = white
-
- #申请的权限
- android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE
-
- #支持的Android API版本,越高兼容性越好
- #android.api = 27
-
- # 支持最低的Android API版本,越低越好
- #android.minapi = 21
-
- # 使用的Android SDK版本
- #android.sdk = 20
-
-
-
- #使用的Androld NDK版本
- #androld.ndk = 17c
-
- #支持最低的NDK版本
- #android.ndk_ap1 = 21
-
- # 使用私有存储(True)or公共存储(False)
- #android.private_storage = True
-
- # Android NDK路径
- #android.ndk_path =
-
- # Android SDK路径
- #android.sdk_path =
-
- # ANT路径,为空会自动下载
- #android.ant_path =
-
- #是否跳过更新SDK
- # android.skip_update = False
-
- #是否自动接受SDK协议
- # android.accept_sdk_license = False
-
- #默认入口
- #android.entrypoint = org.renpy.android.PythonActivity
-
- # 项目白名单
- #android,whitelist =
-
- # 白名单路径
- #android.whitelist_src =
-
- # 里名单路径
- #android.blacklist_src =
-
- # (list) List of Java .jar files to add to the libs so that pyjnius can access
- # their classes. Don't add jars that you do not need, since extra jars can slow
- # down the build process. Allows wildcards matching, for example:
- # OUYA-ODK/libs/*.jar添加的Android jar包
- #android.add_jars = foo.jar,bar.jar,path/to/more/*.jar
-
- # 使用的Java文件
- #android.add_src =
-
-
-
- #添加AndroidAAR文档(目前只适用于sd12_gradle bootstrap)
- #android.add aars =
-
- # 添加Gradle依赖
- #android.gradle_dependencies =
-
- # 添加的Java类
- #android.add_activites = com,example.ExampleActivity
-
- #使用p4a的版本,默认master
- p4a.branch = master
-
- #控制台类别 应用或者游戏
- #android.ouya.category = GAME
-
- #控制台图标文件名称。必须是732x412的PNG图片
- #android.ouya.icon.filename = %(source.dir)s/data/ouya_icon.png
-
- #XML文件过滤器
- #android.manifest.intent_filters =
-
- #设置启动模式为主活动
- #android.manifest.launch_mode = standard
-
- # Android附加库复制进libs/armeabi
- #android.add_libs_armeabi = libs/android/*.so
- #android.add_libsarmeabi_v7a= ibs/android-v7/*.so
- #android.add_libs_x86 = libs/android-x86/*.so
- #android.add_libs_mips = libs/android-mips/*.so
-
- # 屏幕是否常亮
- # Don't forget to add the WAKE_LOCK permission if you set this to True
- #android.wakelock = False
-
- # (list) Android应用程序元数据设置 (key=value format)
- #android.meta_data =
-
- #要添加的Android库
- #android.library_references =
-
- # Android日志过滤
- #android.logcat_filters = *:S python:D
-
- # 用复制库取代生成libpymodules.so
- #android.copy_libs = 1
-
-
- #主更构建的Android架构:armeabi-v7a,arm64-v8a,x86
- android.arch = armeabi-v7a
-
- #
- # Python for androld (p4a) specific
- #
- #p4a特性
- #干载python-for-android路径,为空默认从git下载
- #p4a.sourcedire
-
- #本地的构建方法
- #p4a.local_recipes =
-
- #p4a相关文件
- #p4a.hook =
-
- # 用于构建Android的Bootstrap
- # p4a.bootstrap = sdl2
-
- #p4a端口号
- #p4a.port =
-
- #
- # i0S specific
- #
- #iOS特性
- # (str) Path to a custom kivy-ios folder
- #ios.kivy_ios_dir= ../kivy-ios
- # Alternately, specify the URL and branch of a git checkout:
- ios.kivy_ios_url= https://github.com/kivy/kivy-ios
- ios,kivy_ios_branch = master
-
-
- #Another platform dependency: ios-deploy
- # Uncomment to use a custom checkout
- #ios.ios_deploydir = ../ios_deploy
- # Or specify URL and branch
- ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
- ios.ios_deploy_branch = 1.7.0
-
- # (str) Name of the certificate to use for signing the debug version
- # Get a list of available identities: buildozer ios list_identities
- #ios,codesign.debug = "iPhone Developer: <lastname> <firstname> (<hexstring>)"
-
-
-
-
-
- # (str) Name of the certificate to use for signing the release version
- #ios.codesign.release = %(ios,codesign.debug)s
-
- [buildozer]
-
- # (int) Log level ( 0 = error only, 1 = info, 2 = debug (with command output))
- log_level = 2
-
- # (int) Display warning if buildozer is run as root (0 = False, 1 = True)
- warn_on_root = 1
-
- # (str) Path to build artifact storage, absolute or relative to spec file
- # build dir = ./.buildozer
-
- #(str) Path to build output (i.e..apk,.ipa) storage
- #bindir=./bin
-
- # ---------------------------------------------------------------------------
- # List as sections
- #
- # You can define all the "list” as [section:key].
- # Each line will be considered as a option to the list.
- # Let's take [app] / source.exclude_patterns.
- # Instead of doing:
- #
- # [app]
- # source.exclude_patterns = license,data/audio/*.wav,data/images/original/*
- # This can be translated into:
- #
- #[app:source.exclude_patterns]
- #license
- #data/audio/*.wav
- #data/images/original/*
- #
-
- # ----------------------------------------------------------------------------
- Profiles
- #
- #
-
- # You can extend section / key with a profile
- # For example, you want to deploy a demo version of your application without
- # HD content. You could first change the title to add "(demo)" in the name
- # and extend the excluded directories to remove the HD content.
- #
- #[app@demo]
- #title = My Application (demo)
- #
- # [app:source.exclude_patterns@demo]
- # images/hd/*
- #
- # Then, invoke the command line with the "demo" profile:
-
- #buildozer --profile demo android debug
在项目中先添加的依赖包一定要写到requirements中去,再通过以下命令生成一个调试版本的apk
buildozer -v android debug
首次打包过程会很慢,需要耐心等待。打包完成后会在项目的bin目录下生成一个apk文件。
参考官方链接:https://buildozer.readthedocs.io/en/latest
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。