Saturday, July 23, 2016

How to Compile Mixed Processing OpenCV Tutorial App on Android Studio

The second OpenCV for Android tutorial app, namely Mixed Processing, differs from Camera Preview app in that it also has a C++ source file, thus requiring Android NDK. To compile this project in Android Studio, the first several steps are identical to Camera Preview app, so follow instructions in my previous post. In summary, these steps that are required to integrate OpenCV for Android SDK statically are:

1. Download OpenCV for Android and unzip
2. Import the tutorial project to Android Studio
3. Adjust the Android API and SDK Tools version in the app and library module build.gradle files
4. Copy OpenCV Android native library folder into the app module folder and rename it as jniLibs
5. Add OpenCV static initializing code to the main activity java file
6. Add appcompat dependency in the app module build.gradle file (for API 23 or above only)
7. Add camera permission code in the java file (for API 23 or above only)
8. Change the theme in AndroidManifest.xml file (for API 23 or above only)

Even after all the above changes, the project will still not compile because it contains native C++ file, which we must let Android Studio know what to do with. So, here we go again! I will assume the project directory of ~/AndroidStudioProjects/tutorial-2-mixedprocessing. Before we do anything further, make sure to install Android NDK from SDK Manager.

When you try to compile the project, it will complain with the following error message:
Error:(12, 0) NDK integration is deprecated in the current plugin.
Consider trying the new experimental plugin
Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration

Let's do what it asks for. You probably do not have this gradle.properties file yet, so let's create it.
$ echo "android.useDeprecatedNdk=true" > ~/AndroidStudioProjects/tutorial-2-mixedprocessing/gradle.properties

When you try to build the project, it will now complain with two errors:
Error:(2, 33) opencv2/core/core.hpp: No such file or directory
...
Error:Execution failed for task ':openCVTutorial2MixedProcessing:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '~/Library/Android/Sdk/ndk-bundle/ndk-build'' finished with non-zero exit value 2

Let's tackle these errors one by one. Open up the app module's build.gradle file, and edit it similar to below:
import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "org.opencv.samples.tutorial2"
        minSdkVersion 8
        targetSdkVersion 23

        ndk {
            moduleName "mixed_sample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
        jni.srcDirs = []
    }

    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine '~/Library/Android/Sdk/ndk-bundle/ndk-build', '-C', file('src/main').absolutePath // replace with your path
        }
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}

dependencies {
    compile project(':openCVLibrary310')
    compile 'com.android.support:appcompat-v7:23.0.0'
}

Note that you must replace ~/Library/Android/Sdk/ndk-bundle/ndk-build with the NDK path in your system. You can check this in local.settings file, which will have a line similar to
ndk.dir=~/Library/Android/Sdk/ndk-bundle
You can simply use this path followed by /ndk-build, as shown above.

Now try to build the project again, and you will see the first error message is gone. To solve the last error, edit the app module's jni/Android.mk file as shown below:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
include ~/Downloads/opencv3.1/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk // Replace this with  your own path where you have extracted OpenCV for Android

LOCAL_MODULE    := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

Again, make sure to replace ~/Downloads/opencv3.1/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk with the appropriate path.

Finally, if you plan to run the app on x86 emulator or devices, you must also edit app module's jni/Application.mk file and replace APP_ABI := armeabi-v7a with APP_ABI := all.

 At last, you should be able to compile and run this Mixed Processing tutorial app on your device or emulator!

By the way, if you think you are missing the menu (Such as Samsung Galaxy phones), press-and-hold the back button; it will show the menu items in the full-screen mode.

No comments:

Post a Comment