step one - make java class
src/com/rect/ndktest/NDKTest.java
package com.rect.ndktest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class NDKTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ndktest);
TextView myTextView = new TextView(this);
myTextView.setText( stringNDKTest() );
setContentView(myTextView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ndktest, menu);
return true;
}
public native String stringNDKTest();
public native String stringNDKTest2();
static {
System.loadLibrary("NDKTest");
}
}
step two - NDK 编写C/C++ 命令行格式
生成H头文件
javah -classpath src -d jni com.rect.ndktest.NDKTest
javah
- java JDK tool
-classpaht src
- java class path
-d jni
- 生成路径
com.rect.ndktest.NDKTest
- 生成的H文件
step three - 根据H头文件编写C++类
NDKTest.c
#include <string.h>
#include <jni.h>
#include <stdio.h>
jstring
Java_com_rect_ndktest_NDKTest_stringNDKTest( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello Test NDK !");
}
jstring
Java_com_rect_ndktest_NDKTest_stringNDKTest2( JNIEnv* env,
jobject thiz )
{
printf("hello my good day");
return (*env)->NewStringUTF(env, "Hello Test NDK 222 !");
}
step four - make the android.mk file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_MODULE := NDKTest
LOCAL_SRC_FILES := NDKTest.c
include $(BUILD_SHARED_LIBRARY)