Qt For Android在Window中用脚本批量编译参考

想得太天真了,以为在Windows解决个别仓库的脚本编译问题,就可以一劳永逸地在Window中自由开发安卓了,当连接的第三方库越多,越会发现选择在Windows中开发安卓是多么悲壮的事情,有些库是无法在windows中编译的如openssl。

如果非要在Windows中开发呢?有一个方案就是在Linux编译,生成库文件,在Windows中使用,但那又苦呢?

——————————————————————

几经波折及艰难的研究Android编译过程,最后总算编写出完整的、能在Windows运行的与Qt默认方式编译一致的脚本【网上也有Nijia的编译方式】,具体如下,

@echo off
set path_script=%~dp0

cd /D %path_script%
rd /s/q "%path_script%\build"
rd /s/q "%path_script%\android"

md "%path_script%\build"
cd /D "%path_script%\build"




set ANDROID_NDK_HOST=windows-x86_64
set ANDROID_SDK_ROOT=E:/android/AndroidSdk
set ANDROID_NDK_ROOT=%ANDROID_SDK_ROOT%/ndk/21.3.6528147
set QT_ROOT=C:\Qt\Qt5.12.12
set PATH=%PATH%;%QT_ROOT%\Tools\QtCreator\bin\jom\;%QT_ROOT%\Tools\QtCreator\bin

if not exist "%ANDROID_SDK_ROOT%" (
    echo "not exist path:%ANDROID_SDK_ROOT%"
    pause
)

if not exist "%ANDROID_NDK_ROOT%" (
    echo "not exist path:%ANDROID_NDK_ROOT%"
    pause
)

setlocal enabledelayedexpansion
for %%i in ("x86","armeabi-v7a","arm64-v8a") do (     
    cd %path_script%
    rd /s/q "%path_script%\build"   
    md "%path_script%\build"
    if %%i=="x86" (
        echo "abi type:x86"
        set arch=x86
        set abi=x86
    )
    if %%i=="armeabi-v7a" (
        echo "abi type: armeabi-v7a"
        set arch=armv7
        set abi=armeabi-v7a
    )
    if %%i=="arm64-v8a" (
        echo "abi type: arm64-v8a"
        set arch=arm64_v8a
        set abi=arm64-v8a
    )
    set QTDIR=%QT_ROOT%\5.12.12\android_!arch!
    if not exist "!QTDIR!" (
        echo "not exist path:!QTDIR!"
        pause
    )
    set ANDROID_DEFINITION="-GNMake Makefiles JOM"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DCMAKE_PROJECT_INCLUDE_BEFORE:PATH=!QT_ROOT!/Tools/QtCreator/share/qtcreator/package-manager/auto-setup.cmake"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DQT_QMAKE_EXECUTABLE:STRING=!QTDIR!/bin/qmake.exe"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DCMAKE_PREFIX_PATH:STRING=!QTDIR!"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DCMAKE_C_COMPILER:STRING=!ANDROID_NDK_ROOT!/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DCMAKE_CXX_COMPILER:STRING=!ANDROID_NDK_ROOT!/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DANDROID_NDK:PATH=!ANDROID_NDK_ROOT!"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DCMAKE_TOOLCHAIN_FILE:PATH=!ANDROID_NDK_ROOT!/build/cmake/android.toolchain.cmake"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DANDROID_STL:STRING=c++_shared"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DCMAKE_FIND_ROOT_PATH:PATH=!QTDIR!"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DANDROID_SDK:PATH=!ANDROID_SDK_ROOT!"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DANDROID_NATIVE_API_LEVEL:STRING=16"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DANDROID_ABI:STRING=!abi!"
    set ANDROID_DEFINITION=!ANDROID_DEFINITION! "-DBUILD_SHARED_LIBS:STRING=OFF"

    cd %path_script%/build && cmake.exe  ../zlib-1.2.11 !ANDROID_DEFINITION! -DCMAKE_INSTALL_PREFIX="%path_script%/android/!abi!" 
    cmake --build . --config Release && cmake --build ./ --config Release --target install
)
endlocal
cd %path_script%