Issue
I am trying to setup OpenCV in Android Studio, using NDK. The steps I have followed are:
- Create a library project for OpenCV and import it in MyProject.
- Add all OpenCV library files to the /jni folder.
- Create Android.mk file
However, when I run the Android.mk with ndk-build I get this error:
Nothing to be done for `.../MyProject/jni/Android.mk'.
This is the content of Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include /.../MyProject/jni/OpenCV.mk
I am stuck at building the libraries, any help?
SOLUTION
I managed to use NDK by linking gradle to an external installation of it, and by also using javah to created implementation headers for native methods. Follow these steps to setup Gradle to use NDK:
install ndk (it's not built into Android Studio, so it needs to be installed manually)
setup ndk.dir in local.properties and ndkDir in gradle.properties to link to installation path
create "jni" folder in src/main
add Application.mk file to jni
APP_ABI := armeabi APP_PLATFORM := android-9
add Android.mk file to jni (change LOCAL_SRC_FILES and LOCAL_MODULE values)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c LOCAL_LDLIBS += -llog LOCAL_MODULE := hello
include $(BUILD_SHARED_LIBRARY)
add this code to gradle
sourceSets.main.jni.srcDirs = [] // take the built .so files and place them in jniLibs //noinspection GroovyAssignabilityCheck task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') { commandLine "$ndkDir/ndk-build", '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 }
setup javah as External tool (point to your installation directory) Program: /usr/bin/javah Params: -v -jni -d $ModuleFileDir$/src/main/jni $FileClass$ Working dir: $SourcepathEntry$
run javah on .java file containing native method -> header is created
create implementation file
specifiy module in gradle:
defaultConfig { minSdkVersion 9 targetSdkVersion 21 versionCode 4 versionName '1.11' ndk { moduleName "hello" } }
done!
Solution
You cannot yet use Android Studio for NDK development, but you can add pre-built *.so files of Open CV in your project in the new version of Gradle 0.7.2+
Follow the steps here: How to use opencv in android studio using gradle build tool?
Answered By - Ajay S
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.