Ask Your Question
1

How can I link with native OpenCV in Android studio

asked 2016-11-21 06:24:06 -0600

Humam Helfawi gravatar image

updated 2016-11-21 06:24:44 -0600

In Android Studio, how Can I link my android project (NDK) with OpenCV without linking any java modules. In other words, I want to use opencv from C++ code without exposing it to the java interface. How can I do this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2016-11-21 07:47:37 -0600

Vintez gravatar image

updated 2016-11-21 07:49:05 -0600

You need to Build OpenCV for Android as Native Library.

So try these steps for example: (little Suggestion, download the Master-Branch from opencv from Github)

Set environment variables for your OpenCV Directory and add the Android NDK to your Path Variable: For Mac e.g.:

OPENCV_DIR=/home/declan/Documents/zone/mid/bin/android/opencv/opencv_github # where you want to put openCV on your system
export ANDROID_NDK=/home/declan/Documents/zone/mid/bin/android/google/sdk/ndk-bundle where is Google's NDK on your system

go to the Platforms directory cd ${OPENCV_DIR}/platforms configure the build with following parameters:

scripts/cmake_android_arm.sh -DBUILD_SHARED_LIBS=ON -DANDROID_NATIVE_API_LEVEL=9 -DANDROID_STL=gnustl_shared

and start building:

cd build_android_arm
make -j8

When make finished continue with sudo make install When everything worked fine, your files should be here:

${OPENCV_DIR}/platforms/build_android_arm/lib/armeabi-v7a

NOTE THATS HOW IT WORKS IN MAC! WHEN YOU NEED IT FOR VISUAL STUDIO BUILD IT WITH CMAKE (should be similar)

So after that you have your OpenCV Files built ready for Android. Now you need to add the files to your Android Project. For that simply create a jni folder in <yourProject>/app/src/main/ in this folder you create 2 files: Android.mk and Application.mk These are build files. in them you can write the following:

Android.mk:

LOCAL_PATH := $(call my-dir)

 include $(CLEAR_VARS)

#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=off
OPENCV_LIB_TYPE:=STATIC
ifdef OPENCV_ANDROID_SDK
  ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
    include ${OPENCV_ANDROID_SDK}/OpenCV.mk
  else
    include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
  endif
else
  include /<hardcodedPathToYourOpenCV/opencv-master/platforms/build_android_arm/install/sdk/native/jni/OpenCV.mk
endif

LOCAL_SRC_FILES := <additional Files you created in c++>
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl

LOCAL_MODULE := <Library Name>

include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++11
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
APP_MODULES := <LibraryName>

So far so good, but not finished yet! You have to add some parts into your build.gradlefile.

android{
[...]
    ndk{
        abiFilters 'armeabi-v7a'
    }
}
externalNativeBuild{
    ndkBuild{
        path "src/main/jni/Android.mk/"
    }
}

And this is it. After that, Android Studio should compile your OpenCV Library into your Android Studio Project with Native C++.

edit flag offensive delete link more

Comments

To what should I set <library name=""> ? Is it arbitrary but the same on on locations it occurs ?

chnbr gravatar imagechnbr ( 2017-02-10 12:47:39 -0600 )edit

I got the armeabi-v7a directory. This seems to be for ARM-based devices. How do I compile such that I can run on the simulator ? I think I need to set APP_ABI, but where and to what ?

Thanks Chris

chnbr gravatar imagechnbr ( 2017-02-11 06:08:02 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-11-21 06:24:06 -0600

Seen: 4,519 times

Last updated: Nov 21 '16