6

I've downloaded OpenCV for Android Version 3.2 and imported its java module to my project. I've copied the native (c++) codes into modules directory in openCVLibrary320/app/src/main/jni.

How can I create Application.mk and Android.mk for compiling this sdk? (As i know that ndk-build need these two files)

2

3 Answers 3

0

Its easy follow my all point you will do it ,

  1. import your Opencv ( as u did already)
  2. now goto your project Structure select app than Add new Dependencies and select your opencv Module click ok
  3. Crete a class call it NativeClass.java it will contain your native function and variables
  4. creat a method getMessage from JNI in your nativeClass.java and build that enter image description here
  5. Now Open Terminal and wirte this

    1)  cd app/src/main
    
    2)  javah -d jni -classpath ../../build/intermediates/classes/debug write_your_Pakage_Name.className
     My pakage name is com.example.cvlab.ndktest
     class name -->nativeClass
    

    enter image description here

  6. when u write above code in terminal click enter it will create jni folder and it will have header file
  7. Now copy headerfile and past it in same folder but write .cpp instead of .h
  8. copy function jni function from header file and past that in your cpp file enter image description here
  9. build that if shows error goto gradle file and write android.usedeprecatedndk=true
  10. Now create android.mk file in your jni folder and add this code

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    #opencv
    OPENCVROOT:= your opencv location
    OPENCV_CAMERA_MODULES:=on
    OPENCV_INSTALL_MODULES:=on
    OPENCV_LIB_TYPE:=SHARED
    include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
    
    LOCAL_SRC_FILES := your cpp file name
    
    LOCAL_LDLIBS += -llog
    LOCAL_MODULE := MyLibs
    
    include $(BUILD_SHARED_LIBRARY)
    
  11. now create Application.mk file and write this code

    APP_STL := gnustl_static
    APP_CPPFLAGS := -frtti -fexceptions
    APP_ABI := armeabi-v7a
    APP_PLATFORM := android-16
    
  12. now go to your build.gradel file and afther defultConfiguation put this

        sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
    }
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine "C:/Users/do/AppData/Local/Android/sdk/ndk-bundle /ndk-build.cmd",
                'NDK_PROJECT_PATH=build/intermediates/ndk',
                'NDK_LIBS_OUT=src/main/jniLibs',
                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                'NDK_APPLICATION_MK=src/main/jni/Application.mk'
    }
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
    

    Note: if you installed your android stdk and ndk than you have to give that folder location in commond line like this C:/Users/do/AppData/Local/Android/sdk/ndk-bundle

  13. now build that it will create jnilibs fodler and it will contains your libs files

  14. now goto your mainjava file and load this

static { System.loadLibrary("MyLibs"); }

  1. to call your native code write NativeClass.getMessageFromJNI() and show that in Text view enter image description here
0

If you want to use Java API for OpenCV, then you may have a look at this post.

However, if you want to use pre-built OpenCV static libs with C/C++ code on NDK side then only you will require Android.mk and Application.mk. In my version of these .mk files, I dynamically load the required pre-built libraries from a location on my drive, So while building, .mk files will load the required static libs.

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Location of SDK on my drive
OPENCVROOT := ${HOME}/opencv-sdk-android
OPENCV_CAMERA_MODULES := off
OPENCV_INSTALL_MODULES := on
OPENCV_LIB_TYPE := STATIC
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

# Load your local .cpp and .h files here.
LOCAL_SRC_FILES := hello-jni.c

LOCAL_LDLIBS := -lm -llog -ldl -lz
LOCAL_CPPFLAGS += -fexceptions -frtti -std=c++11
LOCAL_LDFLAGS += -ljnigraphics

include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_STL := gnustl_static
APP_CPPFLAGS += -fexceptions -frtti -std=c++11 -D__STDC_CONSTANT_MACROS

APP_ABI := all
APP_PLATFORM=android-14
2
  • Where do you specify which c++ files need to compiled?
    – MinhNguyen
    Commented May 18, 2017 at 16:39
  • Updated @MinhNguyen
    – ZdaR
    Commented May 20, 2017 at 7:14
0

Although you've requested Android.mk for ndk-build, I would like to suggest CMake which Android Developers recommends for use with native projects.

In that case, you could check out my answer which provides two solutions to integrate the OpenCV 3.2.0 SDK into the Android Project/Application. The implementation provides proper and tested integration, and it leverages the CMakeLists.txt scripts found within the OpenCV SDK to build and link the library modules (included 3rd Party and SDK libs) properly, and it also includes the header files to code in native C/C++ classes which are part of the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.