How To Mix C++ And Java Code (In Qt For Android)

Qt allows you to use Java code in your Android applications. This is the same code that Android Studio (or Eclipse etc.) users use when they are writing applications for Android. In Qt, by default, you are limited to C++ code and what the Qt modules have to offer (which in most cases are enough) but there are certain situations where you need to use something from Android API which is not provided in Qt. An obvious example for this can be using the default Android Galley to open a picture, or using Text-To-Speech engine or any other API that you can think of. If you don’t want to be limited to what is provided by Qt while writing Android apps then follow the steps below to be able to add Java code to your Qt project.

Note: I assume that you already have Qt for Android installed on your computer, if not then you can use this guide for a simple step-by-step on how to install it.



  • First of all create a Qt Widgets Application and set Android as the kit you want to use. (You can use x86 with Emulators and armeabi-v7a with actual Android phones.)
  • Add the following line to your qmake .PRO file. (Append it anywhere you want, but I recommend the bottom of the .PRO file)
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
  • Now you have to download and extract contents of the following ZIP file into your project folder, and add all extracted files to your project, by right clicking on your project and selecting “Add Existing Files”.
Download Btn
Download android-sources


Two files that you will find inside this ZIP files are listed below.

android-sources/AndroidManifest.XML
android-sources/com/mycompany/myappname/myappname.JAVA
  • Now you can write Android code (Assuming you are already familiar with it) inside JAVA file mentioned above. To pass results between Java and Qt (C++) you need to use JNI. You can search my website and all over the internet for information about JNI but to put it simply, it is an interface between your Qt (C++) code and Android (Java) code.
  • You can define functions like this inside your MainWindow.CPP or any other source file to access Java functions. Here it is assumed that you have a function named “somefunction” in your Java code that resturns String values:
JNIEXPORT void JNICALL
Java_com_mycompany_myappname_myappname_somefunction(JNIEnv*/*env*/,
	jobject /*obj*/,
	jstring results)
{
	some_global_var = QAndroidJniObject(results).toString();
}

As stated in Qt Documentation, you have to have the following in your code to be able to define the function above:

Add following line to your header file:

#include <QtAndroidExtras>

Add following line to your .PRO file:

QT += androidextras

Finally, it is extremely important to note that “mycompany”, “myappname” and other names that have been used in this tutorial have to be consistent across all of your project. Obviously you can change them to whatever fits you and your project but you have to repeat those changes in “AndroidManifest.XML”, inside your Java code, inside your CPP sources, change the folder names under “android-sources” folder etc. You MUST be consistent all over the place or your project will crash!

I’ll write more tutorials for specific usage of some Android specific code in Qt projects in the upcoming days and weeks but in the meantime you can put a comment below if you are facing any problems or if you have any questions.



2 Replies to “How To Mix C++ And Java Code (In Qt For Android)”

  1. Great guide. But I was wondering, when in Qt I want to make an app for both iOS and Android and the Android app should be able to call the native code and the iOS app not. Is that possible without having two different source code projects?

    1. Use compiler directives. Something like this:

      #ifdef Q_OS_IOS

      // iOS code goes here

      #endif

      #ifdef Q_OS_ANDROID

      // Android code goes here

      #endif

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.