android深入JNI遇到的问题?
来源:互联网 发布:中英互译软件 编辑:程序博客网 时间:2024/05/02 00:50
=========== 问题1: nativeLoad的实现?
------------- System.loadLibrary ----------- // ./libcore/luni/src/main/java/java/lang/System.java
/**
* Loads and links the library with the specified name. The mapping of the
* specified library name to the full path for loading the library is
* implementation-dependent.
*
* @param libName
* the name of the library to load.
* @throws UnsatisfiedLinkError
* if the library could not be loaded.
*/
public static void loadLibrary(String libName) {
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());//传递参数classLoader
}
------------- Runtime.getRuntime ------------ // ./libcore/luni/src/main/java/java/lang/Runtime.java
/**
* Returns the single {@code Runtime} instance.
*
* @return the {@code Runtime} object for the current application.
*/
public static Runtime getRuntime() {
return mRuntime;
}
/**
* Holds the Singleton global instance of Runtime.
*/
private static final Runtime mRuntime = new Runtime();//常量
---------------- Runtime.loadLibrary -------
/**
* Loads and links the library with the specified name. The mapping of the
* specified library name to the full path for loading the library is
* implementation-dependent.
*
* @param libName
* the name of the library to load.
* @throws UnsatisfiedLinkError
* if the library can not be loaded.
*/
public void loadLibrary(String libName) {
loadLibrary(libName, VMStack.getCallingClassLoader());
}
/*
* Loads and links a library without security checks.
*/
void loadLibrary(String libraryName, ClassLoader loader) {
if (loader != null) {
String filename = loader.findLibrary(libraryName);
if (filename == null) {
throw new UnsatisfiedLinkError("Couldn't load " + libraryName + ": " +
"findLibrary returned null");
if (error != null) {
throw new UnsatisfiedLinkError(error);
}
return;
}
String filename = System.mapLibraryName(libraryName);
List<String> candidates = new ArrayList<String>();
String lastError = null;
for (String directory : mLibPaths) {
String candidate = directory + filename;
candidates.add(candidate);
if (new File(candidate).exists()) {
String error = nativeLoad(candidate, loader);//如果查找的lib不在类路径中,从libPath中查找 nativeLoad是关键
if (error == null) {
return; // We successfully loaded the library. Job done.
}
lastError = error;
}
}
if (lastError != null) {
throw new UnsatisfiedLinkError(lastError);
}
throw new UnsatisfiedLinkError("Library " + libraryName + " not found; tried " + candidates);
}
-------------- Runtime.nativeLoad ------- 暂时没有找到nativeLoad的c/c++实现 ????
private static native String nativeLoad(String filename, ClassLoader loader);
=========== 问题2: JNI native调用java函数时的具体实现?
----------- Jni.h ----------- 如下所见: JNIEnv实际是一个_JNIEnv类型的结构体的typedef,JavaVM实际是_JavaVM结构体
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
-----------
---------- Jni.h中定义了_JNIEnv这个结构体 ----
/*
* C++ object wrapper.
*
* This is usually overlaid on a C struct whose first element is a
* JNINativeInterface*. We rely somewhat on compiler behavior.
*/
struct _JNIEnv {
/* do not rename this; it does not seem to be entirely opaque */
const struct JNINativeInterface* functions; //这个结构体很重要
#if defined(__cplusplus)
jint GetVersion()
{ return functions->GetVersion(this); }
jclass DefineClass(const char *name, jobject loader, const jbyte* buf,
jsize bufLen)
{ return functions->DefineClass(this, name, loader, buf, bufLen); }
.............
---------- JNINativeInterface结构体,其中定义了许多函数指针,暂没找到这些函数的实现.???
/*
* Table of interface function pointers.
*/
struct JNINativeInterface {
void* reserved0;
void* reserved1;
void* reserved2;
void* reserved3;
jint (*GetVersion)(JNIEnv *);
jclass (*DefineClass)(JNIEnv*, const char*, jobject, const jbyte*,
.........
------------- System.loadLibrary ----------- // ./libcore/luni/src/main/java/java/lang/System.java
/**
* Loads and links the library with the specified name. The mapping of the
* specified library name to the full path for loading the library is
* implementation-dependent.
*
* @param libName
* the name of the library to load.
* @throws UnsatisfiedLinkError
* if the library could not be loaded.
*/
public static void loadLibrary(String libName) {
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());//传递参数classLoader
}
------------- Runtime.getRuntime ------------ // ./libcore/luni/src/main/java/java/lang/Runtime.java
/**
* Returns the single {@code Runtime} instance.
*
* @return the {@code Runtime} object for the current application.
*/
public static Runtime getRuntime() {
return mRuntime;
}
/**
* Holds the Singleton global instance of Runtime.
*/
private static final Runtime mRuntime = new Runtime();//常量
---------------- Runtime.loadLibrary -------
/**
* Loads and links the library with the specified name. The mapping of the
* specified library name to the full path for loading the library is
* implementation-dependent.
*
* @param libName
* the name of the library to load.
* @throws UnsatisfiedLinkError
* if the library can not be loaded.
*/
public void loadLibrary(String libName) {
loadLibrary(libName, VMStack.getCallingClassLoader());
}
/*
* Loads and links a library without security checks.
*/
void loadLibrary(String libraryName, ClassLoader loader) {
if (loader != null) {
String filename = loader.findLibrary(libraryName);
if (filename == null) {
throw new UnsatisfiedLinkError("Couldn't load " + libraryName + ": " +
"findLibrary returned null");
}
//如果查找的lib在类路径中(即loader!=null 并且 findLibrary返回的不是空) nativeLoad是关键
String error = nativeLoad(filename, loader);if (error != null) {
throw new UnsatisfiedLinkError(error);
}
return;
}
String filename = System.mapLibraryName(libraryName);
List<String> candidates = new ArrayList<String>();
String lastError = null;
for (String directory : mLibPaths) {
String candidate = directory + filename;
candidates.add(candidate);
if (new File(candidate).exists()) {
String error = nativeLoad(candidate, loader);//如果查找的lib不在类路径中,从libPath中查找 nativeLoad是关键
if (error == null) {
return; // We successfully loaded the library. Job done.
}
lastError = error;
}
}
if (lastError != null) {
throw new UnsatisfiedLinkError(lastError);
}
throw new UnsatisfiedLinkError("Library " + libraryName + " not found; tried " + candidates);
}
-------------- Runtime.nativeLoad ------- 暂时没有找到nativeLoad的c/c++实现 ????
private static native String nativeLoad(String filename, ClassLoader loader);
=========== 问题2: JNI native调用java函数时的具体实现?
----------- Jni.h ----------- 如下所见: JNIEnv实际是一个_JNIEnv类型的结构体的typedef,JavaVM实际是_JavaVM结构体
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
-----------
---------- Jni.h中定义了_JNIEnv这个结构体 ----
/*
* C++ object wrapper.
*
* This is usually overlaid on a C struct whose first element is a
* JNINativeInterface*. We rely somewhat on compiler behavior.
*/
struct _JNIEnv {
/* do not rename this; it does not seem to be entirely opaque */
const struct JNINativeInterface* functions; //这个结构体很重要
#if defined(__cplusplus)
jint GetVersion()
{ return functions->GetVersion(this); }
jclass DefineClass(const char *name, jobject loader, const jbyte* buf,
jsize bufLen)
{ return functions->DefineClass(this, name, loader, buf, bufLen); }
.............
---------- JNINativeInterface结构体,其中定义了许多函数指针,暂没找到这些函数的实现.???
/*
* Table of interface function pointers.
*/
struct JNINativeInterface {
void* reserved0;
void* reserved1;
void* reserved2;
void* reserved3;
jint (*GetVersion)(JNIEnv *);
jclass (*DefineClass)(JNIEnv*, const char*, jobject, const jbyte*,
.........
- android深入JNI遇到的问题?
- Android Studio JNI javah遇到的问题
- android+eclipse+jni遇到的一些问题
- Android JNI 自己遇到的问题记录
- JNI 遇到的问题
- Android中JNI遇到问题
- android中导入hello-jni文件时遇到的问题
- android配置jni过程可能会遇到的问题
- android JNI 配置中遇到的一些问题
- 编写android jni代码时遇到的问题
- Android 导入项目时遇到的JNI和NDK问题
- Android源码编译JNI遇到的问题与解决方法
- android使用CMake进行jni编写遇到的一些问题
- Android开发jni遇到的一些问题,持续更新
- jni学习遇到的问题
- JNI调用遇到的问题
- jni遇到的问题总结
- android ndk hello-jni 遇到问题
- 刘强东 简历
- 推荐:恢复Ext3下被删除的文件
- CollabNet Teamforge 父项目与子项目
- 厚积薄发,丰富的公用类库积累
- 两款主流摄像头OV7620与OV7670 By Demok
- android深入JNI遇到的问题?
- ORACLE 抓取AWR报告
- 单线程 与 多线程 的执行
- ORACLE 测试数据文件丢失 与 恢复
- Lucene 优化
- JAVASE基础部分一------java语言、JRE、JDK、环境变量、Hello world
- C++构造函数
- 女生每月生理期,是升级防火墙吗?
- HTTP状态码大全