3
votes

I am trying to setup OpenCV in Android Studio, using NDK. The steps I have followed are:

  1. Create a library project for OpenCV and import it in MyProject.
  2. Add all OpenCV library files to the /jni folder.
  3. 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!

2

2 Answers

2
votes

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?

4
votes

I have posted a new post about how to build an Android NDK application with OpenCV included using Android Studio and Gradle. More information can be seen here, I have summarized two methods:

(1) run ndk-build within Gradle task

sourceSets.main.jni.srcDirs = []

task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
    ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
    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
}

(2) run ndk-build with an external tool

Parameters: NDK_PROJECT_PATH=$ModuleFileDir$/build/intermediates/ndk NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs NDK_APPLICATION_MK=$ModuleFileDir$/src/main/jni/Application.mk APP_BUILD_SCRIPT=$ModuleFileDir$/src/main/jni/Android.mk V=1

More information can be seen here