ブログを色々参考にしながら進めたが結構苦戦したのでまとめておく。
環境
- macOS Sierra 10.12.1
- Android Studio 2.2.2
- ASUS Zenfone2 laser; Android version 5.0.2; API Level 21 (Lollipop)
- OpenCV for Android version 3.1
手順
0. Android StudioにOpenCV for Android を導入する
OpenCV for AndroidをAndroid Studioに導入するメモ - Qiita をなぞる
うまく動いた。
1. NDK使わないバージョンのOpenCV (15-puzzle) を試す
AndroidStudio2.0でOpenCV3.1(sample編) - プログラミング好きな脳の引き出しをなぞる
これはうまく動いた。
注意点
2. NDK使うバージョンのOpenCV (face-detection) を試す
AndroidStudio2.0でOpenCV3.1(sample with NDK編) - プログラミング好きな脳の引き出し をなぞる
一部漏れがあったので差分だけ補う。エラーが出たら以下の差分を試して解消を試みる。
差分1. NDKをインストールする
Android Studioを開いて、File>Project Structureで開いたWindowにDownload NDKみたいなのがある。
差分2. gradle.propetiesに以下を追加。
android.useDeprecatedNdk=true
差分3. Application.mkを編集
APP_STL := gnustl_static APP_CPPFLAGS := -frtti -fexceptions APP_ABI := armeabi-v7a arm64-v8a APP_PLATFORM := android-8
arm64-v8aを追加した
参考:http://sarl-tokyo.com/wordpress/?p=553
差分4. ライブラリをコピーする
cp -r ~/Downloads/OpenCV-android-sdk/sdk/native/libs ~/Desktop/face-detection/openCVLibrary310/src/main/jniLibs
参考:java - android Static Initialization opencv 3.0 Cannot load library "opencv_java3" - Stack Overflow
まとめ
OpenCV for Android 3.x系じゃ動かなかったから2.4.11を試したという記事もあるが3.1でちゃんと動くことが確認できた
Gist的なメモ
openCVLibrary310/build.gradle
apply plugin: 'com.android.library' android { compileSdkVersion 21 buildToolsVersion "25.0.0" defaultConfig { minSdkVersion 21 targetSdkVersion 21 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } }
openCVSamplefacedetection/build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "25.0.0" defaultConfig { applicationId "org.opencv.samples.facedetect" minSdkVersion 21 targetSdkVersion 21 ndk { moduleName "detection_based_tracker" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } sourceSets.main { jni.srcDirs = [] // This prevents the auto generation of Android.mk jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project. } task buildNative(type: Exec, description: 'Compile JNI source via NDK') { def ndkDir = "/Users/rose/Library/Android/sdk/ndk-bundle" commandLine "$ndkDir/ndk-build", '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source '-j', Runtime.runtime.availableProcessors(), 'all', 'NDK_DEBUG=1' } task cleanNative(type: Exec, description: 'Clean JNI object files') { def ndkDir = "/Users/rose/Library/Android/sdk/ndk-bundle" commandLine "$ndkDir/ndk-build", '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source 'clean' } clean.dependsOn 'cleanNative' tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn buildNative } } dependencies { compile project(':openCVLibrary310') }
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' } } allprojects { repositories { jcenter() } }
gradle.propeties
android.useDeprecatedNdk=true
local.properties
## This file is automatically generated by Android Studio. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must *NOT* be checked into Version Control Systems, # as it contains information specific to your local configuration. # # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. #Sun Dec 04 18:10:43 JST 2016 ndk.dir=/Users/rose/Library/Android/sdk/ndk-bundle sdk.dir=/Users/rose/Library/Android/sdk
settings.gradle
include ':openCVLibrary310' include ':openCVSamplefacedetection'