Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt on Android, QAndroidJniObject::callStaticMethod problem

Qt on Android, QAndroidJniObject::callStaticMethod problem

Scheduled Pinned Locked Moved Mobile and Embedded
androidjniandroid studioqandroidjniobje
3 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on 26 Aug 2015, 12:51 last edited by luca
    #1

    Hi all,
    I'm doing some tests on android using Qt.

    The first test is a simple Qt App for Android where I have the following code:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
            jint a = 2;
            jint b = 4;
            jint max = QAndroidJniObject::callStaticMethod<jint>("java/lang/Math", "max", "(II)I", a, b);
            qDebug() << "********** max " << max;
    ...
    ...
    

    and I get the right output.

    Now I'm trying to use a simple custom Qt library "testjni.so" from an Android Studio project.
    The library is created with QtCreator and has the following code:

    static void test1(JNIEnv *env, jobject obj, jint n)
    {
        qDebug() << "****************** test1 " << n;
    }
    static int square(JNIEnv */*env*/, jobject /*obj*/, jint n)
    {
        qDebug() << "****************** square " << n;
        return Testjni::square(n);
    }
    static JNINativeMethod methods[] = {
        { "test1", 
            "(I)V", 
            (void *)test1 },
        { "square",
            "(I)I", 
            (void *)square }
    };
    JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
    {
        qDebug() << "****************** JNI_OnLoad *************************";
    jint a = 2;
            jint b = 4;
            jint max = QAndroidJniObject::callStaticMethod<jint>("java/lang/Math", "max", "(II)I", a, b);
            qDebug() << "********** max " << max;
    
        JNIEnv* env;
        if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)
               != JNI_OK) {
            return JNI_ERR;
        }
        jclass javaClass = env->FindClass("com/my/package/testjniandroid/MyJavaNatives");
        if (!javaClass) {
            return JNI_ERR;
        }
        if (env->RegisterNatives(javaClass, methods,
                                sizeof(methods) / sizeof(methods[0])) < 0) {
            return JNI_ERR;
        }
        return JNI_VERSION_1_6;
    }
    
    Testjni::Testjni()
    {
    
    }
    
    int Testjni::square(int val)
    {
        qDebug() << "****************** Testjni::square " << val;
        return val*val;
    }
    
    

    and the Android part is:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            System.loadLibrary("testjni");
        }
    

    Now when I start the Android application it load the native library and call JNI_OnLoad but when it execute QAndroidJniObject::callStaticMethod it crash without useful messages.

    Where do I wrong?

    Thanks

    Luca

    1 Reply Last reply
    0
    • B Offline
      B Offline
      benlau
      Qt Champions 2016
      wrote on 26 Aug 2015, 13:27 last edited by benlau
      #2

      I am not an expert in JNI. But I have written an Android unit test program to execute Qt code. Java code send message to C++ code via JNI and vice versa. It is working fine.

      My code:
      quickandroid/ExampleActivityTest.java at DEV · benlau/quickandroid

      However, I use ActivityInstrumentationTestCase2 which depend on an existing .apk. C++ code is launched by QtActivity.java instead of calling System.loadLibrary manually. May be the QtActivity has setup something for getting JNI to load Java class correctly?

      L 1 Reply Last reply 26 Aug 2015, 14:47
      0
      • B benlau
        26 Aug 2015, 13:27

        I am not an expert in JNI. But I have written an Android unit test program to execute Qt code. Java code send message to C++ code via JNI and vice versa. It is working fine.

        My code:
        quickandroid/ExampleActivityTest.java at DEV · benlau/quickandroid

        However, I use ActivityInstrumentationTestCase2 which depend on an existing .apk. C++ code is launched by QtActivity.java instead of calling System.loadLibrary manually. May be the QtActivity has setup something for getting JNI to load Java class correctly?

        L Offline
        L Offline
        luca
        wrote on 26 Aug 2015, 14:47 last edited by
        #3

        @benlau
        Thanks,
        it probably depend on QtActivity that set something as you said but can't find the solution...

        1 Reply Last reply
        0

        1/3

        26 Aug 2015, 12:51

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved