Linking C++ OpenCV to an app in Android Studio - android

I am integrating existing C++ code that uses OpenCV to an Android app, using Android studio. For this purpose, I have installed the package OpenCV-android-sdk and added it as a module to Android studio. I have also created a simple Kotlin app.
So far I have managed to integrate my C++ code into the project. After adding the paths to the OpenCV includes by means of an include_directories statement, the code compiles successfully.
My next step would be to link against the precompiled OpenCV library, to resolve the "undefined symbol" errors. I have no idea how to achieve this/where to specify it. I have tried to find resources on the web, but not two resources tell the same and the solutions seem overly complicated. I am lost in the jungle.

I finally managed. Here comes the recipe (validated under Windows).
Make sure to download the OpenCV Android SDK and copy it somewhere.
The project must be created as a Native C++ app (Phone and tablet). A cpp folder is automatically created with a native-lib.cpp source file and a CMakeLists.txt.
Add the following lines to the CMakeLists file, after the project line:
set(OpenCV_DIR $ENV{OPENCV_ANDROID}/sdk/native/jni)
find_package(OpenCV REQUIRED)
You must define the environment variable OPENCV_ANDROID and make it point to the folder .../OpenCV-android-sdk that you copied. (Alternatively, you can hard-code the path in the set command.)
Then insert at the bottom
target_link_libraries(app_name ${OpenCV_LIBS})
where OpenCV_LIBS points to the folder .../OpenCV-android-sdk/sdk/native/libs, and app_name is the name you gave when creating your app.
Finally, cross fingers and build.

Related

porting a C++ project to android using NDK-CMAKE

I am following this guideline to compile a c++ project.
It compiles successfully (it shows *[100%] Built target MY_SERVICE).
I can also observe that, it generates *.so library files.
After defining the output directory i can see a file with MY_SERVICE name without any extension.
MY_SERVICE is defined in CMAKELists.txt as below
set(BIN_TARGET MY_SERVICE)
My question is that what is the next step and how i can run it on an android platform.

How to set the local cpp source path in Android studio for prebuild library

I created an App which includes our source code as a native lib (so-file). I'm able to step into it and everything works fine so far with this code.
This native lib links against another native lib which was pre-build on a different machine and which I copied into the jni-abi-folder. I have checked out the svn-repository of this so-file in a different folder parallel to my project and need to be able to debug also into it.
When I now do a break, I can see the method names in the callstack so I assume that the symbols can be loaded, but Android Studio doesn't know where to find the source files.
Under Visual Studio, when I did a break, I could specify the symbols in the symbol path and then an error was displaced that I should navigate to the corresponding source file. Then I only needed to navigate to the folder and it was working.
How can this be done with Android Studio - I have the newest version 3.2.1 installed.
I finally found the solution.
I got a stripped version from my colleague, he basically gave me the version inside his apk which is stripped. Now he gave me directly the build version (I think it is located somewhere in the intermediate directory or so, just search for the name of the lib).
Under Run\Edit Configurations\Debugger\LLDB startup commands the original source directory can be mapped to the source directory on my computer which is different. To achieve this, enter "settings set target.source-map or-dir cur-dir"
There should be a way to find out the original source directory from the so file, but I don't know it right now.
I hope this can help somebody else

JNI folder is not picked up by Android studio after headers are generated

I am working on building an Android Library which is part of a larger Android app project. I am using Android Studio.
I created the Android library as a module in the Android app project. I created my native methods, and then I put the .class through javah -jni and I was able to see the jni folder created and the header file generated.
However, Android Studio is not picking up the jni folder and showing it in the project view.
Anybody have any idea, what am I missing?
After hours of looking through docs, and posts, and tutorials, I remembered to check the simplest of things. Make sure your jni folder is in src/main

Facebook Conceal .so files not uploading in Android Project

I cannot get the Conceal Library to work, it may be something I am doing wrong, I wish the instructions were a bit more detailed.
http://facebook.github.io/conceal/documentation/
I have added the crypto.jar to my project, and then the instructions tell me to add the files armeabi/*.so to my jni folder.
"Add a dependency to crypto.jar from java code using your favorite dependency management system, and drop the .so files in libs.zip into your jni/ folder."
I don't have a jni folder, since my project is a normal Android progect, not an NDK project. How can I get this to work, I tried creating the folder and adding it to the build path, but it did not work. I am completely lost on this. In my source code I am loading the libraries like this:
static {
// Load Facebook Conceal Crypto Files
System.loadLibrary("libconceal.so");
System.loadLibrary("libcryptox.so");
}
then the instructions tell me to add the files armeabi/*.so to my jni folder.
That's a bug in their documentation. Pre-compiled .so files will go in libs/. So, you should wind up with libs/armeabi/*.so.
I tried creating the folder and adding it to the build path
That is almost never the right answer and can seriously screw things up, so I recommend that you reverse that step.

I got the error on sample project "Hello Word" on android - cocos2d

I have eclipse 3.7, cocos2d-1.0.1-x-0.9.2, android-ndk-r7.
When I have create sample project but, it will get error "FORCE CLOSE"...
So is there any other thing i want to include on the project ?
Your main application has a load static library method that is pointing to a nonexistent library, which means it has not found it. Recheck your makefiles for the correct module name. Also make sure they have compiled correctly with NDK beforehand and the .a and .so files are in the lib folder.

Categories

Resources