Android 远程调试 JNI 实现
2010-04-05 18:27:00 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾剧懓顪冪€n亜顒㈡い鎰Г閹便劌顫滈崱妤€骞婄紓鍌氬€瑰銊╁箟缁嬫鍚嬮柛顐線缂冩洟姊婚崒娆戭槮婵犫偓闁秵鎯為幖娣妼缁愭鏌″搴′簽濞戞挸绉甸妵鍕冀椤愵澀娌梺缁樻尪閸庣敻寮婚敐澶婂嵆闁绘劖绁撮崑鎾诲捶椤撴稑浜炬慨妯煎亾鐎氾拷

file="/system/framework/com.me.test.jar" />
使得jar包能够被应用程序link到.
2. 接着我们可以添加C++ 代码,来具体实现 Step1 中 定义的 JNI interface 。
2.1 添加 frameworks/base/core/jni/TestInternalApi.cpp 到Android 代码树,在这个C++类中可以调用底层的 C++/C函数控制硬件等。
其中 JNI_OnLoad()函数在.so被load的时候调用, test_TestInterAPI_Func() 函数则是Android 上层JAVA应用调用JNI apiFunction() 的时候具体运行的代码.
#define LOG_TAG "itest_jni"
#include
#include
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include "scm_dbus_utils.h"
#define INTERNALAPI_PKG_NAME "com/me/test/TestNativeApi"
using namespace android;
static jint test_TestInterAPI_Func(JNIEnv* env, jobject clazz)
{
jint ret = (jint)0;
LOGD("call \"%s\"", __FUNCTION__);
return ret;
}
/*
* JNI registration.
*/
static JNINativeMethod gTestInterApiMethods[] = {
{ "apiFunction", "()I", (void *)test_TestInterAPI_Func }
};
int register_com_me_test_TestInternalApiNative(JNIEnv *env)
{
return AndroidRuntime::registerNativeMethods(env, INTERNALAPI_PKG_NAME, gTestInterApiMethods, NELEM(gTestInterApiMethods));
}
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
LOGD("TestInteralApi JNI loaded");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("GetEnv failed");
goto bail;
}
assert(env != NULL);
if (register_com_me_test_TestInternalApiNative(env) < 0) {
LOGE("TestInternalApi native registration failed");
更多精彩
赞助商链接