I have gone through all the Android NDK tutorials and built a few simple examples. It seems to me that every example out there from Android Studio revolves around creating a native library attached to an Android application directly. I need to create a native library that is attached to an already existing Android Library. I don't see an example to do this. Is it even possible to hang a native C++ library off an already existing Android Java Library, so the Android Library has access to those native methods, without attaching both libraries (android and native) to a specific application?
Tried in Android Studio to attach a C++ native library to an already existing Android Library, and used CMake as compiler choice, the gradle does not seem to support. The CMakeLists.txt does not seem to be recognized, the "includes" folder is not generated to support the .cpp in the cpp folder, and the native library generates no links I can see to the Android Library methods.
Related
I am a native Android developer, and a client has asked me to re-use his Qt library project in a native Android app. I am really new to Qt development, I mean, I understand C/C++ but have never dealt with Qt development itself, neither the environment or the deployment methods.
I have been looking into these solutions, but I don't get any closer to being able to create an Android project with a Qt library inside with which I can interact.
Similar solutions:
http://thebugfreeblog.blogspot.com/2012/05/embedded-cross-platform-development-for.html
https://developer.android.com/studio/projects/add-native-code#create-sources
My question: How do I export my Qt project to a usable format for my Android project? Any step-by-step guide would be really appreciated.
Thanks!
Assuming your Qt application is only used for its logic (not the GUI side), what you need it to:
Adding JNI to your Qt library for Android
JNI (Java Native Interface) allows creating an interface to link and use native (C++) code from Java. This is used to wrap your C++ code and provide functions visible and usable directly from your Android application
Install the version of Qt you want for Android using Qt's online installer (a checkbox when choosing the version of Qt you want)
Create a JNI interface to allow your application and the library to talk. This is covered in that sample: https://developer.android.com/ndk/samples/sample_hellojni. I would probably recommend creating another C++/JNI library acting as a wrapper for your C++/Qt library. That way you don't contaminate the original library with JNI dependencies.
Integrate your JNI library in your application
Once your have a JNI interface, you have to cross-compile and ship that library with your Android application
Build your native library from Android Studio (after pointing to your Qt libraries and include dir). Alternatively, you can manually build it from Qt Creator (slightly easier) and import only the resulting .so. But that will be painful if your Qt library changes
Package the library (.so), and load it when your application starts. This is also covered in the sample. Once the library is loaded, you will be able to use the JNI functions that you exposed and pass arguments, after some conversion work. Your Android application can also pass a handle to the native library, allowing communication in both ways.
Notes
Be careful to the licensing of your project. Cross-compiling Qt for Android will likely switch to its GPLv3 or Commercial license, versus LGPLv3 without Android.
Qt libraries have to be included in your final pacakge. So they either have to be statically linked against your original library, or dynamically linked, and stored in your APK
You can leverage Qt JNI classes to make the creation of the interface easier, but that is not required: https://doc.qt.io/qt-5/qandroidjniobject.html
Threading can become difficult, as calls to/from JNI have to run from the main thread
I would recommend starting from a working example if you find one, as that whole setup will definitely take some time. I don't know of a complete tutorial covering that, but feel free to share if you find one!
Background
Although, older versions of OpenCV (ie 2.4) allow Android projects to utilize `SIFT` functionality, the separation of that functionality into `opencv_contrib` makes the task more difficult; integrating a `OpenCV-3.x.x-android-sdk` module into a project leaves you without the ability to use the `SIFT` functionality.
FeatureDetector.create(FeatureDetector.DYNAMIC_SIFT); // Fails
//OpenCV Error: Bad argument (Specified feature detector type is not supported.)
Using External Tools
Although, there are techniques such as [Gouhui Wang's][1] that describe how to build the nonfree portion of OpenCV into an Android project, that process requires external tools. This question is about how to get the same result, but instead leverage the power and convenience of the Android Studio / InteliJ.
Using CMake in Android Studio
Modern Android Studio versions have useful functionality:
Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI)
As indicated in this stackoverflow question and answer concerning building OpenCV with C++ support, it is possible to quickly integrate OpenCV 3.1 (and I suspect other versions) of opencv4android into the C++ build structure that's generated in the new application wizard.
I suspect that someone that understands the details of building projects could utilize Building_OpenCV4Android_from_trunk to answer this question. My expertise in this area is limited, thus, the question.
What specific steps would be required in order to get the nonfree portion of OpenCV to build completely within the native Android Studio build process?
I'm currently working on generating a static C++ library to wrap with JNI use with Android. Using Visual Studio 2015's new support for Android C++ libraries, I was able to generate a dynamic library (.so), and pulled it into Android Studio. Once it was in the proper directory, it was able to link properly, and I was able to call a native method.
However, doing exactly the same thing, but generating a static library (.a), I continuously got UnsatisfiedLinkError, indicating the library was not found by the app. Everything else was controlled for (i.e. same build settings, same location of the library in the file system, etc).
Has anybody else tried doing this? Is there something special that needs to be done to get Android Studio to recognize .as?
Turns out Android Studio just doesn't support Java 8 yet (and with it, support for linking static C++ libraries). So Microsoft is a little ahead of the game with their support for creating those static libraries. Guess it's dynamic linking for the time being.
Like I want to convert a C++ project that I made in VS to an android app. And I found out that there is a Visual C++ android development kit available in VS. So will there be any need of using Java? (as building an android app usually requires)
You can indeed create a Visual Studio project that uses the Android NDK to build an Android app from pure C++ code. The C++ code is compiled into a library that a little bit of Java glue code calls into, and this combination, along with your resources, is packaged into an .APK that you can install. You don't have to edit or otherwise modify the Java code at all; all your program logic can be in C++.
Technically speaking, you can't easily convert an existing VS project into an Android project, but you can copy your code into a new project that creates an Android app, or copy it into a shared code project that is picked up by a project that creates a library for Android that you can call from an app written in either Java or C++. The project templates in VS give simple examples of doing each of these things.
I'm desperately trying to make an application for Android using Android Studio.
Here is my problem:
I'm using a custom android rom on my Pandaboard, and I created a framework extension (java library) which is included in my rom's system.
That means that I have :
/system/framework/com.stooit.MyLib.jar
/system/etc/permissions/com.stooit.MyLib.xml registering my lib.
However, I just can't find a way to include it Android Studio and compile my application.
I tried to include the generated .jar of my lib into Android Studio, however the content is not recognized (the jar is mainly composed of a classes.dex that contain my lib).
Then, I tried to make a new jar with eclipse of my lib, then include it in Android Studio, mark the library as "provided" and then run my application on my board. However, no luck, it just crashes. It doesn't manage to locate the library.
So that's why I'm asking you: Is it even possible to do what I'm trying to do?
The other way I have (but I'd like not to use it) is to copy/paste by android application into my android sources, make an Android Makefile, import my library and build the app with my rom.
Thanks for your help.