1.Activity
package com.devchina.ndk4; import android.app.Activity; import android.os.Bundle; import android.view.View; public class DevchinaActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void click(View view){ DevchinaProvider devchina = new DevchinaProvider(); devchina.cCallJava(); } public void clickAdd(View view){ DevchinaProvider devchina = new DevchinaProvider(); devchina.cCallAdd(); } public void clickStrng(View view){ DevchinaProvider devchina = new DevchinaProvider(); devchina.cCallString(); } }
2.native
package com.devchina.ndk4; public class DevchinaProvider { static { System.loadLibrary("devchina"); } public native void cCallJava(); public native void cCallString(); public native int cCallAdd(); //1.调用java空方法 public void callJava(){ System.out.println("i come from java"); } public int add(int x,int y){ System.out.println("devchina java res = "+(x+y)); return x+y; } public void printString(String s){ System.out.println("devchina java "+ s); } }
3.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_devchina_ndk4_DevchinaProvider */ #ifndef _Included_com_devchina_ndk4_DevchinaProvider #define _Included_com_devchina_ndk4_DevchinaProvider #ifdef __cplusplus extern "C" { #endif /* * Class: com_devchina_ndk4_DevchinaProvider * Method: cCallJava * Signature: ()V */ JNIEXPORT void JNICALL Java_com_devchina_ndk4_DevchinaProvider_cCallJava (JNIEnv *, jobject); /* * Class: com_devchina_ndk4_DevchinaProvider * Method: cCallAdd * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_devchina_ndk4_DevchinaProvider_cCallAdd (JNIEnv *, jobject); /* * Class: com_devchina_ndk4_DevchinaProvider * Method: cCallString * Signature: ()V */ JNIEXPORT void JNICALL Java_com_devchina_ndk4_DevchinaProvider_cCallString (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
4.c文件
#include<stdio.h> #include<jni.h> #include"com_devchina_ndk4_DevchinaProvider.h" #include <android/log.h> #define LOG_TAG "devchina" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) JNIEXPORT void JNICALL Java_com_devchina_ndk4_DevchinaProvider_cCallJava (JNIEnv * env, jobject obj){//ojb,谁调用了此Native方法,这个Obj就是谁 //new 出java对象 jclass clazz = (*env)->FindClass(env,"com/devchina/ndk4/DevchinaProvider");//注意:不能为“.” if(clazz==0){//没有找到类 LOGI("can't find devchina class"); }else{ LOGI("find dechina class"); } //获取类中的方法 jmethodID j_method = (*env)->GetMethodID(env,clazz,"callJava","()V");//最后一个参数是表示方法的签名 if(j_method==0){ LOGI("can't get method"); }else{ LOGI("get method"); } (*env)->CallVoidMethod(env,obj,j_method);//第二个参数表示调用的类对象 } JNIEXPORT jint JNICALL Java_com_devchina_ndk4_DevchinaProvider_cCallAdd (JNIEnv * env, jobject obj){ //1.new java obj jclass clazz = (*env)->FindClass(env,"com/devchina/ndk4/DevchinaProvider"); //2.get method id jmethodID mId = (*env)->GetMethodID(env,clazz,"add","(II)I"); //3.invoke method jint res = (*env)->CallIntMethod(env,obj,mId,5,6); LOGI("c log %d",res); } JNIEXPORT void JNICALL Java_com_devchina_ndk4_DevchinaProvider_cCallString (JNIEnv * env, jobject obj){ jclass clazz = (*env)->FindClass(env,"com/devchina/ndk4/DevchinaProvider"); jmethodID mId = (*env)->GetMethodID(env,clazz,"printString","(Ljava/lang/String;)V"); (*env)->CallVoidMethod(env,obj,mId,(*env)->NewStringUTF(env," devchina from c ")); }
5.mk文件
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE :=devchina LOCAL_SRC_FILES :=devchina.c LOCAL_LDLIBS += -llog include $(BUILD_SHARED_LIBRARY)
6.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用c代码" android:onClick="click" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用c代码,加法" android:onClick="clickAdd" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用c代码,打印String" android:onClick="clickStrng" /> </LinearLayout>
package com.devchina.ndk4; public class DevchinaProvider { static { System.loadLibrary("devchina"); } public native void cCallJava(); public native void cCallString(); public native int cCallAdd(); //1.调用java空方法 public void callJava(){ System.out.println("i come from java"); } public int add(int x,int y){ System.out.println("devchina java res = "+(x+y)); return x+y; } public void printString(String s){ System.out.println("devchina java "+ s); } }
资源下载:http://download.csdn.net/detail/hudan2714/4328983