2011年3月31日 星期四

[Android] Create a new NDK project by yourself

Let's create a new project, and try to mimic the hello-jni.
before the exercise, assume you've already installed NDK.
If not, see the article first.
http://twnin.blogspot.com/2011/03/how-to-use-ndk-android-ndk-rb5.html

---------------------

As usual, create a new Android project
open Eclipse
Files->New->Project->Android Project
Project Name: PlayJni
Build Target: Android 2.2
Application name: PlayJni
Package name: com.play.PlayJni
Create Activety: PlayJni
Min SDK Version: 8
Click 'Finish'

---------------------------
Then, create necessary file for jni.
Right click on project name.
New->folder
Folder name: jni

Right click on project name.
New->file
Select folder: PlayJni/jni
File name: myJni.c

Right click on project name.
New->file
Select folder: PlayJni/jni
File name: Android.mk

Now, you'll see the 'Package Explore' as below.


------------------------------

open PlayJni/jni/myJni.c
Paste the code
#include <string.h>
#include <jni.h>

jstring Java_com_play_PlayJni_PlayJni_stringFromJNI( JNIEnv* env, jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI by myself!");
}

open PlayJni/jni/Android.mk
Paste the script

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

include $(BUILD_SHARED_LIBRARY)
modify PlayJni.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)this.findViewById(R.id.TextView1);
        tv.setText(stringFromJNI());
    }
   
    public native String stringFromJNI();
    static {
        System.loadLibrary("myJni");
    }
 ---------------------------

execute cygwin
Go to 'jni' subdirectory of your project directory.
cd /cygdrive/c/workspace/PlayJni/jni/
ndk-build
like this







launch emulator and run project

沒有留言:

張貼留言