Developing Qt Projects for Android with VisualGDB

This tutorial shows how to debug and build Qt 5.1 projects for Android.

Before we begin, ensure that VisualGDB is installed.

In this tutorial we will create a Qt widget project for Android in Qt Creator and then build and debug the APK file from Visual Studio using VisualGDB. We use Qt Creator to create the project as the initial project creation sets up Qt support libraries.

  1. Start Visual Studio and go to Android->Setup SDK/NDK locations. Set the paths to existing Android tools there or install them.01-androidsettings
  2. Download and install Qt Creator for Android meant for a Windows host.
  3. Start Qt Creator. Go to Tools->Options->Android. Set the Android tool paths there to be the same as in VisualGDB. Press on OK.02-qtsettings
  4. Go to File->New File or Project. Choose Qt GUI Application.03-newqt
  5. Choose a name and location for the project. Press on ‘Next’. 04-newqtname
  6. Uncheck the Desktop kit and select an Android kit instead. Here we choose arm, choose “Android for x86” as well or instead if you intend to support Android for Intel CPUs as well. Note that arm here actually means armeabi-v7a as Qt 5 is not supported for regular armeabi. That means for running and debugging the app, you will also need a armeabi-v7a device. 05-androidkit
  7. The default settings are fine for the rest of the project settings. Choose ‘Next’ and then ‘Finish’ on the next page. 06-newform
  8. We have now made a Qt5 project for Android. Currently the form is empty, let’s add a button to have something to see and test when the app is run. Open the ui file.08-projectmade
  9. Drag and drop a simple push button from the toolbox. We also change its text to “Test”. 09-button
  10. Next we add a clicked handler to it. Right-click on the placed button choose ‘Go to slot’. Select ‘clicked’ from the slots. Now a clicked handler has been made and we are ready to build the project.10-slot11-clicked
  11. Both build and run the project. This is important as currently the build step of copying the Qt libraries is only done by Qt Creator and not an external tool. Once the project has been run by QtCreator, it can be built by other tools as well. In case running the project crashes or does not succeed, it still may have been enough to copy the libraries. In that case, check inside the “android/bin” subfolder in the project folder, it must contain the Qtandroid-debug.apk file before we can proceed. 13-build
  12. Open the ‘Projects’ tab, we will use these settings to create a project with VisualGDB.14-buildsettings
  13. Start Visual Studio. Go to File->New->Project. Choose the ‘Custom Project Wizard’, choose a name and location and press on ‘OK’.15-newcustom
  14. Custom projects allow specifying arbitrary commands for building and debugging. We start by specifying the actions before building. We will use the main make command as the main build command and arrange all other commands before or after.16-custom
  15. The first command is the qmake command. We base it on the qmake command from QtCreator. 19-qmakecmdAdd a new command to the sequence.17-newsequenceCreate a new custom command line. Copy the details of the command from Qt Creator.18-newcustomWe also need to add additional environment variables to the command. Click to edit additional environment. Then click on ‘Add’.21-newenvLook in the variables list from Qt Creator for variables to add. Most do not need to be added as they are system variables.20-addenvWe only add the following variables.22-envThe ready command looks as follows.23-newqmakecmd
  16. Next add the build command. Fill in the command details based on the make command from QtCreator.24-makestepThe ready command looks as follows.25-buildcmd
  17. Now we add a post build command based on the deployment install command from Qt Creator. This command will copy the built library from the Qt project’s build directory to the android subdirectory. 26a-originstallcmd26-installcmd
  18. Finally we add the last command to post build actions. This will build the Android app.27-antcmd
  19. We also add a clean command based on the clean command from Qt Creator.29-origmakeclean28-makeclean
  20. All of the build and clean commands now look as follows. Click on ‘Finish’ to complete the wizard.30-wizfinish
  21. Now we have successfully created a project in VisualGDB building the Qt Creator created Android project. Rebuild the project to test the build commands.42-rebuild
  22. We will use the Custom APK debug feature to debug the project. Go to Android->Debug custom APK file.31-apkdebugChoose the Qtandroid-debug.apk file and choose a name for the debug preset. Press on ‘Debug’.33-apkdebug
  23. Now the app will be automatically deployed and started on the connected Android device.34-deployingFinally the debug session is started.35-debuggingPause execution and use the ‘Show a list of all source files’ function.37-listsourcesOpen the mainwindow.cpp file.39-sourcesSet a breakpoint in the clicked function and continue execution.40-breakpoint
  24. Go to the app running on the device and click on the button. 36-appGo back to Visual Studio. The breakpoint we set earlier has been hit now.41-breakpointhit