Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-88469

Android build crashed When use Jni_load and native class on QT 5.11

    XMLWordPrintable

Details

    • Bug
    • Resolution: Incomplete
    • P1: Critical
    • None
    • 5.11.0
    • QPA
    • Phone System: Android 9
      QT version : Qt 5.11
    • Android
    • 3

    Description

      First, talk about my work environment, they are Qt 5.11, NDK-r10e, and sdk-tools-windows-4333796. And then based on this environment, I make a demo to verify the idea of  use JNI method to link Java and C++ method. When I finish the code, compile it, that is no error. But build it on the phone, there is one mistake. It is like this:

      java.lang.UnsatisfiedLinkError: No implementation found for void org.qtproject.example.DataNatives(int, int)....
      

      I read the issue https://bugreports.qt.io/browse/QTBUG-68488, but I don't know where I should add the "System.loadLibrary("TestAndroid_5_11")".  (libTestAndroid_5_11  is the *so file name of my program.).

      Here is how I do that: 

      • First, I use JNI_OnLoad method to register the native method, like this:
          jint JNI_OnLoad(JavaVM* vm, void* reserved)
          {
              Q_UNUSED(reserved);
              JNIEnv* env;
              if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
              {
                  return -1;
              }
              JNINativeMethod javaMethods[]{
                  {"nativeSendData", "(II)V", reinterpret_cast<void*>(jniSendData)}
              };
          
              QAndroidJniEnvironment jniEnv;
              if (jniEnv->ExceptionCheck())
              {
                  jniEnv->ExceptionDescribe();
                  jniEnv->ExceptionClear();
              }
          
              jclass objectClass = jniEnv->FindClass("qtTest/DataNatives");
              if(!objectClass)
              {
                  qWarning() << "setNativeMethods....Couldn't find class:" << kJniClassName;
                  return;
              }
          
              jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));
          
              if (val < 0)
              {
                  qWarning() << ":setNativeMethods....Error registering methods: " << val;
              }
              else
              {
                  qDebug() << "setNativeMethods....Native Functions Registered";
              }
          
              if (jniEnv->ExceptionCheck())
              {
                  jniEnv->ExceptionDescribe();
                  jniEnv->ExceptionClear();
              }
      
              return JNI_VERSION_1_6;
          }
      
      
      • then, I make the java method ,like this:
        package qtTest;
        
        import android.os.Bundle;
        import android.util.Log;
        
        import org.qtproject.qt5.android.bindings.QtActivity;
        
        class DataNatives
        {
            public static native void nativeSendData(int val1, int val2);
        }
        
        public class TestActivity extends QtActivity
        {
            @Override
            protected void onResume()
            {
                super.onResume();
        
                Log.i("TestActivity","onResume");
                DataNatives.nativeSendData(1, 2);
            }
        
        
            @Override
            public void onWindowFocusChanged(boolean hasFocus)
            {
                Log.i("TestActivity","onWindowFocusChanged");
                DataNatives.nativeSendData(3, 4);
                super.onWindowFocusChanged(hasFocus);
            }
        }
        
      • Last, I make the C++ method, like this:
        static void jniSendData(JNIEnv *envA, jobject thizA, jint val1, jint val2)
        {
            Q_UNUSED(thizA);
            if (val1 != 0)
            {
                qDebug()<<"jniSendData----val1="<<val<<",val2="<<val2;
                envA->ReleaseByteArrayElements(val1, val2, JNI_ABORT);
            }
        }

        So how can I solve this problem?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qtandroidteam Qt Android Team
            sunrise hu xiaofei
            Qt Android Team Qt Android Team
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes