Unsolved Cannot call from Java to C++
-
I have created a minimal Android app that call from Java to C++ in order to learn how to do that. This is the code:
Android App (Main Activity):
package com.example.qttestexample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.example.libexample.MyClass; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyClass foo = new MyClass(); foo.printHello(); } }
AAR package where I place native methods:
package com.example.libexample; class MyJavaNatives { static { try { System.loadLibrary("QtLibExample"); } catch( UnsatisfiedLinkError e ) { System.err.println("!!! Native code library failed to load.\n" + e); } } public static native void printHello(); } public class MyClass { public static void printHello() { MyJavaNatives.printHello(); } }
C++ / QT code:
#ifndef QTLIBEXAMPLE_H #define QTLIBEXAMPLE_H #include "QtLibExample_global.h" #include <jni.h> #include <QDebug> static void printHello(JNIEnv */*env*/, jobject /*obj*/) { qDebug() << "Hello World"; } static JNINativeMethod methods[] = { { "printHello", "(V)V", // const char* function signature (void *)printHello // function pointer } }; extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) { JNIEnv* env; // get the JNIEnv pointer. if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { return JNI_ERR; } // step 3 // search for Java class which declares the native methods jclass javaClass = env->FindClass("com/example/libexample/MyJavaNatives"); if (!javaClass) return JNI_ERR; // step 4 // register our native methods if (env->RegisterNatives(javaClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { return JNI_ERR; } return JNI_VERSION_1_6; } #endif // QTLIBEXAMPLE_H
When I execute it, the app crash and I get the following:
2020-04-24 11:59:12.517 28131-28131/com.example.qttestexample W/System.err: java.lang.UnsatisfiedLinkError: dlopen failed: library "libQt5Core.so" not found
My library (.so) is placed in libexample/src/main/jniLibs/x86
I can understand from the message that the dependency libQt5Core.so is not resolved, as I do not have this library copied in any place and it is referenced from my lib when using for example QDebug but my question is, how can I solve it? what I need to add to my project in Android Studio or Qt and how to do that?
Thanks in advance.