How can I run Java Codes in Qt/C++ without QAndroidExtras?



  • Hi; I'm using Qt 5.7 on Ubuntu 16.04, my Java version is Oracle Java 8. I have got a Java class, it's here:

    package com.pack;
    
    import android.content.Context;
    import android.widget.Toast;
    
    public class AndroidClass
    {
      public static void runToast(Context context)
      {
        Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
      }
    }
    

    I can running this class with QAndroidExtras like this:

    QtAndroid::runOnAndroidThread([]
    {
      QAndroidJniObject::callStaticMethod<void>("com/pack/AndroidClass",
                                                "runToast",
                                                "(Landroid/content/Context;)V",
                                                QtAndroid::androidActivity().object());
    });
    

    But I need to use this Java class with pure JNI (jni.h). I have to choose pure Jni, because I want to use my JNI code both Qt / C++ and C++Builder / FireMonkey. If I choose QAndroidExtras, I can see mismatch and QAndroidExtras license is LGPL license. So, I have to use pure JNI. I wrote a C++ code with pure JNI on Qt, it's here:

    QtAndroid::runOnAndroidThread([]
    {
        JavaVM* jvm;
        JNIEnv* env;
        JavaVMInitArgs jvm_args;
        JavaVMOption options[1];
    
        options[0].optionString = "-Djava.class.path=android/src";
        jvm_args.version = JNI_VERSION_1_8;
        jvm_args.options = options;
        jvm_args.nOptions = 1;
        jvm_args.ignoreUnrecognized = JNI_TRUE;
    
        jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &jvm_args);
        if (res < 0)
        {
          qDebug() << "Cannot create JVM!\n";
          exit(1);
        }
    
        jclass class_ = env->FindClass("com/pack/AndroidClass");
        if (class_ == 0)
        {
          qDebug() << "Class not found!\n";
          exit(1);
        }
    
        jmethodID method_id = env->GetStaticMethodID(class_, "runToast", "(Landroid/content/Context;)V");
        if (method_id == 0)
        {
          qDebug() << "runToast() method not found!\n";
          exit(1);
        }
    
        env->CallStaticVoidMethod(class_, method_id, QtAndroid::androidActivity().object());
    });
    

    But when I ran this project on Android, I get the errors:
    alt text
    Whereas I included the necessary commands in the .pro file:

    android: {
        QT += androidextras
        INCLUDEPATH += /usr/lib/jvm/java-8-oracle/include
        INCLUDEPATH += /usr/lib/jvm/java-8-oracle/include/linux
        LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/ -ljvm
    }
    

    How can I run Java codes in Qt with pure JNI for Qt?
    Also does Qt runs with JavaVM (JNI_CreateJavaVM) on Android? So, I have to create a new JavaVM with pure JNI? Thanks.


  • Moderators

    @Ibrahim Are you using 64bit Qt and compiler? I guess the JDK is 32bit and you're trying to use its lib in a 64bit application. Both must be either 32bit or 64bit, but not a mix.



  • @jsulm thanks, I'm using 64Bit Oracle Java 8. When I compiled code for Desktop 64bit, it is successfuly. But when I compiled code for Android ARMv7 or x86, it gets like the image. (undefined reference to JNI_CreateJavaVM etc.).


  • Moderators

    @Ibrahim So, you're trying to use a library which is built for x86_64 for an ARM build? This is not going to work.



  • @jsulm I found this answer: Link. He said: "you cannot create JavaVM on Android, your app is already running inside a JVM." Well, how can I find running JavaVM for Qt Android app? Also how can I use JavaVM of Qt Android app with pure Jni? I found this: JavaVM *QAndroidJniEnvironment::javaVM(). But I need to add -Djava.class.path=/Java/Sources/Path with options.optionString, because I set my path.
    Thanks.


  • Moderators

    @Ibrahim Qt apps are not running in an Java VM as they are native applications written in C++.
    I don't know whether it is possible to somehow use a Java VM from a Qt app on Android, maybe somebody else knows.



  • @jsulm said in How can I run Java Codes in Qt/C++ without QAndroidExtras?:

    Qt apps are not running in an Java VM as they are native applications written in C++.

    I know it, but we can calling / running Android SDK with Java code, I think Java codes are running on JavaVM. Otherwise how do run calling Java codes in Qt with QAndroidExtras?



  • I found new jni.h and libjvm.so positions:
    jni.h positions:

    /usr/lib/jvm/java-8-oracle/include/jni.h
    
    Android/Sdk/ndk-bundle/platforms/android-*/arch-arm/usr/include/jni.h
    
    android-studio/jre/include/jni.h
    

    libjvm.so positions:

    /home/username/android-studio/jre/jre/lib/amd64/server/libjvm.so
    
    /usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so
    

    Also I updated my .pro file like this:

    #.pro File
    
    # FOR ANDROID:
    android: {
        QT += androidextras
        INCLUDEPATH += /home/username/Android/Sdk/ndk-bundle/platforms/android-19/arch-arm/usr/include/
    }
    
    # FOR LINUX-G++ (DESKTOP):
    linux-g++: {
        INCLUDEPATH += /usr/lib/jvm/java-8-oracle/include
        INCLUDEPATH += /usr/lib/jvm/java-8-oracle/include/linux
        LIBS += -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/ -ljvm
    }
    

    But I get this error: JNI_CreateJavaVM was not declared in this scope.
    Which should I use jni.h and libjvm.so files for Android ARMv7?


  • Moderators

    @Ibrahim I think /home/username/android-studio/jre is the JRE used by AndroidStudio itself (which is a Java application) - it is NOT for the target device!
    amd64 means it is for X86_64 and not ARM.
    You cannot use it on most Android devices.
    Is there libjvm.so in Android/Sdk/ndk-bundle/platforms/ ...?
    You really should ask in an Android developer forum or mailing list as your problem is not Qt related.



  • @jsulm thanks. I used this command: locate "libjvm.so" in terminal, and I see 2 libjvm.so positions. There is not libjvm.so in android-19/include.
    I asked question in Android NDK google+ page. Do you know a platform for this question?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.