티스토리 뷰

반응형

Android Studio NDK 설정

   NDK 다운받기

http://developer.android.com/ndk/index.html

위 페이지로 이동하면 NDK 사용방법과 Document, Download를 제공한다.


NDK 다운로드 페이지 http://developer.android.com/ndk/downloads/index.html

OS 환경에 맞게 다운받아 NDK를 사용할 위치에 압축을 푼다.

다운로드 페이지에서 OS별로 압축 푸는 방법까지 설명되어 있다.



   Android Stuido NDK 설정 - javah

먼저 jni에서 사용할 헤더파일을 만들때 필요한 javah 설정을 한다.


File - Settings - Tools - External Tools 



※ Tool settings

 Program :  jdk 폴더내 bin에 있는 javah.exe  Path

 Paramaters :  -classpath "$Classpath$" -v -jni $FileClass$

 Working directory :  $ProjectFileDir$\app\src\main\jni

위와 같이 설정한다.


※ 수정

이전 : Paramaters :  -classpath $Classpath$ -v jni $FileClass$ 

2017.02.01 수정 : Paramaters :  -classpath "$Classpath$" -v -jni $FileClass$ 




NDKTest.java 클래스를 생성하여 아래와 같이 native 메소드를 작성한다. 추후 헤더파일에 자동으로 생성될 메소드이다.

public class NDKTest {
    public native void ndkTestMethod(String msg);
}

Ctrl + B 를 통해 빌드 후 만들고자 하는 java파일 우클릭을 통해 빌드하여 .class파일이 생성되게 한다.



NDK - javah 를 선택하면 아래와 같은 콘솔 멘트(Create file RegularFileObject[헤더파일명])와 함께 jni 폴더에 com_test_dwfox_jnitest_NDKTest.h  파일이 생성된걸 알 수 있다.

이 헤더 파일을 가지고 NDKTest.java에서 C/C++ 라이브러리를 로드하여 native 메소드를 작성한다.

아래는 생성된 com_test_dwfox_jnitest_NDKTest.h 이다. native 메소드가 자동으로 변형된걸 알 수 있다.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_test_dwfox_jnitest_NDKTest */

#ifndef _Included_com_test_dwfox_jnitest_NDKTest
#define _Included_com_test_dwfox_jnitest_NDKTest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_test_dwfox_jnitest_NDKTest
 * Method:    ndkTestMethod
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_test_dwfox_jnitest_NDKTest_ndkTestMethod
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif





   Android Stuido NDK 설정 - ndk-build


javah와 마찬가지로 File - Settings - Tools - External Tools







※ Tool settings

 Program :  %ANDROID_NDK%ndk-build.cmd # ndk 압축 해제 한곳 위치에 ndk-build.cmd 파일

 Paramaters :  

 Working directory :  $ProjectFileDir$\app\src\main\jni

위와 같이 설정한다.



C/C++ 파일을 만들고 위에서 생성했던 해더파일을 include 하여 필요한 jni 코딩후 

//NDKTest.cpp
#include "com_test_dwfox_jnitest_NDKTest.h"

JNIEXPORT void JNICALL Java_com_test_dwfox_jnitest_NDKTest_ndkTestMethod
(JNIEnv * env, jobject ob, jstring str)
{

}

jni 폴더에 Android.mk와 Application.mk 파일로 빌드 설정을 하고 

//Android.mk
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := ndkTest
LOCAL_SRC_FILES := ndkTest.cpp

include $(BUILD_SHARED_LIBRARY)



ndk-build를 해주면  libs에 .so 파일이 생성 된걸 볼 수 있다. 이 라이브러리를 사용할 java class에서 System.loadLibrary를 통해 사용 하면 된다.



※ NDK Sample 을 이용하여 android.mk와 Application.mk를 복사하거나 분석후 적용하는 것이 가장 빠를 수 있습니다.

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함