深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
}
我们看Runtime的loadLibrary0方法里面怎么处理的:
private synchronized void loadLibrary0(ClassLoader loader, Class<?> callerClass, String libname) {
if (libname.indexOf((int)File.separatorChar) != -1) {
throw new UnsatisfiedLinkError(
"Directory separator should not appear in library name: " + libname);
}
String libraryName = libname;
if (loader != null && !(loader instanceof BootClassLoader)) {
String filename = loader.findLibrary(libraryName);
if (filename == null) {
throw new UnsatisfiedLinkError(loader + " couldn't find \"" +
System.mapLibraryName(libraryName) + "\"");
}
String error = nativeLoad(filename, loader);
if (error != null) {
throw new UnsatisfiedLinkError(error);
}
return;
}
getLibPaths();
String filename = System.mapLibraryName(libraryName);
String error = nativeLoad(filename, loader, callerClass);
if (error != null) {
throw new UnsatisfiedLinkError(error);
}
}
第一步会通过ClassLoader通过我们之前传入的so名称找到文件名,当fileNeme为null时,就会报错找不到so文件,
ClassLoader最终会进去BaseDexClassLoader的findLibrary方法里面:
@Override
public String findLibrary(String name) {
return pathList.findLibrary(name);
}
继续查看pathList(DexPathList )的findLibrary方法:
public String findLibrary(String libraryName) {
// 通过 so 的名称拼接成文件路径
String fileName = System.mapLibraryName(libraryName);
for (NativeLibraryElement element : nativeLibraryPathElements) {
String path = element.findNativeLibrary(fileName);
if (path != null) {
return path;
}
}
return null;
}
System.mapLibraryName("main); 通过so的名称拼接成文件路径返回的是libmain.so,nativeLibraryPathElements 是 native library 路径的集合, 它的是 DexPathList 初始化的时候赋值
public String findNativeLibrary(String name) {
maybeInit();
if (zipDir == null) {
String entryPath = new File(path, name).getPath();
if (IoUtils.canOpenReadOnly(entryPath)) {
return entryPath;
}
} else if (urlHandler != null) {
// Having a urlHandler means the element has a zip file.
// In this case Android supports loading the library iff
// it is stored in the zip uncompressed.
String entryName = zipDir + '/' + name;
if (urlHandler.isEntryStored(entryName)) {
return path.getPath() + zipSeparator + entryName;
}
}
return null;
}
我们看一下DexPathList的构造函数:
DexPathList(ClassLoader definingContext, String dexPath,
String librarySearchPath, File optimizedDirectory, boolean isTrusted) {
...
this.definingContext = definingContext;
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
// save dexPath for BaseDexClassLoader
this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory,
suppressedExceptions, definingContext, isTrusted);
this.nativeLibraryDirectories = splitPaths(librarySearchPath, false);
this.systemNativeLibraryDirectories =
splitPaths(System.getProperty("java.library.path"), true);
List<File> allNativeLibraryDirectories = new ArrayList<>(nativeLibraryDirectories);
allNativeLibraryDirectories.addAll(systemNativeLibraryDirectories);
this.nativeLibraryPathElements = makePathElements(allNativeLibraryDirectories);
if (suppressedExceptions.size() > 0) {
this.dexElementsSuppressedExceptions =
suppressedExceptions.toArray(new IOException[suppressedExceptions.size()]);
} else {
dexElementsSuppressedExceptions = null;
}
...
}
在 DexPathList 的构造函数中,我们可以知道 nativeLibraryPathElements 是所有 Native Library 的集合。
DexPathList 是在 ActivityThread 中创建,ActivityThread 是在 App 启动时候创建的。关于 App 启动的启动流程,可以去找这方面的资料,自行查看。
总结一些 Native Library 的路径来源:
一个是 Native 库的原始路径 System.getProperty(“java.library.path\*\*\*\*”), /system/lib/; /vendor/lib/; /product/lib/
另外一个是App启动时的 Lib 库路径
![](https://img-blog.csdnimg.cn/img_convert/8ae96e4e87d6c85a990267361c4bdf78.png)
## 二、动态加载so库
根据上面Java层so加载流程我们可以知道,在System.loadLibrary(so名称)后把我们的so路径存储到了ClassLoader的pathList的nativeLibraryPathElements里面,我们知道咋热修复的知识里面是把解决bug的dex文件插入到PathList的Elements的前面,这样就可以保证我们ClassLoader findClass时会加载正确的class,那么我们得so是不是也可以这样做呢,把我们动态下载的so文件插入到nativeLibraryPathElements数组的前面。
### 1.将so文件复制到data目录下
Log.d(“Leon”, getExternalCacheDir().getAbsolutePath());
// 先调用 sdk 方法动态加载或者修复
File mainSoPath = new File(Environment.getExternalStorageDirectory(),“so/libmain.so”);
File libSoPath = new File(getDir("lib",Context.MODE_PRIVATE),"so");
if(!libSoPath.exists()){
libSoPath.mkdirs();
}
File dst = new File(libSoPath,"libmain.so");
try {
FileUtil.copyFile(mainSoPath,dst);
} catch (IOException e) {
e.printStackTrace();
}
**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
![](https://i-blog.csdnimg.cn/blog_migrate/1e210328c1ebf43f08d3b6c5f5c13b48.png)
![img](https://img-blog.csdnimg.cn/img_convert/0f7e96f1519d00bad2a69bc8a4abbde4.png)
![img](https://img-blog.csdnimg.cn/img_convert/78d9a8c3bd38b8f54454611a6395815f.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**
零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**