I want to use C++ file in my android application via JNI but i don't know how to integrate C++ file and how to Compile and run it.
Please help me if anybody knows about it
You need to do NDK programming. Android NDK
The Android NDK is a toolset that lets you embed components that make use of native code in your Android applications.
Android applications run in the Dalvik virtual machine. The NDK allows you to implement parts of your applications using native-code languages such as C and C++. This can provide benefits to certain classes of applications, in the form of reuse of existing code and in some cases increased speed.
You can use Android NDK for your requirements.
Android-NDK
The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can be helpful so that you may reuse existing code libraries written in these languages and possibly increased performance.
Tutorial Advanced Android: Getting Started with the NDK
Using Android NDK create shared library of your C,C++ files and load that library at runtime from your Android application. Look at tutorial I linked.
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!
I am using LibGDX to write apps for both Android and iOS and I want to be able to add C++ code to my apps to optimize certain parts and to port some functions etc.
I have been searching the internet and tried to follow some tutorials, but did not find what I need.
How can I write a very basic C++ library which I can load in LibGDX? Which tools do I need to use? Visual Studio? I develop in Android Studio.
I think that I need an .so file for Android and an .a file for iOS, is that correct?
On both platforms, it's possible to include a precompiled library as well as C++ source code directly.
On Android, you'll want to look into using the Android NDK. This allows you to include native C/C++ code that can bridge over to Java. The connection between Java and C/C++ is managed with the JNI. It's a fairly tedious, awkward system for communicating between C++ and Java. You'll want to look into setting up an Android.mk makefile that specifies how to include your library (or source code) into your build.
On iOS, it's a little more tightly linked. You can have Objective-C++ files that can run both C++ and Objective-C code. If you're using Swift, it's a little different (bridging between Objective-C++ and Swift).
In some cases, when the platform (Android/iOS) provides functionality that is superior to what is possible or realistic with C++, you might find yourself architecting the code such that your C++ can reach out to the platform as needed. This means that you might have headers with separate implementation files per platform.
thing.h
thing_android.cpp
thing_ios.mm
The android app's Android.mk file will include thing_android.cpp (but not thing_ios.mm). This file could cross the JNI bridge to talk to Java as needed, whenever you need something from Android SDK.
The iOS app will include thing_ios.mm (but not thing_android.cpp). The .mm extension signifies Objective-C++, so that file could directly call powerful Cocoa libraries as needed.
Finally, on all platforms, you'll want to be sure to either scale back your usage of C++ to the lowest common denominator platform. In other words, if iOS supports a particular feature of C++, and Android doesn't, then you cannot use that particular feature.
I have been wondering if it is possible to use all the functions of opencv in android opencv. I am planning to do my PG project in android Open CV. Because i am familiar with android but not opencv. Is it possible to implement all the functions used in opencv (windows) to Android opencv?
Unfortunately, Java API doesn't provide access to all the functions as in the case of C++ API. First, I will discuss what all the toolkits that are needed for doing so and then how can we use the native(C / C++) in Android.
Toolkits Needed
To develop Android apps using OpenCV Library, we need the following tools (all of these are either Open Source or free software, or both) -
Eclipse with ADT Bundle (You can choose any other IDE like Android Studio)
OpenCV4Android (although, you can compile from source code, it is recommended for novice users to download the binaries.)
CygWin (Only for Windows, not needed on Linux or Mac)
Java JDK (JRE would not be sufficient)
Android Native Development Toolkit (NDK) (needed to run native C/C++ code)
You will need to set environment variables on your OS for the tools to correctly configure.
Alternatively, nVidia also provides a suite of developing tools — TADP. The advantage of TADP over the piece-by-piece method is that you don’t need to follow the often perplexing task of setting up the development environment. The default installation will download a lot of superfluous packages that are not needed (The download size can be greater than 2GB and on slow internet connections, it can turn into a prolonged activity.), rather select the packages manually at the time of download (A dialog box will appear, asking you to select either Complete, Custom or Manually.)
Using C/C++ code in Android
You can get access to native C/ C++ functionality in Android by
Here is the official OpenCV tutorial on setting up .
Also, Check out the Mixed Processing Sample in the Samples folder of Android4OpenCV folder. This example shows how you can pass an image and its grayscale equivalent to a native function and detect features on the image using FAST features detection.
Useful books and tutorials
There is not much study material right now, but I guess these 2 source are the best -
Android Application Programming with OpenCV by Packt
Publishing.
Sample programs in the OpenCV4Android folder.
Yes it is definitely possible to use all funtions in OpneCV on Android. But you have to note that OpenCV4Android includes 2 parts: the Java part and the native part.
In fact the Java part provides most of the functions of the OpenCV library. If you would like to have access to more functionalities and faster speed, then just go for native development. The .so library in the OpenCV SDK pack provides all native functions. In fact if you are familiar with native development, you could even directly use the OpenCV c++ library, just like how people use FFMPEG on android.
I can cross-compile any C/C++ application, statically link it Linux libraries and run it on Android. What was the need of an Android-ndk then? Android-ndk limits us to bionic which has a small subset of gnu libc. Isn't it a better idea to straightaway cross-compile applications and run them through Android shell? Is there any limitation to cross-compiling that I can't see? This URL : Can Linux apps be run in Android? answers my question to some extent but eventually leaves me confused and without clarity.
I think this is enough for Android-NDK
The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.
The NDK provides:
A set of tools and build files used to generate native code libraries
from C and C++ sources
A way to embed the corresponding native libraries into an application
package file (.apk) that can be deployed on Android devices
A set of native system headers and libraries that will be supported
in all future versions of the Android platform, starting from Android
1.5. Applications that use native activities must be run on Android 2.3 or later.
This thing you can not find in other cross-compilation with arm toolchain..
As mentioned in the link http://developer.android.com/sdk/ndk/index.html NDK is a companion for App development folk to create performance sensitive native code. NDK exposes some of the native implementation of Android which could not be found in the general Linux environments. Some of them include the Android/Bitmap, Android/nativeWindow etc. Using these Android natives applcation can speed up CPU intensive processes like some compression or decompression of images.
Even though the externally cross-compiled executables may run in the Android there no guarantee that versions of the standard library implementaions are the same. NDK provides a easier and Android compatible toolchain and other resources, using which is much easier to application developers than having to find a compatible cross-compiler for their usecase.
What is the Android NDK (native development kit) ? How can one use it? Why should one use it?
The NDK (Native Development Kit) is a tool that allows you to program in C/C++ for Android devices. It's intended to integrate with the SDK (it's described as a "companion tool") and used only for performance-critical portions of a project. See here for more information.
NDK may improve application performance. This is usually true for many
processor-bound applications. Many multimedia applications and video games use
native code for processor-intensive tasks.
The performance improvements can come from three sources. Firstly, the native code is compiled to a binary code and run directly on OS, while Java code is translated into Java
byte-code and interpreted by Dalvik Virtual Machine (VM). At Android 2.2 or higher,
a Just-In-Time (JIT) compiler is added to Dalvik VM to analyze and optimize the Java
byte-code while the program is running (for example, JIT can compile a part of the
byte-code to binary code before its execution). But in many cases, native code still
runs faster than Java code.
Java code is run by Dalvik VM on Android. Dalvik VM is specially designed
for systems with constrained hardware resources (memory space, processor
speed, and so on).
The second source for performance improvements at NDK is that native code allows
developers to make use of some processor features that are not accessible at Android SDK,
such as NEON, a Single Instruction Multiple Data (SIMD) technology, allowing multiple
data elements to be processed in parallel. One particular coding task example is the color
conversion for a video frame or a photo. Suppose we are to convert a photo of 1920x1280
pixels from the RGB color space to the YCbCr color space. The naive approach is to apply a
conversion formula to every pixel (that is, over two million pixels). With NEON, we can process multiple pixels at one time to reduce the processing time.
The third aspect is that we can optimize the critical code at an assembly level, which is a
common practice in desktop software development.
Disadvantage
NDK cannot access lots of APIs available in the Android SDK directly, and developing in NDK will always introduce extra complexity
into your application.
The Android NDK is a companion tool used only in conjunction with Android SDK which allows application developers to build performance-critical portions of their apps by use of native (C/C++) code.
This provide benefits in form of reuse of existing code and increased speed.
Please go through below links.
Link-1
Link-2
Link-3
The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.
The following links also answers your question:
What is NDK?
When to Develop in Native Code
NDK Download
How to build NDK app
how to work with NDK
10 tips for Android NDK
The Android NDK is a toolset that lets you embed components that make
use of native code in your Android applications.
Android applications run in the Dalvik virtual machine. The NDK allows
you to implement parts of your applications using native-code
languages such as C and C++. This can provide benefits to certain
classes of applications, in the form of reuse of existing code and in
some cases increased speed.
Source: http://developer.android.com/sdk/ndk/overview.html
The Android NDK is a companion tool to the Android SDK that lets you
build performance-critical portions of your apps in native code. It
provides headers and libraries that allow you to build activities,
handle user input, use hardware sensors, access application resources,
and more, when programming in C or C++. If you write native code, your
applications are still packaged into an .apk file and they still run
inside of a virtual machine on the device. The fundamental Android
application model does not change.
Source: http://developer.android.com/sdk/ndk/index.html
NDK is just a set of tools which lets you to write C/C++ codes for your application.For example suppose you want to add a critical function/performance to your app and you want to write it in C/C++ then eclipse or any other IDE will not allow you to write your C/C++ and in that case you have to use NDK and integrate it in your app.
NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++....Checkout this https://developer.android.com/tools/sdk/ndk/index.html
Android NDK (native development kit)
Android Native Development Kit (NDK) is developers to write code in C/C++ that compiles to native code
Why should one use it?
The source code is compiled directly into machine code for the CPU (and not into an intermediate language, as with Java) then developers are able to get the best performance
How can one use it?
Here best tutorials
https://developer.android.com/ndk/index.html
https://www.androidauthority.com/android-ndk-everything-need-know-677642/
https://www.ntu.edu.sg/home/ehchua/programming/android/Android_NDK.html