MainActivity
package com.example.hello2;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn;
static{
System.loadLibrary("hello2");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
public native void add(int a,int b);
public native DiskInfo getStruct(DiskInfo disk_info);
@Override
public void onClick(View arg0) {
DiskInfo disk_info=new DiskInfo(1,"ZKK");
getStruct(disk_info);
Log.i("zhouke", "::"+disk_info.name+"::"+disk_info.serial);
}
}
package com.example.hello2;
public class DiskInfo {
public String name; //
public int serial; //
public DiskInfo(int num, String myname) {
serial = num;
name = myname;
}
}
.h 文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_example_hello2_MainActivity */
#ifndef _Included_com_example_hello2_MainActivity
#define _Included_com_example_hello2_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_hello2_MainActivity
* Method: add
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_com_example_hello2_MainActivity_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_example_hello2_MainActivity
* Method: getStruct
* Signature: (Lcom/example/hello2/DiskInfo;)Lcom/example/hello2/DiskInfo;
*/
JNIEXPORT jobject JNICALL Java_com_example_hello2_MainActivity_getStruct
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
c文件
#include "com_example_hello2_MainActivity.h"
#include
#include
#include
#define LOG_TAG "cTag"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
struct{
char name[256];
int serial;
};
JNIEXPORT void JNICALL Java_com_example_hello2_MainActivity_add
(JNIEnv * env, jobject obj, jint x, jint y)
{
int result = x + y;
LOGD("result=%d", result);
}
JNIEXPORT jobject JNICALL Java_com_example_hello2_MainActivity_getStruct
(JNIEnv * env, jobject obj, jobject diskobj){
jclass objectClass = (*env)->FindClass(env, "com/example/hello2/DiskInfo");
jfieldID name = (*env)->GetFieldID(env, objectClass,"name","Ljava/lang/String;");
jfieldID serial=(*env)->GetFieldID(env,objectClass,"serial","I");
(*env)->SetObjectField(env,diskobj,name, (*env)->NewStringUTF(env, "hello world"));
(*env)->SetIntField(env,diskobj,serial,10);
//(*env)->setIn
return diskobj;
}
示例2
MainActiviy
package com.example.hello2;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn;
static {
System.loadLibrary("hello2");
}
public class Foo {
public int len;
public String name;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
public native void add(int a, int b);
public native DiskInfo getStruct(DiskInfo disk_info);
private static native int foo(Foo fooObj);
@Override
public void onClick(View arg0) {
/*
* DiskInfo disk_info=new DiskInfo(1,"ZKK"); getStruct(disk_info);
* Log.i("zhouke", "::"+disk_info.name+"::"+disk_info.serial);
*/
Foo foo = new Foo();
foo(foo);
Log.i("zhouke", foo.name);
Log.i("zhouke", foo.len+"");
}
}
hello2.c
#include "com_example_hello2_MainActivity.h"
#include
#include
#include
#define LOG_TAG "cTag"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
struct {
char name[256];
int serial;
};
typedef struct Foo {
int len;
char name[100];
} Foo_t;
JNIEXPORT void JNICALL Java_com_example_hello2_MainActivity_add(JNIEnv * env,
jobject obj, jint x, jint y) {
int result = x + y;
LOGD("result=%d", result);
}
JNIEXPORT jobject JNICALL Java_com_example_hello2_MainActivity_getStruct(
JNIEnv * env, jobject obj, jobject diskobj) {
jclass objectClass = (*env)->FindClass(env, "com/example/hello2/DiskInfo");
jfieldID name = (*env)->GetFieldID(env, objectClass, "name",
"Ljava/lang/String;");
jfieldID serial = (*env)->GetFieldID(env, objectClass, "serial", "I");
(*env)->SetObjectField(env, diskobj, name,
(*env)->NewStringUTF(env, "hello world"));
(*env)->SetIntField(env, diskobj, serial, 10);
//(*env)->setIn
return diskobj;
}
JNIEXPORT jint JNICALL Java_com_example_hello2_MainActivity_foo(JNIEnv *env,
jobject obj, jobject fooObj) {
Foo_t * bar = malloc(sizeof(Foo_t));
jclass clazz;
jfieldID fid;
//init the bar data of C
strcpy(bar->name, "Yachun Miao");
bar->len = strlen(bar->name);
// mapping bar of C to foo
clazz = (*env)->GetObjectClass(env, fooObj);
if (0 == clazz) {
LOGD("0==clazz");
return (-1);
}
fid = (*env)->GetFieldID(env, clazz, "len", "I");
//(*env)->SetLongField(env, fooObj, fid, bar->len);
(*env)->SetIntField(env,fooObj,fid,bar->len);
fid = (*env)->GetFieldID(env, clazz, "name", "Ljava/lang/String;");
jstring name = (*env)->NewStringUTF(env, bar->name);
(*env)->SetObjectField(env, fooObj, fid, name);
free(bar);
return 0;
}
h 文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_example_hello2_MainActivity */
#ifndef _Included_com_example_hello2_MainActivity
#define _Included_com_example_hello2_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_hello2_MainActivity
* Method: add
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_com_example_hello2_MainActivity_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_example_hello2_MainActivity
* Method: getStruct
* Signature: (Lcom/example/hello2/DiskInfo;)Lcom/example/hello2/DiskInfo;
*/
JNIEXPORT jobject JNICALL Java_com_example_hello2_MainActivity_getStruct
(JNIEnv *, jobject, jobject);
JNIEXPORT jint JNICALL Java_com_example_hello2_MainActivity_foo
(JNIEnv *, jclass, jobject);
#ifdef __cplusplus
}
#endif
#endif
.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello2
LOCAL_SRC_FILES := hello2.c
LOCAL_LDLIBS :=-llog
include $(BUILD_SHARED_LIBRARY)