[英]How does dalvik findlibrary?
first, in System.java, it calls Runtime to loadLibrary. 首先,在System.java中,它将Runtime调用到loadLibrary。
public static void loadLibrary(String libName) {
SecurityManager smngr = System.getSecurityManager();
if (smngr != null) {
smngr.checkLink(libName);
}
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
}
second, it calles VMStack.getCallingClassLoader() to findLibrary. 第二,它将VMStack.getCallingClassLoader()调用到findLibrary。
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");
}
//....
}
}
So, i think VMStack.getCallingClassLoader()
is the most meaningful. 所以,我认为
VMStack.getCallingClassLoader()
是最有意义的。 But in its jni file dalvik_system_VMStack.cpp , the Dalvik_dalvik_system_VMStack_getCallingClassLoader
function is hard to learn. 但是在其jni文件dalvik_system_VMStack.cpp中 ,
Dalvik_dalvik_system_VMStack_getCallingClassLoader
函数很难学习。 And at last, how dalvik findLibrary? 最后,dalvik如何找到图书馆?
static void Dalvik_dalvik_system_VMStack_getCallingClassLoader(const u4* args,
JValue* pResult){
ClassObject* clazz =
dvmGetCaller2Class(dvmThreadSelf()->interpSave.curFrame);
UNUSED_PARAMETER(args);
if (clazz == NULL)
RETURN_PTR(NULL);
RETURN_PTR(clazz->classLoader);
}
VMStack.getCallingClassLoader()
returns the class loader of the class that declared the method that called the current method. VMStack.getCallingClassLoader()
返回声明调用当前方法的方法的类的类加载器。 In other words, if my function foo()
called Runtime.loadLibrary()
, it would return foo
's class loader. 换句话说,如果我的函数
foo()
调用Runtime.loadLibrary()
,它将返回foo
的类加载器。
The point of all that is to make sure the library is loaded in the context of whoever called findLibrary
, rather than in the context of java.lang.Runtime
. 所有这一切都是为了确保库是在调用
findLibrary
的人的上下文中加载的,而不是在java.lang.Runtime
的上下文中。
There's a good chance findLibrary()
is implemented by BaseDexClassLoader
, which calls into DexPathList.findLibrary()
, which does the actual work. findLibrary()
很有可能由BaseDexClassLoader
实现,它调用DexPathList.findLibrary()
来完成实际的工作。 The interesting bit there is the walk through nativeLibraryDirectories
, which is initialized from the libraryPath
argument to the BaseDexClassLoader
constructor (which gets it from PathClassLoader
or DexClassLoader
). 有趣的是,遍历
nativeLibraryDirectories
,它从libraryPath
参数初始化为BaseDexClassLoader
构造函数(从PathClassLoader
或DexClassLoader
)。
For Android apps, take a look at android.app.ApplicationLoaders
, which uses a PathClassLoader
. 对于Android应用程序,请查看
android.app.ApplicationLoaders
,它使用PathClassLoader
。 If you trace that back far enough you'll see the directory being retrieved from ApplicationInfo.nativeLibraryDir
. 如果您追溯到足够远,您将看到从
ApplicationInfo.nativeLibraryDir
中检索的目录。
EDIT: going deeper to address comment... 编辑:更深入地发表评论......
/system/lib
comes from the java.library.path
property, which the core libraries extract from the LD_LIBRARY_PATH
environment variable. /system/lib
来自java.library.path
属性,核心库从LD_LIBRARY_PATH
环境变量中提取该属性。 The app-specific library dir is configured by the framework. 特定于应用程序的库目录由框架配置。
The PackageManagerService
constructor sets the lib path in mAppLibInstallDir
, and setInternalAppNativeLibraryPath()
configures nativeLibraryDir
. PackageManagerService
构造函数在mAppLibInstallDir
设置lib路径, setInternalAppNativeLibraryPath()
配置nativeLibraryDir
。
DexPathList.splitLibraryPath()
combines the java.library.path
path with the APK-specific path. DexPathList.splitLibraryPath()
将java.library.path
路径与特定于APK的路径组合在一起。 See the comments there for notes on ordering. 有关订购的说明,请参阅那里的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.