Android.mk and Application.mk to build OpenCV 3.2 - android

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)

Its easy follow my all point you will do it ,
import your Opencv ( as u did already)
now goto your project Structure select app than Add new Dependencies and select your opencv Module click ok
Crete a class call it NativeClass.java it will contain your native function and variables
creat a method getMessage from JNI in your nativeClass.java and build that
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
when u write above code in terminal click enter it will create jni folder and it will have header file
Now copy headerfile and past it in same folder but write .cpp instead of .h
copy function jni function from header file and past that in your cpp file
build that if shows error goto gradle file and write
android.usedeprecatedndk=true
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)
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
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
now build that it will create jnilibs fodler and it will contains your libs files
now goto your mainjava file and load this
static {
System.loadLibrary("MyLibs");
}
to call your native code write NativeClass.getMessageFromJNI()
and show that in Text view

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

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.

Related

Using thread and mutex in Android Studio with ndk 10c

I have the latest version of the 64bit NDK (r10c), and the latest version of Android Studio I can download (0.8.14).
I am making a number of JNI calls to use String, Vector, Atomic, etc. But I can not figure out how to use thread and mutex.
Both of them give me the same error
Error:(92, 5) error: 'thread' is not a member of 'std'
Error:(93, 5) error: 'mutex' is not a member of 'std'
I'm sure that the NDK is using 4.9 of the gnu-libstdc++. If I put in a #error in the file I see my error and compilation stops. It appears that I'm not missing any defines since I can put the #error inside the class and see it.
Here is the ndk config in my build.gradle
ndk {
moduleName "myLib"
ldLibs "log"
stl "gnustl_shared"
cFlags "-std=c++11 -frtti -fexceptions -pthread"
}
The -frtti and -pthread seem to make no difference. I have also tried stl of gnustl_shared as well as gnustl_static, no difference.
By default, NDK still uses GCC 4.6 which has crippled support for C++11. You need the grade equivalent for setting NDK_TOOLCHAIN_VERSION:=4.9 in Application.mk. You can find some answers here: how to specify NDK_TOOLCHAIN_VERSION in gradle file for android ndk build, but unfortunately the bottom line is that today you have to disable automatic ndk-build call by setting jni.srcDirs to empty and use the Android.mk and Application.mk files the old way.
So, if in your jni directory, there are files file1.cpp and file2.cpp, you will need the following Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myLib
LOCAL_SRC_FILES := file1.cpp file2.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -std=c++11 -frtti -fexceptions -pthread
... and Application.mk
APP_ABI := armeabi-v7a
APP_STL := gnustl_shared
NDK_TOOLCHAIN_VERSION := 4.9

How to compile Cmake file with Android.mk?

I need to generate ARM structure shared library for a sampleCPP project.
Sample projects contains:
CMakeLists.txt
some.cpp (s)
Some.h (s)
some.tab.cpp.make (S)
some.tab.hpp.cmake (s)
Now, I want to create a shared library for a different Android project. I tried to compile with [Android-Cmake][1]but it is generating X86 Architecture library not ARM.
Please let me know if there is another way to do it. Also can i run X86 on Android Platform for all version?
Edit :
Here is my Android.mk:
LOCAL_PATH := $(call my-dir)/../ //Path is according JNI Folder
SRC_TOP_DIR := $(LOCAL_PATH)
include $(CLEAR_VARS)
LOCAL_MODULE := smileParse
LOCAL_CFLAGS := -DANDROID
LOCAL_SRC_FILES := main.cpp test.cpp smamain.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include $(LOCAL_PATH)/
Create a folder called JNI in your project:
Create or Edit Android.mk in JNI folder, set include and library path,
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := some.cpp
include $(BUILD_SHARED_LIBRARY)
Declare a java wrapper class, declare a native function:
public class JWrapperSomeClass {
public native void Demo(int para);
}
use javah command to generate the function signature for your C++ method wrapper:
javah -jni -classpath bin/classes/ -d jni com.example.Your.Package.Class
Edit the code in C++
Go to your project folder, run command:
$ANDROID_NDK/ndk-build
where $ANDROID_NDK is the folder where you installed android NDK
That is pretty much it. For more details, you might read NDK or JNI documentation.

Eclipse can't include opencv2 header files

I've created an Android Application Project in Eclipse Juno, and after I've added native support to this project (Android Tools -> Add Native Support). So Eclipse has added in the project automatically a jni directory, within which a .cpp file and the file Android.mk.
Then I've build the project, without errors.
In order to use opencv for android libraries (2.4.5), I've added the path to the 'include' directory of these libraries in the project (Properties -> C/C++ General -> Path And Symbols -> Include Diretories). I've also set in the Eclipse Preferences -> NDK the correct path to the ndk directory in my pc, to make possible to execute ndk-build when I build the projects.
But, if i try to include in the .cpp file:
#include <opencv2/opencv.hpp>
The console show me this result:
12:24:25 **** Build of configuration Default for project ProvaVideoCapture ****
"C:\\Users\\Micaela\\Desktop\\Android\\android-ndk-r8e\\ndk-build.cmd" all
"Compile++ thumb : jniVideoCapture <= jniVideoCapture.cpp
jni/jniVideoCapture.cpp:4:30: fatal error: opencv2/opencv.hpp: No such file or directory
compilation terminated.
make: *** [obj/local/armeabi-v7a/objs/jniVideoCapture/jniVideoCapture.o] Error 1
12:24:26 Build Finished (took 282ms)
My Android.mk file is:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jniVideoCapture
LOCAL_SRC_FILES := jniVideoCapture.cpp
include $(BUILD_SHARED_LIBRARY)
The Application.mk file that I've added manually (it has not been automatically added by Eclipse) is:
APP_ABI := all
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_PLATFORM := android-9
I don't know how to solve this problem, or what I've done wrong.
Could you help me?
Add path like below to your Android.mk file and check
include E:/OpenCV-2.3.1/share/OpenCV/OpenCV.mk
Sample Android.mk for reference
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
include E:/OpenCV-2.3.1/share/OpenCV/OpenCV.mk
include $(OPENCV_MK_PATH)
LOCAL_MODULE := firstcv
LOCAL_SRC_FILES := first.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)

NDK compiling multiple libraries

I am using native code in my android app. Firstly I was only using one library. So everything worked fine. But now I have to integrate one more library into it. I've no idea what should be the ideal structure of the jni folder of my project (as in where to place the entire code, etc.). I found a work around. I created two folders inside jni .i.e library1 and library2. Again created a jni folder inside both the folders and placed respective code in the folders.
I got it to compile. Both .so files are being generated, but I am unable to use it in my application. I cant load the library using System.loadLibrary("library1.so"); Also tried providing full path. But failed
Also I have no idea what to write inside the parent jni folder's Android.mk file.
Current structure:
project_folder -> jni -> library1 -> jni -> "source code" an Android.mk is written here
project_folder -> jni -> library2 -> jni -> "source code" an Android.mk is written here
UPDATE #1 :
Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
make: *** No rule to make target `jni/zap/jni/zap/zap/error.c', needed by `obj/local/armeabi/objs-debug/zap/jni/zap/zap/error.o'. Stop.
I am not using Application.mk.
This is my Android.mk:
TOP_PATH := $(call my-dir)
# Build library 1
include $(CLEAR_VARS)
LOCAL_PATH := $(TOP_PATH)/zap
LOCAL_MODULE := zap
LOCAL_C_INCLUDES := $(LOCAL_PATH)/zap
LOCAL_SRC_FILES := $(LOCAL_PATH)/zap/error.c \
$(LOCAL_PATH)/zap/hello-jni.c \
$(LOCAL_PATH)/zap/zap.c \
$(LOCAL_PATH)/zap/zapd.c \
$(LOCAL_PATH)/zap/zaplib.c
include $(BUILD_SHARED_LIBRARY)
The best structure I've found is to use the jni/ folder for ndk-build makefiles only, and keep the source outside in their own folders. This is easy to add to existing projects without restructuring your tree under jni.
However, you do have to be careful about how you handle the LOCAL_PATH variable and use of $(call my-dir). Here's a working example:
MyProject/
library1/
source1.cpp
library2/
source2.cpp
jni/
Android.mk
Application.mk
Android.mk:
# TOP_PATH refers to the project root dir (MyProject)
TOP_PATH := $(call my-dir)/..
# Build library 1
include $(CLEAR_VARS)
LOCAL_PATH := $(TOP_PATH)/library1
LOCAL_MODULE := library1
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := source1.cpp
include $(BUILD_SHARED_LIBRARY)
# Build library 2
include $(CLEAR_VARS)
LOCAL_PATH := $(TOP_PATH)/library2
LOCAL_MODULE := library2
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := source2.cpp
include $(BUILD_SHARED_LIBRARY)
You can optionally split out the sections in Android.mk to their own makefiles.
I discovered that when compiling from the command line, I can include multiple libraries by running android update project twice, once with each library:
android update project -l ../SDK/library1/ --path . --name $name --target 23 --subprojects
android update project -l ../SDK/library2/ --path . --name $name --target 23 --subprojects
ant release

How to compile a static library using the Android NDK?

I'm trying to compile a static library to use on Android but I can't figure out how to compile it. The library uses standard libraries (stdio.h etc...) and libxml2.
I am trying to compile using arm-eabi-gcc but I get the following error:
/cygdrive/c/android-ndk-r4/build/platforms/android-8/arch-x86/usr/include/asm/posix_types.h:15:28: error: posix_types_64.h: No such file or directory
How do I get this to work?
As I understand it, the correct method is to use ndk-build and not invoking the compiler directly.
In Android.mk you need to specify a module for each static library you want to compile, and then specify that your shared library should use it.
Example of a modified Android.mk file of the hello-jni sample project:
LOCAL_PATH := $(call my-dir)
# Define vars for library that will be build statically.
include $(CLEAR_VARS)
LOCAL_MODULE := <module_name>
LOCAL_C_INCLUDES := <header_files_path>
LOCAL_SRC_FILES := <list_of_src_files>
# Optional compiler flags.
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g
include $(BUILD_STATIC_LIBRARY)
# First lib, which will be built statically.
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_STATIC_LIBRARIES := <module_name>
LOCAL_C_INCLUDES := <header_files_path>
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
If you want control over which modules to compile when you run ndk-build you can create create a Application.mk file (in the same directory as Android.mk) and list all the modules as in the following example:
APP_MODULES := <module_name_1> <module_name_2> ... <module_name_n>
In response to
Can you generate a static library (.a file) without a shared library
that uses it?
(which really should have been its own question), the answer is yes.
By default, the NDK will only build executables and shared libraries (with their dependencies of course). You can, however, force the NDK to build a standalone static library by explicitly referencing it in your Application.mk.
Assuming your static library module is LOCAL_MODULE := libXYZ, add the following line to Application.mk (creating the file in the same folder as your Android.mk if it doesn't exist):
APP_MODULES := XYZ
Note the the APP_MODULES value does not include the lib prefix included in your static library module name.
Alternatively, if you don't want to create an Application.mk, you can specify the value on the command line: ndk-build APP_MODULES=XYZ
A cool trick: if you have an Android.mk file, you can change the build type from:
include $(BUILD_SHARED_LIBRARY)
to
include $(BUILD_STATIC_LIBRARY)
and .a libraries will be output to the obj/ folder into their respective architectures when you ndk-build the library.

Categories

Resources