Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to install additional libraries for Android development?



  • I found two libraries, that would speed up my development. I have linked the installation instructions:

    Unfortunately I don't understand the installation instructions. Let's take a look at the first step for Androidnative after I have cloned the Git repository:

    qpm install android.native.pri
    
    • Where do I have to execute the command?
    • Where is the library installed?

    I have three Qt Creator instances installed on my Mac:

    ~/Qt 
    ~/Felgo
    ~/SailfishOS
    

    I assume, that I have to define the target for the installation of a library like Androidnative, instead of installing something on the macOS level.

    Finally I assume I have to refer to the library in the .pro file to be able to reference the Qt classes or QML types in my QML code.

    The installation guide also mentions the grandle file that is usually not existing, if I create aproject from a Qt template. So the question here is: Where can I get a grandle template? I could create a project in Android Studio, and take a grandle file from there, but I think it wouldn't be compatible.

    If I open the example project of Androidnative I get the following build error:

    ../androidnativeexample/main.cpp:7:10: fatal error: 'qadrawableprovider.h' file not found
    #include "qadrawableprovider.h"
             ^~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    

    It's obvious, that the library is not installed. Thanks for any help.



  • With the help of a colleague I found the problem. The problem was an outdated version in the qpm repository. Here are the steps for a kind of hello world project with androidnative:

    1. Create a new project with Qt Creator and choose Qt Quick app template, that supports Android, with an empty window.
    2. Build and run this template to check you project and create useful Android sources, that you need later.
    3. Install the androidnative library in the root directory of the created project with qpm install android.native.pri as described in the installation tutorial of the library.
    4. Go to a sub-directory cd vendor/android/native/pri and update the code with git checkout masterand git pull
    5. Go back to the root directory of your project and add a sub-directory android-sources
    6. Copy the AndroidManifext.xml file from the build directory of your project, that was created in step 2, to the new android-sources directory.
    7. Copy the build.grandle file from the build directory of your project, that was created in step 2, to the new android-sources directory.
    8. Add the following lines to this build.granle file:
    apply from: "androidnative.gradle"
    setAndroidNativePath("/../vendor/android/native/pri")
    
    1. Copy the androidnative.grandle file to the android-source directory.
    2. Add the AndroidManifest.xml and build.grandle to your project with the contextual menu for adding existing files in the file tree of your project in Qt Creator.
    3. Add these lines to your project's .pro file:
    include(vendor/vendor.pri)
    android {
        ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
    }
    
    1. Now edit your main.cpp file and add the import statements and the following code snipped:
    #ifdef Q_OS_ANDROID
    #include "AndroidNative/systemdispatcher.h"
    #include "AndroidNative/environment.h"
    #include "AndroidNative/debug.h"
    #include "AndroidNative/mediascannerconnection.h"
    #include <QtAndroidExtras/QAndroidJniObject>
    #include <QtAndroidExtras/QAndroidJniEnvironment>
    
    JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
        Q_UNUSED(vm);
        qDebug("NativeInterface::JNI_OnLoad()");
    
        // It must call this function within JNI_OnLoad to enable System Dispatcher
        AndroidNative::SystemDispatcher::registerNatives();
    
        return JNI_VERSION_1_6;
    }
    #endif
    
    1. Finally you can add some code for a simple hello world example. Firs the Import statement.
    import QtQuick.Controls 2.0
    import AndroidNative 1.0 as AN
    
    1. Then some QML code inside the Window element:
    title: qsTr("Hello World")
    
    AN.Share {
            id: an
        }
    
        Button {
            text: "Share"
    
            onClicked: {
                an.shareContent(  {
                     // leave it empty for a stupid example
                }  );
            }
    }
    

    You find more instructions here at Github.



  • I was able to run the example project of Android native. One step is missing in the installation documentation of the project:

    1. Go to the root directory of the example project in your terminal session with cd examples/androidnativeexample from the project root directory
    2. Install the missing dependency with qpm install com.github.benlau.quickandroid
    3. Clean up the build directory and re-build and deploy the project

    I try a small sample project for the integration of androidnative. Unfortunately I have dependency issues and the documentation is incomplete.



  • With the help of a colleague I found the problem. The problem was an outdated version in the qpm repository. Here are the steps for a kind of hello world project with androidnative:

    1. Create a new project with Qt Creator and choose Qt Quick app template, that supports Android, with an empty window.
    2. Build and run this template to check you project and create useful Android sources, that you need later.
    3. Install the androidnative library in the root directory of the created project with qpm install android.native.pri as described in the installation tutorial of the library.
    4. Go to a sub-directory cd vendor/android/native/pri and update the code with git checkout masterand git pull
    5. Go back to the root directory of your project and add a sub-directory android-sources
    6. Copy the AndroidManifext.xml file from the build directory of your project, that was created in step 2, to the new android-sources directory.
    7. Copy the build.grandle file from the build directory of your project, that was created in step 2, to the new android-sources directory.
    8. Add the following lines to this build.granle file:
    apply from: "androidnative.gradle"
    setAndroidNativePath("/../vendor/android/native/pri")
    
    1. Copy the androidnative.grandle file to the android-source directory.
    2. Add the AndroidManifest.xml and build.grandle to your project with the contextual menu for adding existing files in the file tree of your project in Qt Creator.
    3. Add these lines to your project's .pro file:
    include(vendor/vendor.pri)
    android {
        ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
    }
    
    1. Now edit your main.cpp file and add the import statements and the following code snipped:
    #ifdef Q_OS_ANDROID
    #include "AndroidNative/systemdispatcher.h"
    #include "AndroidNative/environment.h"
    #include "AndroidNative/debug.h"
    #include "AndroidNative/mediascannerconnection.h"
    #include <QtAndroidExtras/QAndroidJniObject>
    #include <QtAndroidExtras/QAndroidJniEnvironment>
    
    JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
        Q_UNUSED(vm);
        qDebug("NativeInterface::JNI_OnLoad()");
    
        // It must call this function within JNI_OnLoad to enable System Dispatcher
        AndroidNative::SystemDispatcher::registerNatives();
    
        return JNI_VERSION_1_6;
    }
    #endif
    
    1. Finally you can add some code for a simple hello world example. Firs the Import statement.
    import QtQuick.Controls 2.0
    import AndroidNative 1.0 as AN
    
    1. Then some QML code inside the Window element:
    title: qsTr("Hello World")
    
    AN.Share {
            id: an
        }
    
        Button {
            text: "Share"
    
            onClicked: {
                an.shareContent(  {
                     // leave it empty for a stupid example
                }  );
            }
    }
    

    You find more instructions here at Github.


Log in to reply