Using opencv in native code for Android app development - android

I have namespace error building with ndk-build for native code in my Android app. The error sample is
C:/adt-bundle-windows-x86/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/bits
/allocator.h:54:1: error: unknown type name 'namespace'
C:/adt-bundle-windows-x86/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/bits
/allocator.h:55:1: error: expected ',' or ';' before '{' token
For OpenCV settings, my Application.mk file is
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi
APP_PLATFORM := android-10
That means I am using gnu-libstdc++ in compiling the native code.
My jni.c has c extension. That is I receive from my third party and they prefer in .c extension as if they have to change to .cpp extension, they have to change a lot in their other libraries.
So far is OK, I did all project settings for OpenCV for native development and if I do ndk-build, I can make .so libraries.
The problem of those namespace error happened when I include OpenCV's header file #include <opencv2/contrib/detection_based_tracker.hpp> into jni.c and I got a lot of name space error. If I include that #include <opencv2/contrib/detection_based_tracker.hpp> into cpp file, no error.
My questions are
(1)Is the error is because of using .hpp file in .c file?
(2)If I have no choice and have to use that .c file, is there way around to remove that error?
Thanks

My assumption would be that the file is compiled as a "C" file instead of a "C++" file because of the extension ".c". That means you cannot use any "C++" Code in your jni.c, wike classes or namespaces. These are obviously used however in your file "detection_based_tracker.hpp" that you are using.
So the problem is not that you include a file named ".hpp", but that this file contains "C++" code wich the "C" compiler cannot handle.
One solution to this problem is to only use the "C" functions in opencv (for example "opencv2/imgproc/imgproc_c.h" instead of "opencv2/imgproc/imgproc.hpp"). However, your function "detection_based_tracker.hpp" might not have a "C" version, as far as I can see.
The other option is to add a second file "function.cpp" with the "C++" functions that use opencv. The functions from "function.cpp" can be declared in a file "functions.h" and included in your "jni.c", so you can still use opencv c++ functions. Be careful to only use C style functions then, though (no classes, namespaces, ...) in your "function.h" file, otherwise you will have the same problems as before.

Related

How to build lib protobuf along with dynamic library for use in Android application?

I'm trying to compile an open source project (let's say foo) into a libfoo.so native library to use for my android app. However, this project also uses google's protocol buffer library.
So i was able to compile Google's lib protobuf for iOS as follows: https://gist.github.com/BennettSmith/7150245 .
However, this just generates libprotobuf.a file. The problem is that I can't seem to figure out a way to include this libprotobuf.a file to my libfoo.so file for use with my android application. I THOUGHT i had referenced it when i did:
include $(CLEAR_VARS)
LOCAL_MODULE := libprotobuf
LOCAL_SRC_FILES := ./src/lib/libprotobuf.a
LOCAL_EXPORT_C_INCLUDES := ./src/include
include $(PREBUILT_STATIC_LIBRARY)
in my Android.mk file. However, it seems that it is definitely not getting linked properly. Whenever i try to ndk-build the open source project (which depends on using lib protobuf), i get a bunch of undefined references, like:
undefined reference to 'google::protobuf::internal::empty_string_'
so then i thought that maybe i'm not supposed to have the symbols defined here? like maybe i'm supposed to first compile the libfoo.so file, and then somehow link lib protobuf as libprotobuf.a or libprotobuf.so ?? so i ended up using LOCAL_ALLOW_UNDEFINED_SYMBOLS := true to just get my project to generate the libfoo.so file.
but this libfoo.so gives me some load error as soon as i try to use it within my android app:
dlopen failed: cannot locate symbol "_ZN6google8protobuf11MessageLite15ParseFromStringERKSs" referenced by libfoo.so
So I'm wondering:
How do i include this protobuf library in my android app?
Do i try to include this protobuf library into libfoo.so first?
Would it even work if i tried to ignore linker warnings and then tried to generate libprotobuf.so and include that with my application?
is it even possible to generate libprotobuf.so? I can't generate it using the instructions on their github - i get a ton of errors
Note that the only reason i'm even able to get undefined reference errors when trying to generate libfoo.so is because i manually copied all of the *.h in google's lib protobuf.
Any help would be greatly appreciated!!!

Wrapp existing library (.so) in jni for android app

I wanna realize this idea. I spent several days searching for information, but could not find anything. All tutorials say how to write my own library with JNI, but how to wrap already existing? I need just simple tutorial step by step (and why? if it possible). So I wanna start create native android application.
What I have :
I create C++ library in QTCreator by tutorial from youtube: simple library on C++ (.so) with headers (.h) which do simple cout in console:
Not compiled code mylib.cpp:
#include "mylib.h"
MyLib::MyLib() { }
void MyLib::Test() {
qDebug() << "Hello from our DLL";
// .so
}
Header mylib.h:
#ifndef MYLIB_H
#define MYLIB_H
#include "mylib_global.h"
#include <QDebug>
class MYLIBSHARED_EXPORT MyLib
{
public:
MyLib();
void Test();
};
#endif // MYLIB_H
and mylib_global.h (I think it does't matter)
So after build I have myLib.so.
And now I need wrap it in my android app. So I don't understand what I need to do for it.
I'm develop in Android Studio. And what I know:
Create in java package LibWrappClass with native method - something like "simplePrint()":
public native void simplePrint();
I need to create in /src/main folder "jni". Create Android.mk, myLibWrapper.h and myLibWrapper.cpp. But I don't understand what I need to write in Android.mk for connect my myLib.so to "myLibWrapper.h", and where should I put my library with headers. Can anyone help?
After adding the native method in your java code, You simply build the project. Now you need to move to the location where the class files are written by your IDE. Since you use Android Studio, it must be somewhere your project folder with path
out/production/YourModuleName
Open the location in commandline and run the javah command to generate the header file for your native function
javah -d <your jni folder path> <com.YourPackage.YourClass>
The class YourClass should be where you have declared the native method. This command will create a header file with name something like com_YourPackage_YourClass.h with a function declaration looks like
JNIEXPORT void JNICALL Java_com_YourPackage_YourClass_simplePrint
(JNIEnv *, jobject);
Implement this function in a C/C++ file with whatever operations you have to perform on jni side.
Then, Define the Android.mk file, In this case it will be something like
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := lib
LOCAL_SRC_FILES := lib.c
include $(BUILD_SHARED_LIBRARY)
If you have multiple source files add them in LOCAL_SRC_FILES separated by space.
Next, go to your jni folder and build the project using command ndk-build. This will place the .so file inside the android folder libs/armeabi
Finally rebuild the Android project from Android studio and run.
For detailed instructions and complete source checkout this gist

Use tinyalsa from ndk

I'm writing an app that should use functions from tinyalsa through ndk, I just want to call functions defined by tinyalsa like pcm_open() from my native functions implementations.
I have tried following the documentation about using prebuilt libraries in Android/ndk/docs/PREBUILTS.html but I can't get it working.
Could you please tell how could I do it?
Thanks
I could do it, the process is the next:
Copy tinyalsa.so to ~/Android/ndk/platforms/android-18/arch-arm/usr/lib
Copy asoundlib.h to ~/Android/ndk/platforms/android-18/arch-arm/usr/include
Platform-18 is the one I'm using. It can be specified in Application.mk with the line
APP_PLATFORM := android-18
After adding it to the ndk platform include it in the file where the native functions are implemented
#include <asoundlib.h>
Tell the compiler that we are going to need this library. In Android.mk
LOCAL_LDLIBS := -ltinyalsa
This worked for me :)

OpenCV Android NDK Project will not build

I am using OpenCV 2.4.4 with Eclipse Juno on Ubuntu.
My problem is similar to this one OpenCV for android sample programs showing error
But is now weirding me out. I spent most of my weekend trying to get the ndk and opencv library to play nice and still I cannot get it to work. I have in my test C++ cpp file with the following includes -
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cstdlib>
using namespace cv; //this is a problem
...
...
And it tells me when I try to build that "namespace cv cannot be found".
Oh well, I must have written some path wrong or am not using the correct library, right?
Wrong.
In the terminal, ndk-build builds the .so files without any errors. I can see them plain as day in the project folder.
But if I try to do build in eclipse, i.e. build the apk for tesitng on a virtual device or real device, then I get the namespace cv error and the build fails and then in the cpp file cv is underlined red and in eclipse the file is marked red and I can't even attempt to build until this file is "corrected".
Clearly I'm doing something wrong. But if I close my eclipse project and then reopen it, the cpp file is no longer marked red and I can make an apk build. If I attempt to open the cpp file the red returns and no more builds can happen until I go through the project close/open rigmarole.
I honestly don't know what the cause of the problem is. Any help would be greatly appreciated.
My android.mk file:
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=off
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=STATIC
include /the/correct/path/tp/opencv/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := mylib.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := all #i have tried various targets
APP_PLATFORM := android-8
APP_MODULES := mylib
I didn't think it would be this difficult. I should add that I have successfully used opencv with Android in anothe rproject I built on WIndows with Eclipse 3.5 (I think) and OpenCV 2.4.2
EDIT
I also want to say that I just did a quick 'n dirty test opencv operation. I wrote a quick Sobel function and called it as a native function in a Java file and "sobelled" a bitmap successfully. Of course, I still had to close/open my project to do this.
Hi I had same problem with you and after I added below "opencv android sdk" path that include problem disappeared.
exp : C:\project\OpenCV-2.4.6-android-sdk\sdk\native\jni\include
switch the path with your opencv-android-sdk path.
I know that's maybe too late, but in my case (Windows+Eclipse) I correct this problem just changing the backslash "\" to slash "/" in the Path and Symbols include of opencv jni libs.
Example:
Wrong H:\opencv\sdk\native\jni\include
Correct H:/opencv/sdk/native/jni/include

Android NDK doesn't support header files?

This page has instuctions for compiling iwlist and other commands for Android:
https://code.google.com/p/haggle/wiki/WirelessTools
Unfortunately I'm getting the same type of error as the commenter on that page:
Android NDK: WARNING: Unsupported source file extensions in /home/simon/Android-SDK/wireless_tools.29/Android.mk for module iwlist Android NDK: iwlib.h
When I remove the iwlib.h from the LOCAL_SRC_FILES of the Android.mk, it doesn't show that warning, but it still fails with the same error:
./iwlist.c:633:7: error: 'IW_EV_LCP_PK2_LEN' undeclared (first use in this function)
This is undeclared because it is in the iwlib.h file it's warning about.
So why does the Android NDK not recognize header .h files?
To build the libraries, you unpacked the gz file, and ran make, didn't you? Or you simply renamed wireless.22.h? Anyways, before you runndk-build, you have a wireless.h file in the package directory. Add the following two lines to the end of this file:
#undef IW_EV_LCP_PK_LEN
#undef IW_EV_POINT_PK_LEN
And remove the iwlib.h from the LOCAL_SRC_FILES of the Android.mk. #Gabe is right, header files should not be compiled separately. In iwlist.c you will find line 14:
#include "iwlib.h"
You normally don't compile header files. You include them in a .c or .cpp file that you do compile. But you wouldn't pass them into the compiler as a source file.

Categories

Resources