所以我正在玩弄 JNI,看看它在系统调用方面的限制是什么。我第一个学习 JNI 如何工作的 C 文件仍然运行良好。我正在尝试添加另一个读取 uname 并连接成单个字符串然后返回的 C++ 文件。但是,链接器实际上无法找到新的函数定义 (getKernelInfo())。我已经尝试过很明显的方法,比如仔细检查函数/java 包的类型和名称。但是我似乎找不到问题所在。
以下是相关文件:
安卓.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := example
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := \
example.c \
kernel.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
应用程序.mk
APP_STL := stlport_static
内核.cpp
#include <jni.h>
#include <sys/utsname.h>
#include <string>
using namespace std;
JNIEXPORT jstring JNICALL
Java_com_test_myjniapp_MainActivity_getKernelInfo(JNIEnv * env, jobject obj)
{
struct utsname info;
uname(&info);
string sysInfo = "";
char** names = (char**)&(info.sysname);
for(int i = 0; i < 5; i++, names++) {
sysInfo = sysInfo + string(*names);
if(i != 4)
sysInfo = sysInfo + ", ";
}
jstring res = env->NewStringUTF((const char*)sysInfo.c_str());
return res;
}
MainActivity.java
package com.test.myjniapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
static {
System.loadLibrary("example");
}
private TextView textUID;
private Button btnPress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textUID = (TextView) findViewById(R.id.txtUID);
btnPress = (Button) findViewById(R.id.btnUID);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onUIDClick(View v) {
textUID.setText(Integer.toString(getUID()));
}
public void onPIDClick(View v) {
textUID.setText(Integer.toString(getPID()));
}
public void onTimeClick(View v) {
textUID.setText(Long.toString(getTime()));
}
public void onKernelClick(View v) {
textUID.setText(getKernelInfo());
}
private native int getUID();
private native int getPID();
private native long getTime();
private native String getKernelInfo();
}