<NDKHOME>
to refer to the root directory in which you installed your NDK. For me that's /Users/marko/WorkArea/android-ndk-1.6_r1
. I'm assuming all other directories and files are relative to your Eclipse project root, in my case /Users/marko/Workspace/Android/NDKDemo
.src
directory in your Eclipse project. It serves as the glue to the native code that we'll write later.
package com.marakana;
public class NativeLib {
static {
System.loadLibrary("ndk_demo");
}
/**
* Adds two integers, returning their sum
*/
public native int add( int v1, int v2 );
/**
* Returns Hello World string
*/
public native String hello();
}
<EclipseWorkspace>/NDKDemo/bin
), run javah tool to create the JNI header file.<EclipseWorkspace>/NDKDemo/jni
).<EclipseWorkspace>/NDKDemo/bin
to <EclipseWorkspace>/NDKDemo/jni
NDKDemo/bin$ javah -jni com.marakana.NativeLib
NDKDemo/bin$ mv com_marakana_NativeLib.h ../jni/
<EclipseWorkspace>/NDKDemo/jni/
folder, create ndk_demo.c
file. This is where we'll implement the native code. To start, copy thefunction signatures from the header file, and provide theimplementation for those functions. In this example, the header filelooks like this:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_marakana_NativeLib */
#ifndef _Included_com_marakana_NativeLib
#define _Included_com_marakana_NativeLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_marakana_NativeLib
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_marakana_NativeLib
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
#include "com_marakana_NativeLib.h"
JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
(JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");
}
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
(JNIEnv * env, jobject obj, jint value1, jint value2) {
return (value1 + value2);
}
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c
include $(BUILD_SHARED_LIBRARY)
<NDKHOME>/apps/ndk_demo/
and inside this folder create the Application file:
APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES := ndk_demo
<NDKHOME>/apps/ndk_demo/project
to your Eclipse project:ln -s ~/Workspace/Android/NDKDemo <NDKHOME>/apps/ndk_demo/project
<NDKHOME>/apps/ndk_demo/project
directory, then copy back to Eclipse. I'm running all this on Mac OS X 10.6 and I assume Linux-type shell.make APP=ndk_demo
android-ndk-1.5_r1$ make APP=ndk_demo
Android NDK: Building for application 'ndk_demo'
Compile thumb : ndk_demo <= sources/ndk_demo/ndk_demo.c
SharedLibrary : libndk_demo.so
Install : libndk_demo.so => apps/ndk_demo/project/libs/armeabi
/lib/
directory containing your libndk_demo.so
file.NativeLib
class and from there on, it's just a regular Java object.
package com.marakana;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NDKDemo extends Activity {
NativeLib nativeLib;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nativeLib = new NativeLib();
String helloText = nativeLib.hello();
// Update the UI
TextView outText = (TextView) findViewById(R.id.textOut);
outText.setText(helloText);
// Setup the UI
Button buttonCalc = (Button) findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);
public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());
res = nativeLib.add(v1, v2);
result.setText(new Integer(res).toString());
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="NDK Demo"
android:textSize="22sp" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textOut"
android:text="output"></TextView>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/value1"
android:hint="Value 1"></EditText>
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="+"
android:textSize="36sp"></TextView>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/value2"
android:hint="Value 2"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/buttonCalc"
android:text="="></Button>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="result"
android:textSize="36sp" android:id="@+id/result"></TextView>
</LinearLayout>
聯(lián)系客服