下面以NDK提供的two-libs为例子,走一遍如何在jni层调用其他C文件的函数,原例子在second.c这个jni层文件中调用first.c里面的C函数。
通过Androi.mk文件编译生成一个动态库文件。
1、建立android工程,编写java对应JNI层的本地接口:
package 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.
//
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);
static
{
System.loadLibrary("twolib-second");
}
}