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

手机版本更加方便

生成安卓库

参考了这个代码

在编译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的脚本,内容大致如下(注意:以下脚本仅为示例,具体参数需根据实际情况调整):

bash 复制代码
#!/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,没法编译

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

相关推荐
AORO_BEIDOU42 分钟前
单北斗+鸿蒙系统+国产芯片,遨游防爆手机自主可控“三保险”
华为·智能手机·harmonyos
云云3211 小时前
搭建云手机平台的技术要求?
服务器·线性代数·安全·智能手机·矩阵
云云3211 小时前
云手机有哪些用途?云手机选择推荐
服务器·线性代数·安全·智能手机·矩阵
带电的小王1 小时前
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
android·智能手机·whisper·qualcomm
梦想平凡1 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
元争栈道2 小时前
webview和H5来实现的android短视频(短剧)音视频播放依赖控件
android·音视频
阿甘知识库3 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
元争栈道3 小时前
webview+H5来实现的android短视频(短剧)音视频播放依赖控件资源
android·音视频
MuYe3 小时前
Android Hook - 动态加载so库
android
云空4 小时前
《QT 5.14.1 搭建 opencv 环境全攻略》
开发语言·qt·opencv