//我自己测试的一个例子//nativecpp.h#ifndef NATIVECPP_H_#define NATIVECPP_H_int nativeAdd(int x,int y); //C++本地函数声明#endif /* NATIVECPP_H_ *///====================================//nativecpp.cpp#include "nativecpp.h"int nativeAdd(int x,int y) //C++本地函数实现{ return (x*x+y*y);}//====================================//jnicpp.h#ifndef JNICPP_H_#define JNICPP_H_#include#include extern "C"{JNIEXPORT jint JNICALL Java_com_yprnet_myndkapp1_NDKApp1Activity_Toadd(JNIEnv *env,jobject obj,jint value1,jint value2); //标准的JNI调用函数声明}#endif /* JNICPP_H_ *///====================================//MyNDKApp1.cpp#include "jnicpp.h"#include "nativecpp.h"jint JNICALL Java_com_yprnet_myndkapp1_NDKApp1Activity_Toadd(JNIEnv *env,jobject obj,jint value1,jint value2) //标准的JNI调用函数实现{ value1=value1*10; value2=value2*10; return nativeAdd(value1,value2); //JNI函数调用C++本地函数}//====================================//Android.mkLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := MyNDKApp1### Add all source file names to be included in lib separated by a whitespaceLOCAL_SRC_FILES := MyNDKApp1.cpp nativecpp.cppinclude $(BUILD_SHARED_LIBRARY)//====================================//NDKApp1Activity.javapackage com.yprnet.myndkapp1;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.TextView;import android.widget.Button;public class NDKApp1Activity extends Activity { static { System.loadLibrary("MyNDKApp1"); //加载库,库名为去掉前缀lib和后缀so } public native int Toadd(int x,int y); //定义原生方法 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ndkapp1); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ int num=Toadd(3,5); //调用NDK方法相加 String str=String.valueOf(num); TextView myTextView=null; myTextView=(TextView)findViewById(R.id.textView1); myTextView.setText((CharSequence)str); myTextView=null; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_ndkapp1, menu); return true; }}//======================================//======================================//======================================//========================//Android NDK开发包上的一个例子//android-ndk-r8b\samples\two-libs//========================//first.h#ifndef FIRST_H#define FIRST_Hextern int first(int x, int y);#endif /* FIRST_H *///========================//first.c#include "first.h"int first(int x, int y){ return x + y;}//========================//second.c#include "first.h"#include jintJava_com_example_twolibs_TwoLibs_add( JNIEnv* env, jobject this, jint x, jint y ){ return first(x, y);}//========================//Android.mkLOCAL_PATH:= $(call my-dir)# first lib, which will be built statically#include $(CLEAR_VARS)LOCAL_MODULE := libtwolib-firstLOCAL_SRC_FILES := first.cinclude $(BUILD_STATIC_LIBRARY)# second lib, which will depend on and include the first one#include $(CLEAR_VARS)LOCAL_MODULE := libtwolib-secondLOCAL_SRC_FILES := second.cLOCAL_STATIC_LIBRARIES := libtwolib-firstinclude $(BUILD_SHARED_LIBRARY)//========================//TwoLibs.javapackage com.example.twolibs;import android.app.Activity;import android.widget.TextView;import android.os.Bundle;public class TwoLibs extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); int x = 1000; int y = 42; // here, we dynamically load the library at runtime // before calling the native method. // System.loadLibrary("twolib-second"); int z = add(x, y); tv.setText( "The sum of " + x + " and " + y + " is " + z ); setContentView(tv); } public native int add(int x, int y);}