1. Two method
1-1. NDK
[Ref] https://developer.android.com/ndk/samples/sample_hellojni.html?hl=ko
1-2. CMake
[Ref] https://developer.android.com/studio/projects/add-native-code.html?hl=ko
[Ref] https://developer.android.com/ndk/samples/sample_hellojni.html?hl=ko
2. NDK detail :
[Ref : https://www.youtube.com/watch?v=RmPuwdxR1qs ]
[Ref : http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/]
[Ref : http://intellij.my/2017/05/10/how-to-create-a-c-library-with-ndk-on-android-studio/ ]
2-1. Android.mk
LOCAL_PATH := $(call my-dir)
OPENCV_INSTALL_MODULES := on
include $(CLEAR_VARS)
include D:/Data/Libraries/opencv-2.4.13.3-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_C_INCLUDES := D:/Data/Libraries/opencv-2.4.13.3-android-sdk/OpenCV-android-sdk/sdk/native/jni/include
LOCAL_C_INCLUDES += D:/Data/Libraries/opencv-2.4.13.3-android-sdk/OpenCV-android-sdk/sdk/native/jni/include/opencv
LOCAL_C_INCLUDES += D:/Data/Libraries/opencv-2.4.13.3-android-sdk/OpenCV-android-sdk/sdk/native/jni/include/opencv2
LOCAL_MODULE := liveness-lib
LOCAL_SRC_FILES := livenesslib.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_CFLAGS += -fexceptions
include $(BUILD_SHARED_LIBRARY)
2-2. Application.mk
APP_MODULES := liveness-lib
APP_ABI := armeabi armeabi-v7a x86
APP_STL := stlport_static
2-3. export functions
//// Created by kisat on 2018-04-19.//
#include "com_liveness_livenesssample_LivenessNDK.h"#include "liveness.h"
#ifdef __cplusplus
extern "C" {
#endif/* * Class: com_liveness_livenesssample_LivenessNDK * Method: getVersion * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_liveness_livenesssample_LivenessNDK_getVersion
(JNIEnv *env, jobject)
{
return (*env).NewStringUTF("0.0.2");}
#ifdef __cplusplus
}
#endif
2-4. Usage ( how to use the exported functions )
package com.liveness.livenesssample;
public class LivenessNDK {
static {
System.loadLibrary("liveness-lib"); System.loadLibrary("opencv_java"); }
public native String getVersion();}
LivenessNDK liveNessNDK = null;
LivenessWrapper() {
liveNessNDK = new LivenessNDK();}
public String getVersion() {
return liveNessNDK.getVersion();}
2-5. External tools
2-5-1. javah
* Name : Javah
* Description : Tool for make header
* Program : jdk install path + javah.exe (ex>
C:\Program Files\Java\jdk1.8.0_121\bin\javah.exe )
* Arguments : -classpath $Classpath$ -v -jni $FileClass$
* Working Directory : $ProjectFileDir$\app\src\main\jni
2-5-2. ndk-build
* Name : ndk-build
* Program : ndk directory in android sdk folder ( ex> D:\Android_SDK\ndk-bundle\ndk-build.cmd )
* Working Directory : $ProjectFileDir$/app/src/main
2-5-3. ndk-clean
* Name : ndk-build clean
* Program : ndk directory in android sdk folder ( ex> D:\Android_SDK\ndk-bundle\ndk-build.cmd )
* Argument : clean
* Working Directory : $ProjectFileDir$/app/src/main
3. CMake detail
3-1.
---------------------------------------------------------------------------------------
Error1> Could not find method ndk() for arguments android
Solution1> ndk block in gradle file to defaultConfig
example1>
It turns out that this code
ndk {
moduleName "hello-android-jni"
}
should be placed under "defaultConfig" block:
defaultConfig {
applicationId "com.chenql.helloandroidjni"
minSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
moduleName "hello-android-jni"
}
}
instaed of after "buildTypes" block.
answered Nov 9 '16 at 9:22
Reference1> https://stackoverflow.com/questions/40501986/could-not-find-method-ndk-for-arguments
------------------------------------------------------------------------------------------
Error2> Error: unmappable character for encoding MS949
Solution2> javac myjavasource.java -encoding UTF-8
------------------------------------------------------------------------------------
댓글
댓글 쓰기