Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(), libname);
}
我们看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();
}
/**
*
* copy file
*
* @param src
* source file
* @param dest
* target file
* @throws IOException
*/
public static void copyFile(File src, File dest) throws IOException {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
if (!dest.exists()) {
dest.createNewFile();
}
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(dest).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
码讲义、实战项目、讲解视频,并且会持续更新**
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
[外链图片转存中…(img-yHsQQ2OL-1712860723935)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!