The process of Android loading so

Source: Internet
Author: User

SOURCE version: Android-4.4.4_r1

Take [java.lang.Runtime-load ()] as an example to illustrate (Loadliabrary () Finally and load () the same, interested can self-analysis), the corresponding Android source code in the [srcandroid/libcore/ luni/src/main/java/java/lang/runtime.java/home/shanyu/srcandroid/libcore/luni/src/main/java/java/lang/ Runtime.java],

Start with 320 rows.

    /** * Loads and links The dynamic library is identified through the * specified path. This method was similar to {@link #loadLibrary (String)}, * But it accepts a full path specification whereas {@code load     Library} just * accepts the name of the library to load.     * * @param pathName * The absolute (platform dependent) path to the library to load.     * @throws Unsatisfiedlinkerror * If the library can not be loaded. */     Public voidload (String pathName) {load (PathName, Vmstack.getcallingclassloader ()); }    /** Loads and links the given library without security checks. */    voidLoad (String pathName, ClassLoader loader) {if(PathName = =NULL) {            Throw NewNullPointerException ("PathName = = NULL"); } String Error=doload (pathName, loader); if(Error! =NULL) {            Throw NewUnsatisfiedlinkerror (Error); }    }

Finally, the Doload (String name, ClassLoader loader) function is called, and the function is still in the Runtime.java file:

    Privatestring Doload (string name, ClassLoader loader) {//Android Apps is forked from the zygote, so they can ' t has a custom ld_library_path,//which means that by the default an app's shared library directory is not 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 a app that have multiple libraries that//depend on the needed to the 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 A ClassLoader here//and Pass it to nativeload so, 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 the 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 was for the ClassLoader in question ...String Ldlibrarypath =NULL; if(Loader! =NULL&&Loader instanceof Basedexclassloader) {Ldlibrarypath=((basedexclassloader) loader). Getldlibrarypath (); }        //Nativeload should is synchronized so there ' s only one ld_library_path in use regardless//of how many classloaders is in the system, but Dalvik doesn ' t support synchronized//internal natives.Synchronized ( This) {            returnnativeload (name, loader, ldlibrarypath); }    }    //Todo:should is synchronized, but Dalvik doesn ' t support synchronized internal natives.    Private StaticNative string Nativeload (string filename, ClassLoader loader, string ldlibrarypath);

The final call to the "string nativeload (string filename, ClassLoader loader, string ldlibrarypath)" function, a native function, is defined in [Srcandroid/ Dalvik/vm/native/java_lang_runtime.cpp] file. Start with 64 lines:

/** Static string Nativeload (string filename, ClassLoader loader, string ldlibrarypath) * Load the specified full pat h as a dynamic library filled with * jni-compatible methods. Returns null on success, or a failure * message on failure. */Static voidDalvik_java_lang_runtime_nativeload (Constu4*args, Jvalue*PResult) {Stringobject* Filenameobj = (stringobject*) args[0]; Object* ClassLoader = (object*) args[1]; Stringobject* Ldlibrarypathobj = (stringobject*) args[2]; ASSERT (Filenameobj!=NULL); Char* FileName =dvmcreatecstrfromstring (filenameobj); if(Ldlibrarypathobj! =NULL) {        Char* Ldlibrarypath =dvmcreatecstrfromstring (ldlibrarypathobj); void* sym = Dlsym (Rtld_default,"Android_update_ld_library_path"); if(Sym! =NULL) {typedefvoid(*FN) (Const Char*); Fn Android_update_ld_library_path= reinterpret_cast<fn>(SYM); (*Android_update_ld_library_path)        (Ldlibrarypath); } Else{Aloge ("Android_update_ld_library_path not found; Dependencies would not work!"); }         Free(Ldlibrarypath); } stringobject* result =NULL; Char* Reason =NULL; BOOLSuccess = Dvmloadnativecode (FileName, ClassLoader, &reason); if(!success) {        Const Char* msg = (reason! = NULL)? Reason:"Unknown Failure"; Result=dvmcreatestringfromcstr (msg); Dvmreleasetrackedalloc ((Object*) result, NULL); }     Free(reason);  Free(FileName); Return_ptr (result);}

or pass value + check, then execute [bool success = Dvmloadnativecode (FileName, ClassLoader, &reason);], look at the code for Dvmloadnativecode (...), located at V M/native.cpp # 301 lines.

The process of Android loading so

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.