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

13.7k Views Asked by At

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?

3

There are 3 best solutions below

1
On

You can program in C/C++ using the Android NDK. You'll have to wrap your c++ codebase in a static library and load that through a Java wrapper & JNI.

The standard NDK does not support RTTI and a lot of the functionality of standard c++ is also not available such as std::string, etc. To solve this you can recompile the NDK. Dmitry Moskalchuk supplies a modified version of the NDK that supports this at http://www.crystax.net/android/ndk-r3.php. This modified version works on all Android phones that run on an ARM processor.

Note:JNI calls can decrease your performance by a lot. Java isn't that hard tho -- if you know how to program in C/C++. The advantage of Java is that the application can run on any device running Android, where as NDK applications depend on the architecture it was compiled for.

0
On

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.

1
On

I am not sure how you are proceeding with "Qt for Android". But at this point of time, this idea is officially supported by the KDE team (K Desktop Environment). They call it KDE Necessitas project. And they have released the first beta of this project on October 2012.

So basically when you install the Necessitas SDK, you probably get these things installed.

  1. Qt Creator - The Qt IDE customized for Android.
  2. Android NDK - For building the shared object out of the Qt code.
  3. Android SDK - Customized with Qt support.

Using the customized Qt Creator, you can

  1. Create an Android UI project.
  2. Design the UI in integrated Qt Designer.
  3. Compile the C++ code to build the .so file and wrap it with the Java code.
  4. Create the APK and deploy.
  5. Debug the APK.

One major concern I have with Qt for Android is regarding the UI. The UI have the native look and feel of KDE 4.x (The same which runs on desktops). The android UI themes (Holo etc.) are not fully available yet.

And since this project is still beta, I am not sure about the availability of wrapper classes/functions for the Android native features. Please go through the official documentation for more details.

Hope this helps.