【MQTT(3)】开发一个客户端,QT-Android安卓手机版本

avatar
作者
筋斗云
阅读量:6

手机版本更加方便

生成安卓库

参考了这个代码

在编译Mosquitto以支持安卓平台时,主要涉及到使用Android NDK(Native Development Kit)进行交叉编译。环境的准备参考之前的博客【QT开发(17)】2023-QT 5.14.2实现Android开发

在这里插入图片描述
下载Mosquitto, https://github.com/eclipse/mosquitto.git

2. 编写或准备编译脚本

对于Mosquitto的编译,可能需要编写一个CMake或Makefile脚本来配置编译选项,以支持Android平台。这通常包括指定Android的目标架构(如armeabi-v7a、arm64-v8a等)、API级别、工具链等。

例如,可以创建一个名为mosquitto_android_cmake.sh的脚本,内容大致如下(注意:以下脚本仅为示例,具体参数需根据实际情况调整):

#!/bin/bash CURRENT_DIR=$(dirname $(readlink -f $0)) rm -rf ./build mkdir -p build ANDROID_ABI="armeabi-v7a" ANDROID_NDK="/var/files/yanyixiong/android-ndk-r21e" cmake \     -DANDROID_NDK=${ANDROID_NDK} \     -DANDROID_ABI=${ANDROID_ABI} \     -DANDROID_NATIVE_API_LEVEL=21 \     -DANDROID_TOOLCHAIN_NAME="arm-linux-androideabi-4.9" \     -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \     -DWITH_TLS=OFF \     -DWITH_THREADING=OFF \     -H. -B./build cd build cmake --build . cd .. 
cmake -DANDROID_NDK=/var/files/yanyixiong/android-ndk-r21e \   -DANDROID_ABI="armeabi-v7a" \   -DCMAKE_TOOLCHAIN_FILE="/var/files/yanyixiong/android-ndk-r21e/build/cmake/android.toolchain.cmake"  \    -DWITH_TLS=OFF \     -DWITH_THREADING=OFF \      -DCMAKE_C_FLAGS='-fPIC' \       .. 

问题来了

目前来说,不支持ndk21版本

ndk 10版本

git clone https://github.com/eclipse/mosquitto.git cd mosquitto mkdir _build_android cd _build_android cmake -DANDROID_NDK=/opt/android-ndk/android-ndk-r10e -DANDROID_ABI="armeabi-v7a" -DANDROID_NDK_HOST_X64="YES"  -DANDROID_TOOLCHAIN_NAME="arm-linux-androideabi-4.9" -DCMAKE_TOOLCHAIN_FILE="/opt/android-ndk/build/cmake/android.toolchain.cmake" -DWITH_TLS=OFF -DWITH_THREADING=OFF -DCMAKE_C_FLAGS='-fPIC' .. 

ndk-r8e

参考

#!/bin/bash # Script used to cmake and then make mosquitto for android on MacOSX with android-ndk-r8e and a patched mosquitto source. # Update paths below and run it from inside the root mosquitto dir (right in the one you clone from hg)  # Used patched mosquitto from: https://bitbucket.org/andreasjk/mosquitto # Used NDK: http://dl.google.com/android/ndk/android-ndk-r8e-darwin-x86_64.tar.bz2 # Used cmake toolchain file from https://github.com/Itseez/opencv/blob/master/android/android.toolchain.cmake  # Threading has to be disabled since android doesn't support it fully # Also disabled TLS since I couldn't get cmake to find the openssl lib properly CURRENT_DIR=$(dirname $(readlink -f $0)) ANDROID_ABI="armeabi-v7a" rm -rf mosquitto/build mkdir -p mosquitto/build cd mosquitto/build cmake \    -DANDROID_NDK=${ANDROID_NDK} \    -DANDROID_ABI=${ANDROID_ABI} \    -DANDROID_NDK_HOST_X64="YES" \    -DANDROID_NATIVE_API_LEVEL=19 \    -DANDROID_TOOLCHAIN_NAME="arm-linux-androideabi-4.9" \    -DCMAKE_TOOLCHAIN_FILE="${CURRENT_DIR}/android-cmake/android.toolchain.cmake" \    -DOPENSSL_LIBRARIES="${CURRENT_DIR}/openssl/arch-${ANDROID_ABI}/lib" \    -DOPENSSL_INCLUDE_DIR="${CURRENT_DIR}/openssl/sources/include" \    -DWITH_TLS=ON \    -DWITH_THREADING=OFF .. echo "Start building android ..." make echo "Output file `pwd`" cd .. 

r16b

If you want to use mosquitto on Android platforms, then you should use Android NDK to cross-compile the source for Android platforms.

Before start compiling, you should download Android NDK from Google official website. In addition, you should make sure that the version of cmake on your device is above 3.6.0. We suggest you use linux to do the following things.

Firstly, edit {SOURCE_DIR}/CMakeLists.txt and add following commands:

add_compile_options(-fPIE) add_compile_options(-fPIC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pie") 

after

add_definitions (-DCMAKE -DVERSION=\"${VERSION}\") 

Secondly, enter the source folder({SOURCE_DIR}), and setup the build configure using the command below:

cmake -DANDROID_NDK=/home/sususweet/android_things/android-ndk-r16b -DANDROID_ABI="armeabi-v7a" -DANDROID_NDK_HOST_X64="YES"  -DANDROID_TOOLCHAIN_NAME="arm-linux-androideabi-4.9" -DCMAKE_TOOLCHAIN_FILE="/home/sususweet/android_things/android-ndk-r16b/build/cmake/android.toolchain.cmake" -DWITH_TLS=OFF -DWITH_THREADING=OFF -H. -B./build 

Then go into build folder:

cd build 

Use cmake to compile the source code:

cmake --build . 

Then you can find built mosquitto in {SOURCE_DIR}/build folder.

5.14.4 必须是ndk20,没法编译

在这里插入图片描述在这里插入图片描述

如果想继续,就必须变更版本。就此作罢,写个笔记避避坑

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!