#anaconda3 @ win10
python --version
#Python 3.8.5
pip install virtualenv
python -m virtualenv kivy_venv
#若virtualenv 报错, 试试:#conda create python=3.8 -n kivy_venv
kivy_venv\Scripts\activate.bat
##: conda activate kivy_venv
which pip
#/home/z//python_root/Miniconda3-4.10.3/envs/kivy_venv/bin/pip
pip install "kivy[dev]" kivy_examples
#dev, base, full 参见: https://kivy.org/doc/stable/gettingstarted/installation.html : "Installing Kivy’s dependencies"
#显示例子
python kivy_venv\share\kivy-examples\demo\showcase\main.py
#centos 8 x64 stream @"win10x64 hyperv"
/home/z/python_root/Miniconda3-4.10.3/bin/activate kivy_venv
#安装buildozer
cd /home/z/
git clone https://github.com/kivy/buildozer.git
cd buildozer
sudo python setup.py install
#"安装buildozer" 这段可以用此替代: pip install buildozer
cd /home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/pictures/
buildozer init
#安装buildozer所需依赖
#https://buildozer.readthedocs.io/en/latest/installation.html#targeting-android
export JAVA_HOME=/home/z/java_root/jdk-11.0.15+10/
export PATH=$JAVA_HOME/bin:$PATH
buildozer android release
#成功编译出apk: 能在手机上正常运行.
#/home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/pictures/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/dists/myapp/build/outputs/apk/debug/myapp-debug.apk
"""
#centos 8 stream x64 @ "hyperv @win10x64"
buildzer android release
#报错: Cython (cython) not found, please install it
#解决:
#sudo dnf install python38-Cython.x86_64 -y #这句可能不需要
pip install Cyton
which cython
#~/python_root/Miniconda3-4.10.3/envs/kivy_venv/bin/cython
编译python时, 报错 ctypes 找不到,
原因: 编译hostpython3时没有找到合适的libffi:
解决过程:
#报错:
"""
skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/8/../../../libffi.so when searching for -lffi
skipping incompatible //lib/libffi.so when searching for -lffi
skipping incompatible //usr/lib/libffi.so when searching for -lffi
cannot find -lffi
"""
ls -lh /usr/lib64/libffi.so.* #不兼容的so如下
"""
lrwxrwxrwx. 1 root root 26 Jul 27 10:11 /usr/lib/libffi.so -> /usr/lib64/libffi.so.6.0.2
lrwxrwxrwx. 1 root root 15 Jul 27 12:54 /usr/lib/libffi.so.6 -> libffi.so.6.0.2
-rwxr-xr-x. 1 root root 73K Jul 27 10:11 /usr/lib/libffi.so.6.0.2
"""
ls -lh /usr/lib64/libffi.so.* #另一组so如下, (经过后面尝试发现, 这组是兼容的)
"""
lrwxrwxrwx. 1 root root 26 Jul 27 10:11 /usr/lib64/libffi.so.6 -> /usr/lib64/libffi.so.8.1.0
lrwxrwxrwx. 1 root root 15 Jul 27 12:54 /usr/lib64/libffi.so.8 -> libffi.so.8.1.0
-rwxr-xr-x. 1 root root 73K Jul 27 10:11 /usr/lib64/libffi.so.8.1.0
"""
#错误解决:
mkdir /tmp/usr_lib; cp /usr/lib/libffi.so* /tmp/usr_lib#备份
unlink /usr/lib/libffi.so #删掉incompatible 的so
sudo ln -s /usr/lib64/libffi.so.8.1.0 /usr/lib/libffi.so #指向好用的so
#单独编译看下效果: 正常编译了
gcc -pthread -shared build/temp.linux-x86_64-3.9/home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/helloworld/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Modules/_ctypes/_ctypes.o build/temp.linux-x86_64-3.9/home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/helloworld/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Modules/_ctypes/callbacks.o build/temp.linux-x86_64-3.9/home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/helloworld/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Modules/_ctypes/callproc.o build/temp.linux-x86_64-3.9/home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/helloworld/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Modules/_ctypes/cfield.o build/temp.linux-x86_64-3.9/home/z/python_root/Miniconda3-4.10.3/envs/kivy_venv/share/kivy-examples/demo/helloworld/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Modules/_ctypes/stgdict.o -L/usr/local/lib -lffi -ldl -o build/lib.linux-x86_64-3.9/_ctypes.cpython-39-x86_64-linux-gnu.so
> buildozer android release 报错: Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/cython/: There was a problem confirming the ssl
> 原因是pip镜像站是https: https://pypi.tuna.tsinghua.edu.cn/simple
> 改为http的pip镜像站即可: http://mirrors.aliyun.com/pypi/simple/
#buildozer android release 继续报错
"""
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/cython/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/cython/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement Cython (from versions: none)
ERROR: No matching distribution found for Cython
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
#构建好py虚拟环境
conda create python=3.8 -n beeware_venv
conda activate beeware_venv
pip install briefcase
#set JAVA_HOME=D:\jdk1.8.0_111
#创建py项目:
briefcase new
#一路默认 novelz
#novelz/src/novelz/*.py
#如果要用pycharm 打开novelz/src/novelz/, 则应该安装: pip install toga==v0.3.0.dev35
#刚刚创建py项目后, 将py转为android项目:
briefcase create android
#之后修改了py代码, 只需要更新android项目:
briefcase update android
#编译android项目为apk包
briefcase build android
可以在cmd.exe下, 执行 D:\Miniconda3\Scripts\activate.bat , 激活base环境. 再执行conda activate beeware_venv 激活bee
#打开一个cmd窗口执性以下内容:
D:\Miniconda3\Scripts\activate.bat
conda activate beeware_venv
echo %PATH%
#手工找出环境变量PATH中beeware_venv的部分, 这就是下面 git bash中手工激活beeware_venv的脚本
git bash 执行以下命令,手工激活 beeware_venv
beeware_env_path=/d/Miniconda3/envs/beeware_venv:/d/Miniconda3/envs/beeware_venv/Library/mingw-w64/bin:/d/Miniconda3/envs/beeware_venv/Library/usr/bin:/d/Miniconda3/envs/beeware_venv/Library/bin:/d/Miniconda3/envs/beeware_venv/Scripts:/d/Miniconda3/envs/beeware_venv/bin
export PATH=$PATH:$beeware_env_path
briefcase create android
#create步会卡在下载 https://briefcase-support.org/python?platform=android&version=3.8
# 手工下载 放到路径 /home/z/beeware_demo/Python-3.8-Android-support.b5.zip
#修改 /home/z/python_root/Miniconda3-4.10.3/envs/beeware_venv/lib/python3.8/site-packages/briefcase/commands/create.py : install_app_support_package 函数
#将
# support_package_url = app.support_package
#改成:
# support_package_url = "/home/z/beeware_demo/Python-3.8-Android-support.b5.zip"
#再次运行briefcase create android可以通过
#如果 它慢: "Using app template: https://github.com/beeware/briefcase-android-gradle-template.git, branch 3.8"
#修改 /home/z/python_root/Miniconda3-4.10.3/envs/beeware_venv/lib/python3.8/site-packages/briefcase/commands/create.py
""" @property
def app_template_url(self):
"""The URL for a cookiecutter repository to use when creating apps."""
#return f"https://github.com/beeware/briefcase-{self.platform}-{self.output_format}-template.git"
#本行改成 github镜像加速地址:, 即可解决慢问题
return f"https://github.91chi.fun//https://github.com/beeware/briefcase-{self.platform}-{self.output_format}-template.git"
"""
briefcase build android
#运行中
#有以下报错:
"""
/home/z/.briefcase/tools/android_sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip: error:
'/home/z/beeware_weather/weather/android/gradle/weather/app/build/intermediates/merged_native_libs/debug/out/lib/x86_64/libssl.so': The file was
not recognized as a valid object file
/home/z/.briefcase/tools/android_sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip: error:
'/home/z/beeware_weather/weather/android/gradle/weather/app/build/intermediates/merged_native_libs/debug/out/lib/x86_64/libcrypto.so': The file
was not recognized as a valid object file
"""
#解决
cd /home/z/beeware_wth/wth/android/gradle/wth/app/build/intermediates/merged_native_libs/debug/out/lib/armeabi-v7a/
unlink libcrypto.so
unlink libssl.so
ln -s `pwd`/libcrypto1.1.so `pwd`/libcrypto.so
ln -s `pwd`/libssl1.1.so `pwd`/libssl.so
python --version
#Python 3.8.13
briefcase --version
#0.3.8
briefcase build android
#报错详情
"""
> Task :app:compileDebugJavaWithJavac
Exception in thread D:\weather\android\gradle\weather\gradlew.bat output streamer:
Traceback (most recent call last):
File "D:\Miniconda3\envs\beeware_venv\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\Miniconda3\envs\beeware_venv\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\Miniconda3\envs\beeware_venv\lib\site-packages\briefcase\integrations\subprocess.py", line 404, in _stream_output_thread
print(popen_process.stdout.readline())
File "D:\Miniconda3\envs\beeware_venv\lib\codecs.py", line 323, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 95: invalid continuation byte
"""
#解决办法:
export PYTHONIOENCODING=utf-8
app.py
"""
A simple weather app.
"""
from pprint import pprint
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, LEFT, RIGHT
import httpx
TOKEN = "7eadee4312991c2935a8f9109645f19e" #Enter your weatherstack.com token here.
#http://api.weatherstack.com/current?access_key=7eadee4312991c2935a8f9109645f19e&query=New%20York
BASE_URL="http://api.weatherstack.com/current"
class weatherman(toga.App):
def startup(self):
"""
Construct and show the Toga application.
Usually, you would add your application to a main content box.
We then create a main window (with a name matching the app), and
show the main window.
"""
main_box = toga.Box(style=Pack(direction=COLUMN))
location_label = toga.Label("Location", style=Pack(padding=(0, 5)))
self.location_input = toga.TextInput(style=Pack(flex=1))
location_box = toga.Box(style=Pack(direction=ROW, padding=5))
location_box.add(location_label)
location_box.add(self.location_input)
weather_box_label = toga.Label("Weather Results", style=Pack(padding=(0, 5)))
self.weather_box_input = toga.TextInput(readonly=True, style=Pack(flex=1))
weather_box = toga.Box(style=Pack(direction=ROW, padding=5))
weather_box.add(weather_box_label)
weather_box.add(self.weather_box_input)
button = toga.Button("Fetch weather", on_press=self.weather, style=Pack(padding=5))
main_box.add(location_box)
main_box.add(button)
main_box.add(weather_box)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def weather(self, widget):
params = dict(access_key=TOKEN, query=self.location_input.value)
resp = httpx.get(BASE_URL, params=params).json()
try:
self.weather_box_input.value = f'The weather report for {resp["request"]["query"]} \
{resp["current"]["temperature"]} \
{resp["current"]["weather_descriptions"][0]} \
{resp["current"]["feelslike"]}'
except ValueError:
self.weather_box_input.value = "Location unknown!"
def main():
return weatherman()
pyproject.toml
[tool.briefcase.app.wth]
formal_name = "wth"
description = "My first application"
icon = "src/wth/resources/wth"
sources = ['src/wth']
requires = [
'httpx~=0.23.0' #这行是新增的, 因为app.py中用到了此库
]
cd /home/z/
mkdir /home/z/beeware_demo_wth
cd /home/z/beeware_demo_wth
briefcase new
#应用名为:wth
cd /home/z/beeware_demo_wth/wth/
vi /home/z/beeware_demo_wth/wth/src/app.py
#替换为上面app.py的内容
briefcase create android
#注意慢问题, 参照上一节
#将会创建android项目结构 :/home/z/beeware_demo_wth/wth/android/
briefcase build android
#会报错 libcrypt.so, libssl.so找不到, 解决办法上一节已经写了. 这里复制来
cd /home/z/beeware_wth/wth/android/gradle/wth/app/build/intermediates/merged_native_libs/debug/out/lib/armeabi-v7a/
unlink libcrypto.so
unlink libssl.so
ln -s `pwd`/libcrypto1.1.so `pwd`/libcrypto.so
ln -s `pwd`/libssl1.1.so `pwd`/libssl.so
#再build一次即可
briefcase build android
#下载apk, 到手机上试一下:
/home/z/beeware_demo_wth/wth/android/gradle/wth/app/build/outputs/apk/debug/app-debug.apk