Click here to Skip to main content
15,892,059 members
Articles / Mobile Apps / Android
Tip/Trick

How to Make a C++ Qt Android Project with CMake on Windows

Rate me:
Please Sign up or sign in to vote.
4.83/5 (6 votes)
14 Jun 2017CPOL3 min read 20.2K   14   4
A step-by-step tutorial about how to build a Qt/C++ project on Android

I am writing this because I've spent more than four days to figure this out, and maybe others have the same issue.
So let's get straight to the point. To achieve this, you need to install a couple of software:

  1. A Qt 5.7
  2. Android SDK (revision 25.2.5)/NDK (android-ndk-r14b)
  3. A CMake (3.7.0)
  4. Python (3.5.2 -- in this case, maybe other versions are good too)
  5. qt-android-cmake from GiHhub
  6. And to try this out, you need something like QML-Android-Demo project

If you got all of this, you can start to build up your environment:

  1. Hop in to your local CMake bin directory and create a batch file just now, call it "SetEnv_QtArm.bat" put these lines in it:
    @ECHO OFF
    SET PATH=%PATH%d:\Programs\CMake\bin\
    SET QT5_ANDROID=d:\Programs\Qt\5.7\android_armv7\
    SET QT5_HOST=d:\Programs\Qt\5.7\android_armv7\mkspecs\win32-g++\

    Reminder: Don't forget to change the paths to reflect your layout!
    And I am doing this because I use multiple build setups on my desk and I don't like the idea to pollute my global environmental settings. Ok, if you saved the file, create a shortcut from it and place it on to your desktop or wherever you find it easily.

  2. In your QML-Android-Demo folder, create a build.bat file and put this line in it:
    cmake -G"MinGW Makefiles" -DCMAKE_SYSTEM_NAME=Android 
    -DCMAKE_C_COMPILER=d:/Programs/AndroidToolchain/bin/arm-linux-androideabi-gcc.exe 
    -DCMAKE_CXX_COMPILER=d:/Programs/AndroidToolchain/bin/arm-linux-androideabi-c++.exe 
    -DCMAKE_TOOLCHAIN_FILE=d:/Programs/qt-android-cmake/toolchain/android.toolchain.cmake 
    -DANDROID_NDK=d:/Programs/android-ndk-r14b 
    -DCMAKE_MAKE_PROGRAM=d:/Programs/AndroidToolchain/bin/make.exe -DCMAKE_BUILD_TYPE=Debug 
    -DANDROID_TOOLCHAIN_NAME="arm-linux-androideabi-4.9" -DANDROID_NATIVE_API_LEVEL=android-18 
    -DANDROID_PLATFORM=android-18  -DANDROID_ABI="armeabi-v7a" d:/Prog/QML-Android-Demo-master/

    If you are there, I think this is a great time for you to create a new directory, call it build. And again don't forget to change the paths or anything else which needed to reflect your system layout!

  3. If you were watchful, you already noticed that there is stuff called AndroidToolchain I haven't mentioned about anything yet. So here it comes, the latest and greatest Android NDK comes with a handy-dandy python script called "make_standalone_toolchain.py" let's put to good use and create our toolchain like: "python make_standalone_toolchain.py --arch arm --api 18 --install-dir d:/Programs/AndroidToolchain" -- of course, python is in the "echo %PATH%" somewhere.
  4. Okay, there is one final HAXOR left. For some reason, we have to put two folder form the Android SDK (build-tools and platforms) appear in the ../Qt/5.7 folder, so you either copy those folders into it, or you make junctions points. Let's make just those points: mklink /J d:\Programs\Qt\5.7\build-tools "c:\Program Files (x86)\Android\android-sdk\build-tools" and run this to the platforms folder too.
  5. It's build time! Hit the SetEnv_QtArm.bat. Go to the ../QML-Android-Demo project/build and call our build.bat "..\build.bat", if everything goes as it should be, there won't be any error and everything is generated.

Go back to the projects' root and type this cmake --build build, it will give you an error so you have to edit your build.gradle which is just yet created. Go the ../build/src and edit this file like this:

compileSdkVersion androidCompileSdkVersion.toInteger()
buildToolsVersion androidBuildToolsVersion

To something which reflects your system in my case:

compileSdkVersion 18
buildToolsVersion "23.0.1"

So move back to the project root and call camke --build build again after that, you have to see something like this at the end:

BUILD SUCCESSFUL

Total time: 23.898 secs
PREDEX CACHE HITS:   0
PREDEX CACHE MISSES: 2
Stopped 0 compiler daemon(s).
Android package built successfully in 37.245 ms.
  -- File: D:/Prog/QML-Android-Demo-master/build/src//build/outputs/apk/src-debug.apk

Finish! After this, you only need to install it manually or with adb!

Troubleshooting

  • Configure the env variable QT5_android to point your Qt5 installation. The QML-Android-Demo projects CMakeLists.txt check this variable and if cannot find in the system envs. You forget to put this variable in the system envs. (This is why we made a bat file.)
  • Configure the env variable Qt5_host to point to your host Qt5 installation. same as the above.
  • error java.lang.IllegalArgumentsExceptions: android.buildToolsVersion is missing edit the build.gradle file according to the point 4th
  • The same goes to the compileSdkVersion too

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Hungary Hungary
Not much to tell, I'm in the programing business for a while and I planning to stay a little bit more Wink | ;)

Comments and Discussions

 
QuestionAbout starting project Pin
Member 1403665629-Oct-18 4:01
Member 1403665629-Oct-18 4:01 
GeneralMy vote of 5 Pin
hooodaticus15-Jun-17 7:20
hooodaticus15-Jun-17 7:20 
QuestionSpelling mistake in instructions. Pin
Member 1282942013-Jun-17 0:51
Member 1282942013-Jun-17 0:51 
AnswerRe: Spelling mistake in instructions. Pin
Vhalun14-Jun-17 7:51
Vhalun14-Jun-17 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.