Android NDK Example [開發] 안드로이드


Android NDK Example


 

- source project : NdkExample.zip


1. make a new 'NdkExample' project under the 'ndk-root/apps' folder.


2. make a JNI java file(class) for wrapping.


public class NdkExample {
    static {
        System.loadLibrary("NdkExample");
    }

    public native String stringFromJni();
}







3. make a 'Application.mk' file under the 'ndk-root/apps/NdkExample' folder.


APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES := NdkExample




4. make a 'NdkExample/project' and make 'jni' and 'libs' folder under the 'project' folder.




5. make a 'Android.mk' file under the 'jni' folder.


LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := NdkExample
LOCAL_SRC_FILES := NdkExample.c
include $(BUILD_SHARED_LIBRARY)




6. move to 'NdkExample/bin' folder and make a header file from the 'NdkExample.class' file


javah com.example.NdkExample



7. move the header file to 'NdkExample/project/jni' folder and rename to 'NdkExample.h' for using easier.



8. make a 'NdkExample.c' file under the 'NdkExample/project/jni' folder.


#include "NdkExample.h"
#include <string.h>

jstring Java_com_example_NdkExample_stringFromJni(JNIEnv *env, jobject thiz)
{
    return (*env)->NewStringUTF(env, "Hello world from native code.");
}



9. compile the project under the 'ndk-root' folder.


make APP=NdkExample



10. move the 'libNdkExample.so' file to 'NdkExample/libs/armeabi' folder.
when you don't have the 'NdkExample/libs/armeabi' folder, you should make it.

*NOTE: You must change the name of loadLibrary like below.
the name is used by 'libNdkExample.so' file without 'lib' prefix and '.so' surfix.

System.loadLibrary("NdkExample");




11. use the library on your Android SDK project that you made(ex, 'NdkExample')


public class NdkExampleMain extends Activity {
    /** Called when the activity is first created. */
    NdkExample m_jniProject;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
       m_jniProject = new NdkExample();
        TextView tv = (TextView)findViewById(R.id.string_from_jni);
        tv.setText(m_jniProject.StringFromJNI());
       
    }
}






12. then, you can see the "Hello world from native code." text on your AVD emulator.




13. finish.


핑백

덧글

  • netohk 2010/06/01 16:02 # 삭제 답글

    Step5 :
    MyProject.c ==> NdkExample.c
  • 나도야 2010/06/04 10:20 # 답글

    감사합니다~
    오타 수정했습니다 ^^
  • 2010/07/05 22:05 # 삭제 답글 비공개

    비공개 덧글입니다.
  • 나도야 2010/07/06 16:53 # 답글

    말씀하신 부분에서 nullpointerexception 이 발생하는 이유는 여러가지 일 수 있습니다.
    우선은 예외가 setText 메소드에서 발생하는지, stringFromJni 메소드에서 발생하는지를 알아야겠네요.
    로그나 디버깅으로 어디서 예외가 발생하는지를 찍어보세요.
    제 생각으로는 컴파일한 so 파일이 잘못되었거나, 위치가 잘못되었을것 같네요.
  • dd 2013/05/02 06:27 # 삭제 답글

    9번에서 약간 막혔는데요.. ndk 루트 폴더가서 make APP=NdkExample 하면
    자동으로 so 파일이 만들어진다고 적혀있는데 기본 세팅이 좀 빠진것같습니다..
    제가 가진 ndk 폴더는 그냥 다운받고 압축푼 형태인데 NdkExample 이 어딧는지 어떻게 알죠?
댓글 입력 영역