android ndk 开发实例
根据自己这几天的使用过程,写出了这篇十分简单明了的 android ndk 开发实例,没有
过多的解释,其实也不用太多解释,本文所举例子比 hellojni 稍微复杂那么一点点。本文假
设读者已配置好 android ndk 开发环境。(未配置好环境请看:
http://www.mydreamsky.tk/?p=428。)
一、在 eclipse 里新建一个工程,工程名为:MyJNI,package 为:com.jpf.myjni,其他按
照惯例。Com/jpf/myjni /MyJNI.java 的代码为:
package com.jpf.myjni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class MyJNI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
TextView tv = new TextView( this );
tv.setText( stringFromJNI() +add());
System. out .println( "Here we go ..." );
setContentView(tv);
System. out .println( "Done!" );
}
public native String stringFromJNI();
public native int add();
static {
System.loadLibrary ( "myjni" );
}
}
二、在工程目录下新建 jni 目录,在其下新建一个文件:myjni.c,代码为:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <jni.h>
#include <android/log.h>
#define LOG_TAG "MYJNI"
#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
__VA_ARGS__)
static char s_string[] = "My god, I did it!";
int add(){
int x,y;
x=100;
y=100;
评论0