Android Java code startup: app_process

Source: Internet
Author: User

 

On Android, If you directly use dalivik to load framework. jar, you will find that most of the native methods in it are not available. At the same time, you cannot find dalvikvm in/init. RC. There is no dalvikvm in the ps list. So how does Android start Java programs?

On Android, Java programs are started through app_process. In/init. RC, there is the following code:

Service zygote/system/bin/app_process-xzygote/system/bin -- zygote -- start-system-Server

Socket zygote stream 666

Onrestart write/sys/android_power/request_state wake

Onrestart write/sys/power/State on

The command line parameters of app_process are as follows:

App_process [Java-options] cmd-Dir start-class-name [Options]

Therefore, the correspondence between parameters in init. RC is as follows:

-Xzygote: Java-Options. These parameters are passed to Dalvik. These parameters must start with-. If a parameter is not prefixed with-or --, it indicates that Java-options ends.

/System/bin: cmd-Dir, that is, the current directory. The parent path of the file operation will be this path.

Start-class-Name: NULL

-- Zygote -- start-system-server: option.

If (I <argc) {<br/> Arg = argv [I ++]; <br/> If (0 = strcmp ("-- zygote", ARG )) {<br/> bool startsystemserver = (I <argc )? <Br/> strcmp (argv [I], "-- start-system-server") = 0: false; <br/> setargv0 (argv0, "zygote "); <br/> set_process_name ("zygote"); <br/> runtime. start ("com. android. internal. OS. zygoteinit ", <br/> startsystemserver); <br/>}else {<br/> set_process_name (argv0); <br/> runtime. mclassname = ARG; <br/> // remainder of argS get passed to startup class main () <br/> runtime. margc = argc-I; <br/> runtime. margv = argv + I; <br/> logv ("app process is starting with PID = % d, class = % S. /n ", <br/> getpid (), runtime. getclassname (); <br/> runtime. start (); <br/>}< br/>} 

From the main function of app_process (in app_main.cpp), app_process has two startup Methods: init. this method in RC will start COM. android. internal. OS. zygoteinit, and change the process name to zygote. Another method is to simulate the startup of COM. android. internal. OS. runtimeinit and call its main method. finishinit will be executed at the end of Main. finishinit is a native method. This method will call the onstarted method of app_process, the class to be executed will be called in onstarted.

Regardless of app_process, it will call start in frameworks/base/CORE/JNI/androidruntime. cpp.

Method (496 rows ). This start method sets Dalvik Parameters Based on the android property system, initializes the native method of Java code, and finally starts Dalvik. Note that most native methods (about 80%) of Java code are initialized here. These initialization codes check the correctness of Java code (for example, whether there is a corresponding native method, in the initialization process, any step of error will cause the process to exit. The entire native method initialization process is as follows: Start (androidruntime. cpp, row 766)-> starregulatory (androidruntime. cpp, row 1136)-> register_jni_procs (androidruntime. cpp, row 1011 ). The initialized Java classes are: (androidruntime. cpp, row 1018 ):

Static const regjnirec gregjni [] ={< br/> reg_jni (response), <br/> reg_jni (response), <br/> reg_jni (register_android_ OS _systemclock ), <br/> reg_jni (register_android_util_eventlog), <br/> reg_jni (register_android_util_log), <br/> reg_jni (region), <br/> reg_jni (region ), <br/> reg_jni (region), <br/> reg_jni (register_android_content_xmlblock ), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_text_androidcharacter), <br/> reg_jni (region ), <br/> reg_jni (register_android_ OS _process), <br/> reg_jni (scheme), <br/> reg_jni (scheme), <br/> reg_jni (register_android_view_display ), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_graphics), <br/> reg_jni (register_android_view_surface ), <br/> reg_jni (register_android_view_viewroot), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_bitmap ), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_canvas), <br/> reg_jni (register_android_graphics_colorfilter ), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_interpolator), <br/> reg_jni (region), <br/> reg_jni (region ), <br/> reg_jni (register_android_graphics_matrix), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_paint ), <br/> reg_jni (register_android_graphics_path), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_picture ), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_graphics_region), <br/> reg_jni (register_android_graphics_shader ), <br/> reg_jni (register_android_graphics_typeface), <br/> reg_jni (response ), <br/> reg_jni (register_android_database_sqlitedatabase), <br/> reg_jni (region ), <br/> reg_jni (region), <br/> reg_jni (register_android_ OS _debug), <br/> reg_jni (register_android_ OS _exec), <br/> reg_jni (region ), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_ OS _power), <br/> reg_jni (register_android_ OS _statfs ), <br/> reg_jni (register_android_ OS _systemproperties), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_net_networkutils ), <br/> reg_jni (region), <br/> reg_jni (register_android_ OS _memoryfile), <br/> reg_jni (region), <br/> reg_jni (region ), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_media_audiosystem), <br/> reg_jni (register_android_media_audiotrack ), <br/> reg_jni (register_android_media_jetplayer), <br/> reg_jni (region), <br/> reg_jni (region), <br/> reg_jni (register_android_1_th_database ), <br/> reg_jni (response), <br/> reg_jni (register_android_1_th_scosocket ), <br/> reg_jni (region), <br/> reg_jni (register_android_server_bluetootheventloop), <br/> reg_jni (region), <br/> reg_jni (region ), <br/> reg_jni (region), <br/> reg_jni (register_android_util_base64), <br/> reg_jni (register_android_location_gpslocationprovider), <br/> }; 

These initialized native methods are interfaces for Java code to deal with underlying services, so we must initialize these methods.

Another advantage of starting Java class with app_process is that it will initialize ibinder, so that ibinder messages can be received in Java code and JNI code.

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.