Qt Creator, Android, Mac

Now for something totally different…. Note: No graphics on this set of instructions.

In one of my previous posts, I showed how to work with PSOC5 with QtCreator, bare metal. In this post I will discuss my experience with actually getting QtCreator to do an android application. A “Hello World” type of application.

I wholeheartedly recommend having had experience with Android Studio and having created and loaded at least a “hello world” application on your Android device. This may pre-install items for you that you need. At the very least, it gives you experience on the setup aspects, which is invaluable. Android studio is free. The Android tools and SDK’s are free, unlike Apple’s system, which requires you to pay annually to officially use their development tools and officially develop for their platform.

You must have experience developing a regular Qt application for your computer before attempting this. Familiarity with the Qt development system will help you navigate many of the uncertainties you will experience in getting Android to work. It is very painful the first time.

QtCreator Version

First, the version I used that I finally got to work was QtCreator 6.0.2, and Qt 6.2.2 (QT folks are at 6.2.4 when this was written). I started with Creator 6.0.1, but it was updated before I got the round tuit to finish. So it might work in 6.0.1. It might also work in previous versions of QtCreator, but I cannot guarantee it, as I never had success. I have tried several times before.

I am using Qt 6.2.2 because I am developing some open source applications, and that falls within the scope of their free license and allowing you to have a free account. Eventually, I believe they will try to choke off the Qt system and go full closed source, but they have not yet fully pulled that trigger. So, beware registering your company.

Baby Steps

Go to https://doc.qt.io/qt-5/android-getting-started.html and read the instructions. On the Mac, you can follow them to a point, but they are mostly fluff that confuses you, if you are on Macintosh. Ignore Gradle for now, and only worry about OpenJDK. This information is repeated on the next page we will visit. Having CMake installed may or may not be a good thing, but realize that some of Qt’s own tutorials use it. (I find qmake much better, but Microsoft and Google are stuck on CMake.)

First Install Item

First, install JDK 11. I used the AdoptOpenJDK, which forwards to a new website address. (Qt should address that, but they at least have documentation.) Ignore most of the rest of that page if you are on Macintosh. Not sure about Linux and Windows. There is stuff there you probably need on those platforms.

Second go to page https://doc.qt.io/qtcreator/creator-developing-android.html#specifying-android-device-settings. This page has meat on it. Read it more or less all the way through before following instructions. It instructs you to use QtCreator menu->Preferences->Devices->Android, and then tells you you need JDK installed.

Before this screen, find out where the JDK was installed. On my system it was in /Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home. [For some reason, JDK was installed in /Library and the /Library directory appears to be now available for update by root, for some reason. Maybe this is a Monterey thing.]

Second Install Items (Handled by QtCreator)

In my system, I decided to put the android SDK into my home directory Library (~/Library). (That is where Android Command Line wants to put is, as I discovered.) It is /Users/myHomedirectory/Library/AndroidSDK. (Put your mac name in the “myHomeDirectory” location) Click on the “SetupSDK” button. It will install the latest Android SDK there.

Once downloaded, it may offer to set up the NDK. Do So. Also, set up the SSL. That web page and that dialog is where you will spend a lot of time getting things set up. Make sure the “Automatically create kits” checkbox is enabled.

For me, I had to leave and go back to this dialog several times over several days. It picked up and continued, once I re-started the processed.

Android 11 and 12 (was Android 31) are the latest released versions. Android 32 is the code number for the next version being worked on. Once the SDK manager list box fills up, click on the expander on the left by Android 11, and click SDK Platform and Google Play Intel X86 Atom_64 System Image. Also, the Tools section should have the first 6 or so boxes checked, automatically. Then click the “Apply” button to the right of the List Box.

Go have lunch or breakfast or dinner. It will take a while.

Create An AVD

Click on the Devices Tab. Then click on the “Add” button on the right side of the dialog. Select “Android Device” and select “Start Wizard.”

On the new dialog box, enter Galaxy_Nexus, and select Galaxy Nexus from the dropdown. (You can always add new AVD’s later.) For Architecture select X86_64. The Target API should fill in. I usually set the SD Card Size to about 2 gig as I simulate an application with a large amount of data. Click OK.

Then, in order to make sure everything is OK, click on “Start AVD.” If it does not start, something has installed incorrectly (or not finished installation). I had to restart the process a few times due to having to leave the computer for an extended amount of time. Things like sleeping computers keep getting in the way.

It will take a while for the AVD to initialize and run. Once it does, you can quit the qemu emulator system to shut down the emulator.

Verify The Kits

Click on Kits in the left hand list box. Under the Kits tab, look for “Android Qt 6.2.2 Clang x86_64,” or whatever matches your Qt kit install. The Device should be listed as your Galaxy_Nexus. If it is not, then select it. If your phone is attached via USB cable to your Mac, it may show up in the list. If so, that is very good!

If your device does show up, then click on “Android Qt 6.2.2 Clang armeabi-v7a” and in the same dropdown box, select that as the device to use. If not, make sure that the sdk and ndk are installed, and that Android studio is not running. And the Android File Transfer is not running. You may have to reboot to clear out what is trying to hang around.

Create your application

This was the hardest part for me, because the tutorials drive you out into the cow pasture. I got lost several times before I saw a comment about using qmake. There is no tutorial on a Qt Widgets based application on Android, but that is the exact way you should do it.

Select File->New File Or Project. Select Application, Qt. Select Qt Widgets Application. Click the Choose button. Name your project, and select folder to create it in, click next. Set Build system to be qmake. Click next. MainWindow and QMainWindow selections show you that you are in good shape. Click Next. Click Next on Translation File. On the Kit Selection, click on the “Android Qt 6.2.2 Clang x86_64,” and the “Android Qt 6.2.2 Clang armeabi-v7a,” and the “Qt 6.2.2 for macOS.” Click Next. Click Finish.

Prepare For Debug Launch

On the Left Hand Bar, click on Projects. To test the system out, click on the “Android Qt 6.2.2 Clang x86_64,” Kit and click on Build underneath it. Make sure the Shadow build is selected and Build directory are good. I usually leave those alone, since they are typically pre-selected. Click On Run beneath that. The setting should be left alone for now. (Worked for me!)

Go back to edit view on the IDE. A this point, I start the AVD from the Preferences->Devices dialog, just because my first try failed when I did not do that. Set a breakpoint at QApplication in Main, and Click on the Debug Arrow. The build and launch should occur.

Breakpoint Hit! Yea! Err, No!

At this point, you may get to the breakpoint. If so, the kit defaulted to the Mac toolkit. This has happened to me every time. It seems to ignore the first kit selection you make after a new project creation. You have to re-select the Android X86_64 kit, and re-compile.

First Time Takes Forever

Like waiting for the right moment for the first kiss, the first time you do an android development, it takes forever. QtCreator/Android SDK downloads and installs Gradle, since the version you installed by hand is the one it does not like. If it does this, the launch will fail, because you only wait a few minutes for things to work.

You may have to halt the build and start over. After Gradle has been automatically downloaded and installed, the first launch after a reboot is like watching paint dry. It may also fail and have to be restarted.

The first launch is painfully slow. Several minutes will go by. In my case, 10 minutes + passed before the application launched. It also failed to launch with the debugger as even QtCreator gave up. Yours will too. I am running an i9 with 8 cores and 40 gb RAM installed.

I also received debugger error dialog box, which I dismissed. This appears to be a normal occurrence for the first launch. I went to the application in the emulator, and dismissed it in typical android fashion. Then I launched the application again. The second time, it made it to the breakpoint and I could single step.

Failed Success!

It is my experience that the very first QtCreator debug launch always times out. The debugger connect fails. The application runs, and it does not hit a breakpoint. Therefore, I recommend a “warm up” with a “hello world” application that does nothing, to allow all the daemons and fails to get out of the system before you debug your real app, especially if it can mess up your app files on your emulator or device.

Your Android Device

Select the arm7 kit and recompile and debug on the device a similar way. It should be more or less seamless, with no changes to the defaults in the kits, based on my experience. It is truly a marvel.

Wrapping Up

It took me a few years to get to this point. Finally, I can attempt to move my PC Apps to Android (and possibly IOS, but that is not high on my list).

Keep In Mind

I created a button and in the clicked slot, I put an “exit(0);” The button did not show on my phone. Hmm. I moved it closer to the center and to the left hand side. Bingo! It worked.

To make sure the MainWindow was the correct size, I used “showMaximized()” and then, to get the screen coordinates, I was able to use “this->height();” and “this->width();”.

It is my lack of experience in Qt that leads me to believe that their rubber banding of control centering has bugs, from a novice user point of view. My testing forced me to take programmatic control to adjust controls to fit on the current screen width. My tries with automatic centering usually changed the size of the buttons against my desires. Ugh. With experience, I am sure that can be magically overcome, but I am usually miles down the road before I learn those tricks.

So, I get the current screen size on Android Devices and adjust buttons and fields to accommodate. You may have to do this also. Does not mean this is the right way to do it!

PSOC

With Qt, I can now do a desktop app that communicates with a PSOC (over USB or RS232 or both), and an Android app with the same code base, in the same IDE, talking to the same PSOC. Communication paths may be different, but I don’t care about those unless they die horrible deaths.

Enjoy!

Add a Comment

Your email address will not be published.