Skip to content

QtCreator CMake for Android plugin

Santa Claus is coming to … wait a minute, it’s not Christmas yet!

Status update: The patch was merged into master branch on Mar 6th, therefore it will be part of QtCreator 4.10(5.0?) not 4.9!

I have the pleasure to let you know that KDAB has contributed to Qt with yet another super cool project! It’s about QtCreator CMake for Android! I know it’s a strange coincidence between this article and The Qt Company’s decision to ditch QBS and use CMake for Qt 6, but I swear I started to work on this project *before* they announced it 🙂 ! This plugin enables painless experience when you want to create Android apps using Qt, CMake and QtCreator. It’s almost as easy as Android Qmake QtCreator plugin! The user will build, run & debug Qt on Android Apps as easy as it does with Qmake. Before I go into the boring details, let’s see what the requirements are and, more importantly, when it will be available! Requirements:

  • cmake 3.7 or newer (needed for server support in QtCreator)
  • NDKr18 or newer (only Clang and libc++ shared are supported)
  • Qt 5.12.1 (was too late for this patch to get in 5.12.0)

When will it be available? Well, I started the blog with the Santa on purpose, because, sadly, it’s too late to push it in QtCreator 4.8 and it will be available in the next version (4.9). If you can’t wait for QtCreator 4.9 and you like to try it sooner, you can apply this patch on top of QtCreator’s master branch. Now back to technical details, in order to build your Qt Android application, this plugin must do some magic:

  • after it builds the C++ bits, it copies all the targets (DynamicLibraries) into “{build_folder}/android/libs/{arch}”
  • generates android_deployment_settings.json, which is needed by androiddeployqt tool

After this step, androiddeployqt will complete your Android Qt APK by copying all the Qt dependencies (libs & resources). Last but not least, these are qmake features that you’ll not find in cmake:

  • IDE management for ANDROID_PACKAGE_SOURCE_DIR yes, it supports even the same naming as qmake. You’ll need to add the following piece of cmake script to your CMakeLists.txt file:
    if(ANDROID)
        set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android" CACHE INTERNAL "")
    endif()
    

    The CACHE is mandatory, otherwise QtCreator won’t see the variable and it won’t use it

  • IDE support ANDROID_EXTRA_LIBS, you’ll need to add the next piece of CMake script to your CMakeLists.txt file
    if(ANDROID)
        if (ANDROID_ABI STREQUAL "armeabi-v7a")
            set(ANDROID_EXTRA_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/3rd-party/android_armeabi-v7a/ssl/libcrypto.so ${CMAKE_CURRENT_SOURCE_DIR}/3rd-party/android_armeabi-v7a/ssl/libssl.so CACHE INTERNAL "")
        endif()
    endif()
    

    The previous snippet will add libcrypto.so and libssl.so files ONLY for armeabi-v7a android ABI.

Note: KDAB offers training in CMake, including all the latest tips from our trainers, who are all active developers.

FacebookTwitterLinkedInEmail

12 thoughts on “QtCreator CMake for Android plugin”

  1. Does it pack just the shared objects? Wouldn’t be nice if this would have something to do with “install” step of CMake?
    At the moment Qt Creator has some deployment support via QtCreatorDeployment.txt, but one has to manually set it up, which is a pain. http://doc.qt.io/qtcreator/creator-deployment-embedded-linux.html
    It would so much better if there would be some cross platform support for the “installed” targets.

    1. BogDan Vatra

      Currently it packs all shared objects.
      Install step will be very difficult with cmake, because the shared objects must go in some special folders (libs/arm64-v8a, libs/armv7a, etc).
      That’s the reason why I chose to copy those libs from QtCreator and not using cmake ;-).

      1. Shouldn’t this use “the cmake way” of make install rather than copying it manually over?

        1. BogDan Vatra

          Well, if an user really insist to use “the cmake way” you can disable the “Magic” QtCreator step and add one in which you do the “make install”, but be aware that you’ll need to set the right paths before doing that.
          My goal was (still is and it will be) to create a painless experience for the users ;-).

  2. > after it builds the C++ bits, it copies all the targets (DynamicLibraries) into “{build_folder}/android/libs/{arch}”
    generates android_deployment_settings.json, which is needed by androiddeployqt tool
    nice ! it would be great if QtCreator had the same level of integration with CMake for building and deploying, e.g. macos bundles, windows installers, and dare I say AppImages …

    1. BogDan Vatra

      Hehe, I’m going to tell you a secret, I did this plugin (and also the android qmake one) because I’m way too lazy to build, deploy & debug cmake Qt apps manually on Android ;-).

  3. Is there an example project that creates an Android app with CMake? I have installed Qt 5.12.1 and QtCreator 4.9.0-beta1, but I’m not sure how to do this. I assumed that I can use the same CMakeLists.txt that I use for building on the desktop version (which uses add_executable()) also for the Android app, just like I could use the same qmake project for both the desktop version and the Android app. But I get an error at link time:

    Qt/5.12.1/android_armv7/lib/libQt5Gui.so: error adding symbols: file in wrong format

    Or do I need a separate CMakeLists.txt for the Android app with a shared library target? A simple example project how to set this up would be really helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close