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");

            }

            //如果查找的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*,
......... 
原创粉丝点击