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.




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




윈도우에서 계정에 한글이 포함되어 있으면,
AVD가 실행이 되지 않는다.

이럴때는, 영문계정을 만들어서 해도 되겠지만,
간단하게 AVD의 경로를 수정해도 된다.

android move avd -n AVD8 -p c:\avd\AVD8

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