[ Native ]: Using Java functions & 3rd-party libraries in Qt for Android

hich9n picture hich9n · Aug 5, 2013 · Viewed 13.5k times · Source

Recently I use qt to write a android app.
But I have a big question, can I use/call some native android APIS, such as call special activity?

e.g, In Android SDK I call contacts in java:
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT);

Is there a some c++ wrapper libraries for java android sdk APIs?

Or if I need a some special component (that has written with java), can I have it in my qt android program?
Or make a program with mixed pages/activities of "pure qt" and "standard java" or "C++ android NDK" ?

(I ask this question because I have a experience with MOSYNC framework, that was a good one but you have been limited to its own libraries, you couldn't use any external libraries or android standard APIs, ...)

Thanks in advance.

EDIT: according to improvements in JNI ( http://qt-project.org/doc/qt-5/qtandroidextras-index.html), I have a question: can I use 'any' android Java third-party libraries in my qt project? As I use those libraries in Java SDK projects?

Answer

Logan Pickup picture Logan Pickup · Aug 20, 2013

In order to do what you want, you need to call Java code from your C++ code, through JNI. I don't know any C++ wrapper libraries for this. You will need to get the Java VM from JNI_OnLoad, and when you want to call a Java method you will need to attach the VM to your thread, with gives you a JNIEnv, which you can use to find the class you want and call methods on it. This link should help with the details.

As to whether it is worth it to use QT to develop Android applications, yes, if you need cross-platform support and want to deploy the same application to Android, Linux, Windows, etc. If you are only developing Android apps, then in my experience it is easier and faster to use Android directly.