Excerpt
If you develop an application for the Android platform with Qt, you may want to code some stuff in Java. In this case, you may use Android Studio in addition to Qt Creator. If you do nothing and use the default configuration generated by Qt Creator, your .java
files will be put in the Qt directory and not in the directory of your project. Cross your fingers if you upgrade your version of Qt… or take your destiny in your own hands and configure your future now.
Show me the damage done
If you do like screenshots, enjoy.
I assume that you have created a new project in Qt Creator, and that you have copied the template files to your directory structure. You can now launch Android Studio to work on the Java side, it’s quite more convenient than using Qt Creator for this part, not mentionning that you may want to use the debugger1 with Android Studio GUI.
And you’re done. It’s so good to feel like a pro :
Let’s have a look at your Qt directory…
Hem, it looks like your java file is not in the best place one can consider. If you use a version control system, you probably have not had the file added in.
Let’s fix it
It took me time to figure out how to fix it.2 It’s so simple I have to fill this part with unecessary words to make it bigger. You only need to create a src
directory under the android
directory lying in your project directory. It looks like this :
If you try creating a class now, Android Studio gently asks you where should it put it :
And you can see the slight difference in Android Studio :
Wasting some more time
If you have more knowledge on this subject, it would be very kind of you to leave a comment in order to kill the voodoo in this matter. Here is what I believe.
Android Studio uses and is tied to Gradle. Gradle uses the notion of source set. In a source set, you can put several source directories. Qt defines template files 3 in order to build an Android apk
. These template files include a build.gradle
script. This template Gradle script actually adds, in the main
source set, the Qt directory.
sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java'] aidl.srcDirs = [qt5AndroidDir + '/src', 'srcqt', 'aidl'] res.srcDirs = [qt5AndroidDir + '/res', 'res'] resources.srcDirs = ['src'] renderscript.srcDirs = ['src'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } }
I’ve not found where the default main source set is specified. It appears that Gradles finds sources that are inside the src
directory if it exist in the same directory than gradle.build
, that is QtProjectPath/android/src
by default when creating templates in Qt Creator.
Consequently, you have nothing to do if you put your Java files in this directory. If you want to add your Java files somewhere else, you just have to add source directories to your source set using one of the documented syntax.
You can even define several source sets, but it is beyond the scope of this article.
- But if you are this kind of programmer, you know only the concept of debugger, and you have no experience in debugging because you never need to, because you never introduce a bug in a program : it’s ineffecient, why would you do that ? Even opening someone else’s project make bugs fade away in a whisper of fear.
- I’ve hiked in Qt Creator documentation, in Android Studio documentation, in IntelliJ Idea documentation, in Gradle documentation, on Bogdan Vatra’s blog and found the naked truth : we’re alone in the universe. So I decided to act as a responsible individual, and consequently did a lot of stupid things. So poped up the solution. It’s just a note, for you to know. May prooves itself useful. In some way.
- This configuration is copied from your kit when you click on
Create Templates
(well, my understanding is that the template files copied in your project directory are used as templates by Qt Creator when building theapk
. At leastManifest.xml
is such a template.). I’m not sure about the correct way to use several kits in a project if you must have different values in the files copied from the templates…