在android中如何用Java加载解析so

 更新时间:2021年10月09日 11:20:45   作者:小道安全  
我们在android开发项目过程中都必然会更so加载打交道,那么so加载在系统中的顺序和流程是怎样的,我们就有必要对这个加载过程进行熟悉了解掌握

理论基础

so的加载是一种解析式装载,这与dex有一定区别,dex是先加载进行优化验证生成odex,再去解析odex文件,而so更像边解析边装载,在加载过程中主要解析是load段。
下面主要是以java层的so加载进行从源码上进行解析加载流程。

java层的so加载流程分析

 System.loadLibrary入口点

在java层我们知道加载so文件是通过System.loadLibrary函数其实现的,下面就以其作为入口点进行分析它的调用关系和实现。
System.loadLibrary在的函数定义系统source\libcore\luni\src\main\java\java\lang\system.java的文件中。

下面是其函数定义实现。

//参数就是要加载的so文件名称
 public static void loadLibrary(String libName) {
         //通过调用Runtime下面的loadLibrary函数实现
         //函数有两个参数,参数1是加载的so文件名,参数2 类加载器。
        Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
    }

Runtime的loadLibray解析

通过上面的System.java的loadLibrary函数我们需要继续分析Runtime.java文件中的loadLibray函数的定义实现。
Runtime的loadLibrary函数在android系统中的位置是
source\libcore\luni\src\main\java\java\lang\Runtime.java文件。

下面是Runtime的 loadLibrary函数的定义实现源码。

    /*
     * Searches for and loads the given shared library using the given ClassLoader.
     */
    void loadLibrary(String libraryName, ClassLoader loader) {
        if (loader != null) {
            //通过加载器去查找要加载的so文件名
            String filename = loader.findLibrary(libraryName);
            //查找失败
            if (filename == null) {
                // It's not necessarily true that the ClassLoader used
                // System.mapLibraryName, but the default setup does, and it's
                // misleading to say we didn't find "libMyLibrary.so" when we
                // actually searched for "liblibMyLibrary.so.so".
                throw new UnsatisfiedLinkError(loader + " couldn't find \"" +
                                               System.mapLibraryName(libraryName) + "\"");
            }
            //加载so文件名
            String error = doLoad(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 (IoUtils.canOpenReadOnly(candidate)) {
                String error = doLoad(candidate, loader);
                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的doLoad解析

通过上面的Runtime的loadLibrary函数,我们看到加载so的函数是走到doLoad函数,那么我们就需要继续分析Runtime下的doload函数的定义实现。
Rutime下的doload函数在系统中的
source\libcore\luni\src\main\java\java\lang\Runtime.java文件中。

下面的代码是Runtime的doload函数的定义实现。

 private String doLoad(String name, ClassLoader loader) {
        // Android apps are forked from the zygote, so they can't have a custom LD_LIBRARY_PATH,
        // which means that by default an app's shared library directory isn't on LD_LIBRARY_PATH.

        // The PathClassLoader set up by frameworks/base knows the appropriate path, so we can load
        // libraries with no dependencies just fine, but an app that has multiple libraries that
        // depend on each other needed to load them in most-dependent-first order.

        // We added API to Android's dynamic linker so we can update the library path used for
        // the currently-running process. We pull the desired path out of the ClassLoader here
        // and pass it to nativeLoad so that it can call the private dynamic linker API.

        // We didn't just change frameworks/base to update the LD_LIBRARY_PATH once at the
        // beginning because multiple apks can run in the same process and third party code can
        // use its own BaseDexClassLoader.

        // We didn't just add a dlopen_with_custom_LD_LIBRARY_PATH call because we wanted any
        // dlopen(3) calls made from a .so's JNI_OnLoad to work too.

        // So, find out what the native library search path is for the ClassLoader in question...
        String ldLibraryPath = null;
        if (loader != null && loader instanceof BaseDexClassLoader) {
            ldLibraryPath = ((BaseDexClassLoader) loader).getLdLibraryPath();
        }
        // nativeLoad should be synchronized so there's only one LD_LIBRARY_PATH in use regardless
        // of how many ClassLoaders are in the system, but dalvik doesn't support synchronized
        // internal natives.
        synchronized (this) {
            return nativeLoad(name, loader, ldLibraryPath);
        }
    }

总结

从以上的源码实现流程分析,我们可以看出Android在java层加载so的接口是System.loadLibrary(),通过层层递进关系从而实现java层的加载so。
下图是详细的java层加载so函数的调用关系。

在这里插入图片描述

到此这篇关于在android中如何用Java加载解析so的文章就介绍到这了,更多相关Android 加载so内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android倒计时神器(CountDownTimer)

    Android倒计时神器(CountDownTimer)

    这篇文章主要为大家详细介绍了Android倒计时神器CountDownTimer,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • Android Studio导入jar包过程详解

    Android Studio导入jar包过程详解

    这篇文章主要介绍了Android Studio导入jar包过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • Android自定义个性化的Dialog示例

    Android自定义个性化的Dialog示例

    这篇文章主要介绍了Android自定义个性化的Dialog,结合实例形式分析了自定义Dialog的功能、样式、布局等相关操作技巧,需要的朋友可以参考下
    2017-02-02
  • Android自定义View实现圆形切图效果

    Android自定义View实现圆形切图效果

    这篇文章主要为大家详细介绍了Android自定义View实现圆形切图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android开发apk反编译和二次打包教程

    Android开发apk反编译和二次打包教程

    反编译不是让各位开发者去对一个应用破解搞重装什么的,主要目的是为了促进开发者学习,借鉴好的代码,提升自我开发水平。下面我们就来研究下如何进行APK反编译以及二次打包
    2016-04-04
  • MT6589平台通话录音时播放提示音给对方功能的具体实现

    MT6589平台通话录音时播放提示音给对方功能的具体实现

    MT6589平台通话录音时如何播放提示音给对方,可以通过修改以下文件即可,希望对你有所帮助
    2013-06-06
  • 在Android界面上显示和获取Logcat日志输出的方法

    在Android界面上显示和获取Logcat日志输出的方法

    这篇文章主要介绍了在Android界面上显示和获取Logcat日志输出的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Android自定义控件仿iOS滑块SwitchButton

    Android自定义控件仿iOS滑块SwitchButton

    这篇文章主要为大家详细介绍了Android自定义控件模仿iOS滑块SwitchButton,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android 利用ImageView属性实现选中和未选中效果

    Android 利用ImageView属性实现选中和未选中效果

    这篇文章主要介绍了Android巧用ImageView属性实现选中和未选中效果,实现思路通常我们会选择在布局里加个ImageView,然后通过代码层面加个判断去让ImageView加载不同状态的图片,需要的朋友可以参考下
    2023-06-06
  • Notification自定义界面

    Notification自定义界面

    这篇文章主要为大家详细介绍了Notification自定义界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11

最新评论