Android.mk and Application.mk for building OpenCV 3.2

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

How can I create Application.mk and Android.mk to compile this sdk? (Since I know that ndk-build needs these two files)

+6
source share
3 answers

If you want to use the Java API for OpenCV, you can check out this post .

However, if you want to use pre-created static OpenCV libraries with C/C++ on the NDK side, then you only need Android.mk and Application.mk . In my version of these .mk files .mk I dynamically load the necessary pre-built libraries from a location on my disk. Therefore, when building .mk files load the required static libraries.

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 
0
source

It easily follows everything you do

  • import your Opencv (as you already did)
  • now go to your project. Select the Structure application than Add New Dependencies and select your opencv. Click OK.
  • Crete class calls it NativeClass.java, it will contain your own function and variables
  • create getMessage method from JNI in your nativeClass.java and create it enter image description here
  • Now open a terminal and run 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

  • when u writes the above code in the terminal, press Enter, it will create a jni folder and it will have a header file
  • Now copy the header file and skip it in the same folder, but write .cpp instead of .h
  • Copy function of jni function from header file and minus what in your cpp file enter image description here
  • build if shows goto gradle file and write error android.usedeprecatedndk = true
  • Now create android.mk file in 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) 
  • now create an Application.mk file and write this code

     APP_STL := gnustl_static APP_CPPFLAGS := -frtti -fexceptions APP_ABI := armeabi-v7a APP_PLATFORM := android-16 
  • now go to build.gradel file and otherwise 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 file and ndk, you must specify this folder location in this line C: / Users / do / AppData / Local / Android / sdk / ndk-bundle

  • now let's create it to create jnilibs fodler and it will contain your libs files

  • Now go to mainjava file and download this

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

  • to call your own code write NativeClass.getMessageFromJNI () and show that in a text view enter image description here
0
source

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

In this case, you can check my answer , which offers two solutions for integrating the OpenCV 3.2.0 SDK into the Android project / application. The implementation provides correct and tested integration and uses the CMakeLists.txt scripts found in the OpenCV SDK to correctly assemble and link library modules (including third-party and SDK libraries), as well as header files for C / C ++ code, which are part of the project.

0
source

Source: https://habr.com/ru/post/1013924/


All Articles