//****************** Here is my Java class that I want to talk to from my Qt app: *********************
package org.qtproject.qt5.android.bindings;
public class JniTest
{
public int GetNum( )
{
return 5;
}
}
//*****************************************************************************************************
//***************** Here is my C++ code that attempts to find my Java class **********************
#include <jni.h>
#include <android/log.h>
#include <QtGlobal>
#define dbprintf(...) __android_log_print(ANDROID_LOG_VERBOSE, "JniTest", __VA_ARGS__)
JavaVM *vm = NULL;
JNIEnv *env = NULL;
//*******************************
jint JNI_OnLoad(JavaVM *vmIn, void *reserved)
{
dbprintf( "JNI_OnLoad: enter" );
vm = vmIn;
return JNI_VERSION_1_6;
}
//*******************************
void JniTest( )
{
qDebug( "JniTest enter: jvm=%08x", (int)vm );
if( !vm )
return;
JavaVMAttachArgs args = { JNI_VERSION_1_6, NULL, NULL };
vm->AttachCurrentThread( &env, &args );
qDebug( "JniTest enter: env=%08x", (int)env );
if( !env )
return;
// Getting a standard Java class succeeds
CreateClass( "java/lang/String" );
// Getting my Java class fails
CreateClass( "org/qtproject/qt5/android/bindings/JniTest" );
// Getting known Qt java classes also fail
CreateClass( "org/qtproject/qt5/android/QtNative" );
CreateClass( "org/qtproject/qt5/android/bindings/QtActivity" );
}
//*******************************
jclass CreateClass( const char *name )
{
jclass cls = env->FindClass( name );
jclass gcls = NULL; //cls ? (jclass)env->NewGlobalRef( cls ) : NULL;
qDebug( "JniTest CreateClass: name=%s, cls=%08x gcls=%08x", name, (int)cls, (int)gcls );
return gcls;
}
//*****************************************************************************************************
//****************** Here is my Java class that I want to talk to from my Qt app: *********************
package org.qtproject.qt5.android.bindings;
public class JniTest
{
public int GetNum( )
{
return 5;
}
}
//*****************************************************************************************************
//***************** Here is my C++ code that attempts to find my Java class **********************
#include <jni.h>
#include <android/log.h>
#include <QtGlobal>
#define dbprintf(...) __android_log_print(ANDROID_LOG_VERBOSE, "JniTest", __VA_ARGS__)
JavaVM *vm = NULL;
JNIEnv *env = NULL;
//*******************************
jint JNI_OnLoad(JavaVM *vmIn, void *reserved)
{
dbprintf( "JNI_OnLoad: enter" );
vm = vmIn;
return JNI_VERSION_1_6;
}
//*******************************
void JniTest( )
{
qDebug( "JniTest enter: jvm=%08x", (int)vm );
if( !vm )
return;
JavaVMAttachArgs args = { JNI_VERSION_1_6, NULL, NULL };
vm->AttachCurrentThread( &env, &args );
qDebug( "JniTest enter: env=%08x", (int)env );
if( !env )
return;
// Getting a standard Java class succeeds
CreateClass( "java/lang/String" );
// Getting my Java class fails
CreateClass( "org/qtproject/qt5/android/bindings/JniTest" );
// Getting known Qt java classes also fail
CreateClass( "org/qtproject/qt5/android/QtNative" );
CreateClass( "org/qtproject/qt5/android/bindings/QtActivity" );
}
//*******************************
jclass CreateClass( const char *name )
{
jclass cls = env->FindClass( name );
jclass gcls = NULL; //cls ? (jclass)env->NewGlobalRef( cls ) : NULL;
qDebug( "JniTest CreateClass: name=%s, cls=%08x gcls=%08x", name, (int)cls, (int)gcls );
return gcls;
}
//*****************************************************************************************************
To copy to clipboard, switch view to plain text mode
//*********************** Here is the resulting output from my test code ******************************
Bookmarks