Important: Please read the Qt Code of Conduct -

Android library with java code

  • Hi guys!

    I have a simple question that I couldn't be able yet to figure it out. I have my Android application and I want to create many library for specific functions (GPS, Network, etc). I'd like to modularize these modules into .so libraries but I'm not sure how to include my java code in a library project. Is this even possible?

    I took QtNotifier as example and had no problem to add my file and make calls from C++ to Java and from Java to C++. Therefore, my question is only about .pro configuration.

    I set ANDROID_PACKAGE_SOURCE_DIR and DISTFILES but there is no trace in Compile Output of my java file :/
    I added some errors to this file and QtCreator doesn't even notice that the file will not compile.

    Could someone guide me with this problem? Am I taking a good decision about architecture of my Android application?

    Thanks in advance,

  • Qt Champions 2016

    Unless you write your Android function in JNI completely, it is not possible to bundle native Java source within .so file. Or you may build a .jar file together with .so .

  • @benlau thanks for the reply!!

    I'm thinking about having a common folder for Java sources and include it from my application. Will the java code be accessible from all the C++ modules? I mean, can I use the methods from Java classes in my libraries?

  • Qt Champions 2016

    You need to tell where is your Java files by configure your build.gradle .

    For example, you may add your Java file path to java.srcDirs in build.gradle

        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java']
                aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
                res.srcDirs = [qt5AndroidDir + '/res', 'res']
                resources.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                assets.srcDirs = ['assets']
                jniLibs.srcDirs = ['libs']

  • @benlau

    Thanks for answering my question!!! I could solve it :D

Log in to reply