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++
andC++Builder / FireMonkey
. If I chooseQAndroidExtras
, I can see mismatch andQAndroidExtras
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:
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 forQt
?
Also doesQt
runs withJavaVM (JNI_CreateJavaVM)
onAndroid
? So, I have to create a newJavaVM
with pure JNI? Thanks. -
@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
withoptions.optionString
, because I set my path.
Thanks. -
@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
andlibjvm.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 usejni.h
andlibjvm.so
files for Android ARMv7? -
@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. -