下面我们来看看这个文件:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_marakana_FibLib */ #ifndef _Included_com_marakana_FibLib #define _Included_com_marakana_FibLib #ifdef __cplusplus extern "C" { #endif /* * Class: com_marakana_FibLib * Method: fibN * Signature: (I)J */ JNIEXPORT jlong JNICALL Java_com_marakana_FibLib_fibN (JNIEnv *, jclass, jint); /* * Class: com_marakana_FibLib * Method: fibNI * Signature: (I)J */ JNIEXPORT jlong JNICALL Java_com_marakana_FibLib_fibNI (JNIEnv *, jclass, jint); #ifdef __cplusplus } #endif #endif
#include "com_marakana_FibLib.h" /*(注释一)*/ /* 递归斐波那契算法(注释二)*/ long fibN(long n) { if(n<=0) return 0; if(n==1) return 1; return fibN(n-1) + fibN(n-2); } /*迭代斐波那契算法(注释三)*/ long fibNI(long n) { long previous = -1; long result = 1; long i=0; int sum=0; for (i = 0; i <= n; i++) { sum = result + previous; previous = result; result = sum; } return result; } /*头文件中生产的JNI方法的签名(注释四)*/ JNIEXPORT jlong JNICALL Java_com_marakana_FibLib_fibN (JNIEnv *env, jclass obj, jlong n) { return fibN(n); } /*头文件中生产的JNI方法的签名(注释五) */ JNIEXPORT jlong JNICALL Java_com_marakana_FibLib_fibNI (JNIEnv *env, jclass obj, jlong n) { return fibNI(n); }
package com.marakana; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fibonacci extends Activity implements OnClickListener { TextView textResult; Button buttonGo; EditText editInput; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Find UI views editInput = (EditText) findViewById(R.id.editInput); textResult = (TextView) findViewById(R.id.textResult); buttonGo = (Button) findViewById(R.id.buttonGo); buttonGo.setOnClickListener(this); } public void onClick(View view) { int input = Integer.parseInt(editInput.getText().toString()); //注释一 long start, stop; long result; String out = ""; // Dalvik - Recursive start = System.currentTimeMillis(); //注释二 result = FibLib.fibJ(input); //注释三 stop = System.currentTimeMillis(); //注释四 out += String.format("Dalvik recur sive: %d (%d msec)", result, stop - start); // Dalvik – Iterative start = System.currentTimeMillis(); result = FibLib.fibJI(input); //注释五 stop = System.currentTimeMillis(); out += String.format("\nDalvik iterative: %d (%d msec)", result, stop - start); // Native - Recursive start = System.currentTimeMillis(); result = FibLib.fibN(input); //注释六 stop = System.currentTimeMillis(); out += String.format("\nNative recursive: %d (%d msec)", result, stop - start); // Native - Iterative start = System.currentTimeMillis(); result = FibLib.fibNI(input); //注释七 stop = System.currentTimeMillis(); out += String.format("\nNative iterative: %d (%d msec)", result, stop - start); textResult.setText(out); //注释八 } }
注释八:格式化输出并打印在屏幕上。
linc注:
把ndk-build写到环境变量:
用文本编辑器打开/etc/profile
在profile文件末尾加入:
NDK=/home/linc/android/android-ndk-r5b export NDK PATH=$NDK:$PATH export PATH