将Qt for Python应用于Android

Taking Qt for Python to Android

将Qt for Python应用于Android

April 19, 2023 by Shyamnath Premnadh | Comments

​2023年4月19日:Shyamnath Premnadh |评论

Deploying Python applications to Android is a big talking point these days among the Python community, and a frequently asked question by people developing their first GUI applications. There are already a couple of Python frameworks that offer the ability to deploy your applications to Android devices, and Qt for Android has existed for a decade now. With QML and Qt Quick, one can use Qt to develop native Android applications. It was high time we bridged the gap and brought PySide applications to Android. 

​将Python应用程序部署到Android是当今Python社区的一大话题,也是开发第一个GUI应用程序的人经常问的问题。已经有几个Python框架提供了将应用程序部署到Android设备的能力,而Qt for Android已经存在了十年。有了QML和Qt Quick,人们可以使用Qt开发本地Android应用程序。现在是我们弥合差距并将PySide应用程序引入Android的时候了

With the new 6.5 release, we give you a new CLI tool, pyside6-android-deploy, that provides an easy interface to deploy your PySide6 application 🎉. 

在新的6.5版本中,我们为您提供了一个新的CLI工具pyside6 android deploy,它为部署pyside6应用程序提供了一种简单的界面🎉.

Technical Details 

技术细节

Currently, this tool is only available on Linux-based environments, and only a subset of the Qt modules are supported (see Considerations).  

目前,此工具仅在基于Linux的环境中可用,并且仅支持Qt模块的一个子集(请参阅注意事项)。

This tool uses a custom fork of the python-for-android project with a custom Qt bootstrap and recipes for PySide6 and shiboken6. The reason for using python-for-android instead of androiddeployqt is that python-for-android already provides a way to package the Python interpreter, which is by default not pre-installed on Android platforms. It can also be easily extended to include new Python packages, even those with a C/C++ backend. python-for-android already supports many popular Python packages like numpy, pandas, etc. 

​该工具使用python-for-android的自定义fork,并为PySide6和shiboken6提供自定义Qt引导和方法。使用python for android而不是androiddeployqt的原因是,python for android已经提供了一种打包python解释器的方法,默认情况下,python解释器不会预装在android平台上。它还可以很容易地扩展到包括新的Python包,甚至是那些有C/C++后端的包。python for android已经支持许多流行的python包,如numpy、pandas等

The entire deployment process is a 3-step process, where the first two steps need to be done only once per Android platform. The first two steps are setting up and cross-compiling Qt for Python wheels required for pyside6-android-deploy to work.  

整个部署过程是一个三步过程,每个Android平台只需要完成前两步。前两个步骤是为pyside6 android部署所需的Python轮子设置和交叉编译Qt。

Steps to deploy your PySide application to Android 

将PySide应用程序部署到Android的步骤

As mentioned above, Steps 1 and 2 need to be done only once for a specific Android architecture. Each PySide6 application that you deploy will use the same Qt for Python wheels. 

如上所述,对于特定的Android架构,步骤1和2只需要执行一次。部署的每个PySide6应用程序都将对Python轮子使用相同的Qt。

Step 1: Setting up prerequisites 

步骤1:设置先决条件

The Android dependencies for PySide6 are the same dependencies as those for Qt for Android. This step involves downloading the JDK, Android NDK, and Android SDK. You may skip this step if you already have them downloaded and set up. The recommended NDK version for PySide6 version 6.5 is r25. You can either use Qt Creator to install all the dependencies or install the dependencies manually, as shown below.  

​PySide6的Android依赖项与Qt for Android的依赖项相同。此步骤包括下载JDK、Android NDK和Android SDK。如果已经下载并设置了它们,则可以跳过此步骤。PySide6 6.5版的NDK推荐版本是r25。可以使用Qt Creator安装所有依赖项,也可以手动安装依赖项,如下所示

1. Install JDK 11 or above. See instructions here.

​1.安装JDK 11或更高版本。请参阅此处的说明。

2. Download the latest version of `Android Command Line Tools Only ` for Linux. This will download a .zip file. 

2.下载适用于Linux的最新版本“仅限Android命令行工具”。这将下载一个.zip文件

3. You can use the following script to help you download and setup Android SDK and NDK(r25c) into your current working directory. 

3.可以使用以下脚本帮助您将Android SDK和NDK(r25c)下载并安装到当前的工作目录中。

#!/bin/bash 
shopt -s extglob 
if [ -z "$1" ] 
  then 
    echo "Supply path to  commandlinetools-linux-*_latest.zip" 
    exit 1 
fi 
android_sdk=$(pwd)/android_sdk 
mkdir -p $android_sdk 
unzip $1 -d $android_sdk 
latest=$android_sdk/cmdline-tools/latest 
mkdir -p $latest 
mv $android_sdk/cmdline-tools/!(latest) $latest 
$latest/bin/sdkmanager "ndk;25.2.9519653" "build-tools;33.0.2" "platforms;android-31" "platform-tools" 
cp -avr $android_sdk/cmdline-tools/latest $android_sdk/tools

Simply run the script by giving it execution permission (chmod +x) and passing the path to the downloaded .zip file as a cli argument. 

只需通过授予脚本执行权限(chmod+x)并将下载的.zip文件的路径作为cli参数进行传递即可运行该脚本。

4. Add the Android SDK and NDK paths into the following environment   variables: 

4.将Android SDK和NDK路径添加到以下环境变量中:

export ANDROID_SDK_ROOT=“/android_sdk” 
export ANDROID_NDK_ROOT=“/android_sdk/ndk/25.2.9519653” 

Step 2: Cross-compile Qt for Python wheels for Android 

步骤2:交叉编译用于Qt for Python的Python轮子

Python-for-android requires shiboken6 and PySide6 to be cross-compiled during the deployment process to produce Qt for Python binaries that are compatible with the specific Android platform. Cross-compiling Qt for Python every time for each of your applications can be a cumbersome, time-consuming task 😫. Hence, it is better to use cross-compilation once to create Qt for Python wheels for an Android platform, which can be reused for each of the applications that you deploy. We have made creating these wheels easier for you with a Python script which is available in the Qt for Python source repository.

​Python for android要求在部署过程中交叉编译shiboken6和PySide6,以生成与特定android平台兼容的Qt for Python二进制文件。每次为每个应用程序交叉编译Qt for Python可能是一项繁琐而耗时的任务😫. 因此,最好使用一次交叉编译来为Android平台创建Qt for Python轮子,它可以被部署的每个应用程序重用。我们使用Python脚本使您更容易创建这些轮子,该脚本可在Qt for Python源代码库中获得。

Make sure to install all the project requirements up to and including “Getting the source”, then install the cross-compilation requirements like this: 

​确保安装所有项目需求,包括“获取源代码”,然后安装如下交叉编译需求:

pip install –r tools/cross_compile_android/requirements.txt 

And run the following script:  

并运行以下脚本:

python tools/cross_compile_android/main.py --plat-name=aarch64 --ndk-path=$ANDROID_NDK_ROOT --qt-install-path=/opt/Qt/6.5.0 --sdk-path=$ANDROID_SDK_ROOT

In the above command, --plat-name refers to one of aarch64, armv7a, i686, x86_64 depending on the target architecture. --qt-install-path refers to the path where Qt 6.5.0 or later is installed. Use  --help  to see all the other available options.   

在上面的命令中,--plat-name指的是aarch64、armv7a、i686、x86_64中的一个,具体取决于目标体系结构。--qt-install-path指的是安装qt 6.5.0或更高版本的路径。使用--help查看所有其他可用选项。

After the successful completion of this command, you will have the Qt for Python wheels in `pyside-setup/dist` folder. 

成功完成此命令后,将在“pyside setup/dist”文件夹中拥有用于Python轮子的Qt。

Note: We recommend using venv or virtuanenv

Step 3: Deploy your application - pyside6-android-deploy

步骤3:部署您的应用程序pyside6-android-deploy

For this tutorial, we take a simple QtQuick example Scene Graph Painted Item example.  You can download this example directly from the link. One main requirement of pyside6-android-deploy tool is that the main Python entry point file should be named main.py. Change the name of painteditem.py to main.py and adjust correspondingly in painteditem.pyproject. The requirement of having the main Python entry point named main.py comes from python-for-android. Having the .pyproject file is not a strict requirement, but it enables pyside6-android-deploy to find the files associated with the project more easily and not include any unnecessary files. 

​在本教程中,我们以一个简单的QtQuick示例场景图绘制项目为例。可以直接从链接下载此示例。pyside6-android-deploy工具的一个主要要求是Python主入口点文件应命名为main.py。将painteditem.py的名称更改为main.py,并在painteditem.pyproject中进行相应调整。Python主入口点将命名为main.php的要求来自Python for android。拥有.pyproject文件并不是一个严格的要求,但它使pyside6-android-deploy能够更容易地找到与项目相关的文件,并且不包括任何不必要的文件

Run the following command: 

运行以下命令:

pyside6-android-deploy --wheel-pyside=pyside-setup/dist/PySide6-6.5.0a1-6.5.0-cp37-abi3-android_aarch64.whl --wheel-shiboken=pyside-setup/dist/shiboken6-6.5.0a1-6.5.0-cp37-abi3-android_aarch64.whl --name=painteditem  

In the above command --name refers to the application’s name, as shown in your Android device. Use --help to see the other available options. 

在上面的命令中,--name指的是应用程序的名称,如Android设备中所示。使用--help查看其他可用选项。

At the end of this command, you will have a .apk file created within the application directory, which you can install on your aarch64 based Android device. Hurray, you have successfully taken your Qt for Python application to Android🎉.

在这个命令的最后,会在应用程序目录中创建一个.apk文件,可以将其安装在基于aarch64的Android设备上🎉.

Considerations🤔 

注意事项🤔

1. Presently, the tool `pyside6-android-deploy` only works from Linux.  

1.目前,“pyside6 android deploy”工具仅适用于Linux。

     This limitation comes from our cross-compilation infrastructure that we are actively looking to improve and allow macOS and Windows users to also deploy Android applications. 

这一限制来自我们的交叉编译基础设施,我们正在积极寻求改进,并允许macOS和Windows用户也部署Android应用程序。

2. Qt modules supported: QtCore, QtGui, QtWidgets, QtNetwork, QtOpenGL, QtQml,     QtQuick, QtQuickControls2.

2.支持Qt模块:QtCore、QtGui、QtWidgets、QtNetwork、QtOpenGL、QtQml、QtQuick、QtQuickControls2。

3. The main Python entry point of the application should be named main.py 
     This requirement comes from python-for-android. 

3.应用程序的主要Python入口点应命名为main.py
这个要求来自python for android。

What can you expect in the future?🤖

你对未来有什么期待?🤖

1. More tutorials and examples

1.更多教程和示例
     You can expect all the Qt for Android examples to also work and be available for PySide6 Android deployment.

可以期待所有适用于Android的Qt示例也能工作,并可用于PySide6 Android部署。

2. Simplifying the current deployment process

2.简化当前部署流程

3. There are some manual steps that we are aiming to remove, to allow the deployment of applications to be more straightforward.

3.我们打算删除一些手动步骤,以使应用程序的部署更加简单。

4.Additional Qt modules compatibility

4.附加Qt模块兼容性

   There are many examples and real use-case that our Qt for Android offering has, and we want to aim to be compatible with them. That’s why we see value in adding support for QtConcurrent, QtPositioning, QtLocation, QtBluetooth, QtSensors, etc. 

我们的Qt for Android产品有很多例子和真实的用例,我们希望与它们兼容。这就是为什么我们看到了增加对QtConcurrent、QtPositing、QtLocation、QtBluetooth、QtSensors等的支持的价值。


Thank you for reading this tutorial😀. Have fun trying out PySide6 Android deployment. Feel free to drop us a message or open a suggestion/bug in JIRA. Your feedback fuels us in making Qt for Python better. Also, feel open to connecting to the Qt for Python community through our community platforms

​感谢您阅读本教程😀. 尝试PySide6 Android部署会很有趣。请随时给我们留言或在JIRA中打开一个建议/bug。您的反馈为我们改进Qt for Python提供了动力。此外,请对通过我们的社区平台连接到Qt for Python社区持开放态度

Stay tuned for further updates on Android deployment 👍. 

敬请关注Android部署的进一步更新👍.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值