Results 1 to 5 of 5

Thread: Cannot get JNI to find my custom Java class from my Qt Android app

  1. #1
    Join Date
    Sep 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Cannot get JNI to find my custom Java class from my Qt Android app

    Hello

    I am trying to use JNI from my Android Qt C++ app so that I can access the Android Bluetooth functionality via a simple Java class that I wrote.

    While I am able to successfully use FindClass() to get the standard Android and Java classes (e.g. android/bluetooth/BluetoothDevice, java/lang/String, etc...), I cannot seem to get it to find my Java class.

    Does anybody out there have any idea what I’m doing wrong?

    Any help would be greatly appreciated
    Thank you
    Scott



    Here is some simple test code that demonstrates my problem:

    I place my Java test class in the same source folder and package (i.e. <proj>/android/src/org/qtproject/qt5/android/bindings ) as QtApplication.java and QtActivity.java. It appears to be properly compiled, dex’ed, and placed in the package before being sent up to the device to be executed.

    Qt Code:
    1. //****************** Here is my Java class that I want to talk to from my Qt app: *********************
    2. package org.qtproject.qt5.android.bindings;
    3.  
    4. public class JniTest
    5. {
    6. public int GetNum( )
    7. {
    8. return 5;
    9. }
    10. }
    11. //*****************************************************************************************************
    12.  
    13. //***************** Here is my C++ code that attempts to find my Java class **********************
    14. #include <jni.h>
    15. #include <android/log.h>
    16. #include <QtGlobal>
    17.  
    18. #define dbprintf(...) __android_log_print(ANDROID_LOG_VERBOSE, "JniTest", __VA_ARGS__)
    19.  
    20. JavaVM *vm = NULL;
    21. JNIEnv *env = NULL;
    22.  
    23. //*******************************
    24. jint JNI_OnLoad(JavaVM *vmIn, void *reserved)
    25. {
    26. dbprintf( "JNI_OnLoad: enter" );
    27. vm = vmIn;
    28. return JNI_VERSION_1_6;
    29. }
    30.  
    31. //*******************************
    32. void JniTest( )
    33. {
    34. qDebug( "JniTest enter: jvm=%08x", (int)vm );
    35. if( !vm )
    36. return;
    37.  
    38. JavaVMAttachArgs args = { JNI_VERSION_1_6, NULL, NULL };
    39. vm->AttachCurrentThread( &env, &args );
    40.  
    41. qDebug( "JniTest enter: env=%08x", (int)env );
    42. if( !env )
    43. return;
    44.  
    45. // Getting a standard Java class succeeds
    46. CreateClass( "java/lang/String" );
    47.  
    48. // Getting my Java class fails
    49. CreateClass( "org/qtproject/qt5/android/bindings/JniTest" );
    50.  
    51. // Getting known Qt java classes also fail
    52. CreateClass( "org/qtproject/qt5/android/QtNative" );
    53. CreateClass( "org/qtproject/qt5/android/bindings/QtActivity" );
    54. }
    55.  
    56. //*******************************
    57. jclass CreateClass( const char *name )
    58. {
    59. jclass cls = env->FindClass( name );
    60. jclass gcls = NULL; //cls ? (jclass)env->NewGlobalRef( cls ) : NULL;
    61. qDebug( "JniTest CreateClass: name=%s, cls=%08x gcls=%08x", name, (int)cls, (int)gcls );
    62. return gcls;
    63. }
    64.  
    65. //*****************************************************************************************************
    To copy to clipboard, switch view to plain text mode 
    //*********************** Here is the resulting output from my test code ******************************
    I/Qt ( 5276): qt start
    D/dalvikvm( 5276): Trying to load lib /data/app-lib/com.pasco.QtAndroidJniTest-1/libQtAndroidJniTest.so 0x42094680
    D/dalvikvm( 5276): Added shared lib /data/app-lib/com.pasco.QtAndroidJniTest-1/libQtAndroidJniTest.so 0x42094680
    V/JniTest ( 5276): JNI_OnLoad: enter
    W/Qt ( 5276): ..\src\androidjnimain.cpp:449 (jboolean startQtApplication(JNIEnv*, jobject, jstring, jstring)): Can't set environment ""
    W/Qt ( 5276): kernel\qcoreapplication.cpp:412 (QCoreApplicationPrivate::QCoreApplicationPrivate( int&, char**, uint)): WARNING: QApplication was not created in the main() thread.
    W/dalvikvm( 5276): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtNativeInputConnection'
    W/dalvikvm( 5276): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtExtractedText'
    D/Qt ( 5276): ..\QtAndroidJniTest\mainwindow.cpp:60 (void JniTest()): JniTest enter: jvm=41aa40c0
    D/Qt ( 5276): ..\QtAndroidJniTest\mainwindow.cpp:67 (void JniTest()): JniTest enter: env=5bbba0e0
    D/Qt ( 5276): ..\QtAndroidJniTest\mainwindow.cpp:53 (_jclass* CreateClass(const char*)): JniTest CreateClass: name=java/lang/String, cls=1d200001 gcls=00000000
    D/Qt ( 5276): ..\QtAndroidJniTest\mainwindow.cpp:53 (_jclass* CreateClass(const char*)): JniTest CreateClass: name=org/qtproject/qt5/android/bindings/JniTest, cls=00000000 gcls=00000000
    D/Qt ( 5276): ..\QtAndroidJniTest\mainwindow.cpp:53 (_jclass* CreateClass(const char*)): JniTest CreateClass: name=org/qtproject/qt5/android/QtNative, cls=00000000 gcls=00000000
    D/Qt ( 5276): ..\QtAndroidJniTest\mainwindow.cpp:53 (_jclass* CreateClass(const char*)): JniTest CreateClass: name=org/qtproject/qt5/android/bindings/QtActivity, cls=00000000 gcls=00000000
    //************************************************** ************************************************** *

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot get JNI to find my custom Java class from my Qt Android app

    Please ask Java questions on some Java forum and not on a forum devoted to Qt. We're not Java specialists.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Cannot get JNI to find my custom Java class from my Qt Android app

    This *is* a Qt question.

    I have successfully been able to do this in a pure non-Qt Android app using the Android NDK.

    Because:
    * This forum is for Qt running on Embedded and Mobile devices (which I assume includes Android devices)
    * The Qt designers needed to sprinkle a little Java into their implementation (due to Android's java-centricity)
    * The Qt library source code actually uses the JNI interface

    I was assuming that they (or someone else in this forum) might have some insight that I do not have.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot get JNI to find my custom Java class from my Qt Android app

    Quote Originally Posted by scottwild View Post
    This *is* a Qt question.
    All I can see is a Java class and some JNI code where the only relation to Qt is the use of qDebug().

    Because:
    * This forum is for Qt running on Embedded and Mobile devices (which I assume includes Android devices)
    * The Qt designers needed to sprinkle a little Java into their implementation (due to Android's java-centricity)
    * The Qt library source code actually uses the JNI interface

    I was assuming that they (or someone else in this forum) might have some insight that I do not have.
    I really think you'll get help quicker on some Java site. We're C++ programmers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Cannot get JNI to find my custom Java class from my Qt Android app

    I found the problem.

    I need to call FindClass from within JNI_OnLoad instead of from the context of my button press handler.

Similar Threads

  1. Replies: 4
    Last Post: 10th July 2014, 15:22
  2. How do I add Java code to Qt Android apps via Qt Creator
    By scottwild in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 1st October 2013, 23:09
  3. Replies: 7
    Last Post: 18th August 2011, 14:43
  4. java class
    By mickey in forum General Programming
    Replies: 2
    Last Post: 19th July 2009, 15:49
  5. is there a class in QT4 like robot class of java
    By sathyanarayanan in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2009, 11:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.