Android NDK Example


- source project :

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 {

    public native String stringFromJni();

3. make a '' 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 '' file under the 'jni' folder.

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

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 '' 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 '' file without 'lib' prefix and '.so' surfix.


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;
    public void onCreate(Bundle savedInstanceState) {
       m_jniProject = new NdkExample();
        TextView tv = (TextView)findViewById(;

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

13. finish.

Posted by 바츠05 트랙백 0 : 댓글 0