0

I am writing a mobile app in Qt that needs to make some platform-specific calls, like requesting battery level. So from the Qt level I am trying to make a call to the Android SDK level. From what I gathered on the internet, the following should do.

I made a small Java class with the code to be called:

// AndroidService.java
package path.to.java;

public class AndroidService {
    public static int doSomething() {
        System.out.println("Android says hi");
        return 1;
    }
}

Next, in my Cpp code, I make a call to this using Qt's Android JNI library:

// androidservice.cpp
#include <QtAndroidExtras>

void doSomething() {
    QAndroidJniObject::callStaticMethod<int>("path.to.java.AndroidService", "doSomething");
}

I call this in my main function.

Lastly, I added the following to my .pro file:

// project.pro
android {
    QT += androidextras
    SOURCES += mobileservice/androidservice.cpp
    OTHER_FILES += mobileservice/android/src/path/to/java/AndroidService.java
    ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/mobileservice/android
}

Upon compilation, this builds without error. However, when running the app, I get a crash with the following stack trace:

E/AndroidRuntime( 7184): java.lang.UnsatisfiedLinkError: dlopen failed: library "libQt5AndroidExtras.so" not found
E/AndroidRuntime( 7184):    at java.lang.Runtime.loadLibrary(Runtime.java:371)
E/AndroidRuntime( 7184):    at java.lang.System.loadLibrary(System.java:988)
E/AndroidRuntime( 7184):    at org.qtproject.qt5.android.bindings.QtActivity.loadApplication(QtActivity.java:252)
E/AndroidRuntime( 7184):    at org.qtproject.qt5.android.bindings.QtActivity.startApp(QtActivity.java:655)
E/AndroidRuntime( 7184):    at org.qtproject.qt5.android.bindings.QtActivity.onCreate(QtActivity.java:895)
E/AndroidRuntime( 7184):    at android.app.Activity.performCreate(Activity.java:6500)
E/AndroidRuntime( 7184):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
E/AndroidRuntime( 7184):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
E/AndroidRuntime( 7184):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
E/AndroidRuntime( 7184):    at android.app.ActivityThread.access$1000(ActivityThread.java:198)
E/AndroidRuntime( 7184):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
E/AndroidRuntime( 7184):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 7184):    at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime( 7184):    at android.app.ActivityThread.main(ActivityThread.java:6837)
E/AndroidRuntime( 7184):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 7184):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 7184):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
E/AndroidRuntime( 7184):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Despite adding the androidextras library to my .pro file, the app seems not to be able to find it. Am I missing something?

gleerman
  • 1,793
  • 4
  • 24
  • 38
  • This other topic may help you: http://stackoverflow.com/questions/4723681/unable-to-dlopenlibsomething-so-cannot-load-library-link-image1995-failed – DYangu May 26 '16 at 11:34

2 Answers2

1

Okay, I think I found it. What I didn't mention is that my project consists of subprojects: project.pro is built first and then included by app.pro and by test.pro. In that case, apparently the statement

QT += androidextras

must be written in all applicable .pro files. Otherwise, qmake won't take it into account and the libraries won't be included during compilation.

Thanks all for your input.

gleerman
  • 1,793
  • 4
  • 24
  • 38
0

Are you sure libQt5AndroidExtras.so was present when you ran your app? If you are using Qt Creator you can take a look at Deploying Applications to Android Devices Qt documentation. E.g. you need to select Deploy local Qt libraries to temporary directory option to deploy Qt libraries when debugging or Bundle Qt libraries in APK when creating an APK.

You can also add to your pro file (androiddeployqt should detect the dependencies though):

ANDROID_DEPLOYMENT_DEPENDENCIES = <path>/libQt5AndroidExtras.so
talamaki
  • 5,324
  • 1
  • 27
  • 40
  • I tried both, but that doesn't do it. From the stack trace, it looks like libQt5AndroidExtras is used by standard QtActivity.onCreate, but the library was omitted by doing QT += androidextras. I tested it, and even without adding the whole JNI stuff above, simply adding QT += androidextras makes the app crash for the reason above. – gleerman May 26 '16 at 15:20