android java代码的启动:app_process

在android上,如果你直接用dalivik去加载framework.jar,你会发现里面的大部分native方法无法使用。同时,在 /init.rc里面,你也找不到dalvikvm。在ps列表,也没有dalvikvm。那android是怎么启动java程序的呢?

在android上,java程序是通过app_process启动的。在/init.rc里面,有如下一段代码:

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

app_process的命令行参数如下:

app_process [java-options] cmd-dir start-class-name [options]

因此,init.rc里面的各个参数的对应关系为:

-Xzygote: java-options,这些参数会传给dalvik,这些参数必须以-开头,一旦遇到不是以-开头的或者--,代表java-options结束。

/system/bin:cmd-dir,也就是当前目录,文件操作的父路径将为此路径。

start-class-name:空

--zygote --start-system-server:选项。

  1. if  (i < argc) {  
  2. arg = argv[i++];  
  3. if  (0 == strcmp( "--zygote" , arg)) {  
  4. bool  startSystemServer = (i < argc) ?  
  5. strcmp(argv[i], "--start-system-server" ) == 0 :  false ;  
  6. setArgv0(argv0, "zygote" );  
  7. set_process_name("zygote" );  
  8. runtime.start("com.android.internal.os.ZygoteInit" ,  
  9. startSystemServer);  
  10. else  {  
  11. set_process_name(argv0);  
  12. runtime.mClassName = arg;  
  13. // Remainder of args get passed to startup class main()   
  14. runtime.mArgC = argc-i;  
  15. runtime.mArgV = argv+i;  
  16. LOGV("App process is starting with pid=%d, class=%s./n" ,  
  17. getpid(), runtime.getClassName());  
  18. runtime.start();  
  19. }  
  20. }  

 

  1. if (i < argc) {   
  2. arg = argv[i++];   
  3. if (0 == strcmp("--zygote", arg)) {   
  4. bool startSystemServer = (i < argc) ?   
  5. strcmp(argv[i], "--start-system-server") == 0 : false;   
  6. setArgv0(argv0, "zygote");   
  7. set_process_name("zygote");   
  8. runtime.start("com.android.internal.os.ZygoteInit",   
  9. startSystemServer);   
  10. else {   
  11. set_process_name(argv0);   
  12. runtime.mClassName = arg;   
  13. // Remainder of args get passed to startup class main()   
  14. runtime.mArgC = argc-i;   
  15. runtime.mArgV = argv+i;   
  16. LOGV("App process is starting with pid=%d, class=%s./n",   
  17. getpid(), runtime.getClassName());   
  18. runtime.start();   
  19. }   
  20. }  

 

 

从app_process的main函数(在app_main.cpp里面)可以看出,app_process有两种启动方式:一种是 init.rc里面的这种方式,这种方式将会以zygote模式启动com.android.internal.os.ZygoteInit,并将进程名称改为zygote;另外一种是以非zygote模拟启动com.android.internal.os.RuntimeInit,并调用它的main 方法,main的最后会执行finishInit,finishInit是一个native方法,这个方法会调用app_process的 onStarted方法,在onStarted里面将会调用真正要执行的class。

无论app_process,它都将会调用frameworks/base/core/jni/AndroidRuntime.cpp里面的start

方法(496行)。这个start方法会根据android属性系统设置dalvik的参数,并初始化java代码的native方法,最终启动dalvik。需要注意的是,java代码的大部分native方法(差不多80%)都是在这里面初始化的,这些初始化代码会检查java代码的正确性(例如是否有对应的 native方法,是否有需要的属性等),在初始化过程中,任何一步的错误都将导致进程退出。整个native方法初始化过程如下:start(AndroidRuntime.cpp,766行)->startReg(AndroidRuntime.cpp,1136 行)-> register_jni_procs(AndroidRuntime.cpp,1011行)。初始化的java class有(AndroidRuntime.cpp,1018行):

  1. static   const  RegJNIRec gRegJNI[] = {  
  2. REG_JNI(register_android_debug_JNITest),  
  3. REG_JNI(register_com_android_internal_os_RuntimeInit),  
  4. REG_JNI(register_android_os_SystemClock),  
  5. REG_JNI(register_android_util_EventLog),  
  6. REG_JNI(register_android_util_Log),  
  7. REG_JNI(register_android_util_FloatMath),  
  8. REG_JNI(register_android_text_format_Time),  
  9. REG_JNI(register_android_pim_EventRecurrence),  
  10. REG_JNI(register_android_content_AssetManager),  
  11. REG_JNI(register_android_content_StringBlock),  
  12. REG_JNI(register_android_content_XmlBlock),  
  13. REG_JNI(register_android_emoji_EmojiFactory),  
  14. REG_JNI(register_android_security_Md5MessageDigest),  
  15. REG_JNI(register_android_text_AndroidCharacter),  
  16. REG_JNI(register_android_text_KeyCharacterMap),  
  17. REG_JNI(register_android_os_Process),  
  18. REG_JNI(register_android_os_Binder),  
  19. REG_JNI(register_android_os_Hardware),  
  20. REG_JNI(register_android_view_Display),  
  21. REG_JNI(register_android_nio_utils),  
  22. REG_JNI(register_android_graphics_PixelFormat),  
  23. REG_JNI(register_android_graphics_Graphics),  
  24. REG_JNI(register_android_view_Surface),  
  25. REG_JNI(register_android_view_ViewRoot),  
  26. REG_JNI(register_com_google_android_gles_jni_EGLImpl),  
  27. REG_JNI(register_com_google_android_gles_jni_GLImpl),  
  28. REG_JNI(register_android_graphics_Bitmap),  
  29. REG_JNI(register_android_graphics_BitmapFactory),  
  30. REG_JNI(register_android_graphics_Camera),  
  31. REG_JNI(register_android_graphics_Canvas),  
  32. REG_JNI(register_android_graphics_ColorFilter),  
  33. REG_JNI(register_android_graphics_DrawFilter),  
  34. REG_JNI(register_android_graphics_Interpolator),  
  35. REG_JNI(register_android_graphics_LayerRasterizer),  
  36. REG_JNI(register_android_graphics_MaskFilter),  
  37. REG_JNI(register_android_graphics_Matrix),  
  38. REG_JNI(register_android_graphics_Movie),  
  39. REG_JNI(register_android_graphics_NinePatch),  
  40. REG_JNI(register_android_graphics_Paint),  
  41. REG_JNI(register_android_graphics_Path),  
  42. REG_JNI(register_android_graphics_PathMeasure),  
  43. REG_JNI(register_android_graphics_PathEffect),  
  44. REG_JNI(register_android_graphics_Picture),  
  45. REG_JNI(register_android_graphics_PorterDuff),  
  46. REG_JNI(register_android_graphics_Rasterizer),  
  47. REG_JNI(register_android_graphics_Region),  
  48. REG_JNI(register_android_graphics_Shader),  
  49. REG_JNI(register_android_graphics_Typeface),  
  50. REG_JNI(register_android_graphics_Xfermode),  
  51. REG_JNI(register_com_android_internal_graphics_NativeUtils),  
  52. REG_JNI(register_android_database_CursorWindow),  
  53. REG_JNI(register_android_database_SQLiteDatabase),  
  54. REG_JNI(register_android_database_SQLiteDebug),  
  55. REG_JNI(register_android_database_SQLiteProgram),  
  56. REG_JNI(register_android_database_SQLiteQuery),  
  57. REG_JNI(register_android_database_SQLiteStatement),  
  58. REG_JNI(register_android_os_Debug),  
  59. REG_JNI(register_android_os_Exec),  
  60. REG_JNI(register_android_os_FileObserver),  
  61. REG_JNI(register_android_os_FileUtils),  
  62. REG_JNI(register_android_os_ParcelFileDescriptor),  
  63. REG_JNI(register_android_os_Power),  
  64. REG_JNI(register_android_os_StatFs),  
  65. REG_JNI(register_android_os_SystemProperties),  
  66. REG_JNI(register_android_os_UEventObserver),  
  67. REG_JNI(register_android_net_LocalSocketImpl),  
  68. REG_JNI(register_android_net_NetworkUtils),  
  69. REG_JNI(register_android_net_wifi_WifiManager),  
  70. REG_JNI(register_android_os_MemoryFile),  
  71. REG_JNI(register_com_android_internal_os_ZygoteInit),  
  72. REG_JNI(register_android_hardware_Camera),  
  73. REG_JNI(register_android_hardware_SensorManager),  
  74. REG_JNI(register_android_media_AudioRecord),  
  75. REG_JNI(register_android_media_AudioSystem),  
  76. REG_JNI(register_android_media_AudioTrack),  
  77. REG_JNI(register_android_media_JetPlayer),  
  78. REG_JNI(register_android_media_ToneGenerator),  
  79. REG_JNI(register_android_opengl_classes),  
  80. REG_JNI(register_android_bluetooth_Database),  
  81. REG_JNI(register_android_bluetooth_HeadsetBase),  
  82. REG_JNI(register_android_bluetooth_BluetoothAudioGateway),  
  83. REG_JNI(register_android_bluetooth_RfcommSocket),  
  84. REG_JNI(register_android_bluetooth_ScoSocket),  
  85. REG_JNI(register_android_server_BluetoothDeviceService),  
  86. REG_JNI(register_android_server_BluetoothEventLoop),  
  87. REG_JNI(register_android_server_BluetoothA2dpService),  
  88. REG_JNI(register_android_message_digest_sha1),  
  89. REG_JNI(register_android_ddm_DdmHandleNativeHeap),  
  90. REG_JNI(register_android_util_Base64),  
  91. REG_JNI(register_android_location_GpsLocationProvider),  
  92. };  

 

  1. static const RegJNIRec gRegJNI[] = {   
  2. REG_JNI(register_android_debug_JNITest),   
  3. REG_JNI(register_com_android_internal_os_RuntimeInit),   
  4. REG_JNI(register_android_os_SystemClock),   
  5. REG_JNI(register_android_util_EventLog),   
  6. REG_JNI(register_android_util_Log),   
  7. REG_JNI(register_android_util_FloatMath),   
  8. REG_JNI(register_android_text_format_Time),   
  9. REG_JNI(register_android_pim_EventRecurrence),   
  10. REG_JNI(register_android_content_AssetManager),   
  11. REG_JNI(register_android_content_StringBlock),   
  12. REG_JNI(register_android_content_XmlBlock),   
  13. REG_JNI(register_android_emoji_EmojiFactory),   
  14. REG_JNI(register_android_security_Md5MessageDigest),   
  15. REG_JNI(register_android_text_AndroidCharacter),   
  16. REG_JNI(register_android_text_KeyCharacterMap),   
  17. REG_JNI(register_android_os_Process),   
  18. REG_JNI(register_android_os_Binder),   
  19. REG_JNI(register_android_os_Hardware),   
  20. REG_JNI(register_android_view_Display),   
  21. REG_JNI(register_android_nio_utils),   
  22. REG_JNI(register_android_graphics_PixelFormat),   
  23. REG_JNI(register_android_graphics_Graphics),   
  24. REG_JNI(register_android_view_Surface),   
  25. REG_JNI(register_android_view_ViewRoot),   
  26. REG_JNI(register_com_google_android_gles_jni_EGLImpl),   
  27. REG_JNI(register_com_google_android_gles_jni_GLImpl),   
  28. REG_JNI(register_android_graphics_Bitmap),   
  29. REG_JNI(register_android_graphics_BitmapFactory),   
  30. REG_JNI(register_android_graphics_Camera),   
  31. REG_JNI(register_android_graphics_Canvas),   
  32. REG_JNI(register_android_graphics_ColorFilter),   
  33. REG_JNI(register_android_graphics_DrawFilter),   
  34. REG_JNI(register_android_graphics_Interpolator),   
  35. REG_JNI(register_android_graphics_LayerRasterizer),   
  36. REG_JNI(register_android_graphics_MaskFilter),   
  37. REG_JNI(register_android_graphics_Matrix),   
  38. REG_JNI(register_android_graphics_Movie),   
  39. REG_JNI(register_android_graphics_NinePatch),   
  40. REG_JNI(register_android_graphics_Paint),   
  41. REG_JNI(register_android_graphics_Path),   
  42. REG_JNI(register_android_graphics_PathMeasure),   
  43. REG_JNI(register_android_graphics_PathEffect),   
  44. REG_JNI(register_android_graphics_Picture),   
  45. REG_JNI(register_android_graphics_PorterDuff),   
  46. REG_JNI(register_android_graphics_Rasterizer),   
  47. REG_JNI(register_android_graphics_Region),   
  48. REG_JNI(register_android_graphics_Shader),   
  49. REG_JNI(register_android_graphics_Typeface),   
  50. REG_JNI(register_android_graphics_Xfermode),   
  51. REG_JNI(register_com_android_internal_graphics_NativeUtils),   
  52. REG_JNI(register_android_database_CursorWindow),   
  53. REG_JNI(register_android_database_SQLiteDatabase),   
  54. REG_JNI(register_android_database_SQLiteDebug),   
  55. REG_JNI(register_android_database_SQLiteProgram),   
  56. REG_JNI(register_android_database_SQLiteQuery),   
  57. REG_JNI(register_android_database_SQLiteStatement),   
  58. REG_JNI(register_android_os_Debug),   
  59. REG_JNI(register_android_os_Exec),   
  60. REG_JNI(register_android_os_FileObserver),   
  61. REG_JNI(register_android_os_FileUtils),   
  62. REG_JNI(register_android_os_ParcelFileDescriptor),   
  63. REG_JNI(register_android_os_Power),   
  64. REG_JNI(register_android_os_StatFs),   
  65. REG_JNI(register_android_os_SystemProperties),   
  66. REG_JNI(register_android_os_UEventObserver),   
  67. REG_JNI(register_android_net_LocalSocketImpl),   
  68. REG_JNI(register_android_net_NetworkUtils),   
  69. REG_JNI(register_android_net_wifi_WifiManager),   
  70. REG_JNI(register_android_os_MemoryFile),   
  71. REG_JNI(register_com_android_internal_os_ZygoteInit),   
  72. REG_JNI(register_android_hardware_Camera),   
  73. REG_JNI(register_android_hardware_SensorManager),   
  74. REG_JNI(register_android_media_AudioRecord),   
  75. REG_JNI(register_android_media_AudioSystem),   
  76. REG_JNI(register_android_media_AudioTrack),   
  77. REG_JNI(register_android_media_JetPlayer),   
  78. REG_JNI(register_android_media_ToneGenerator),   
  79. REG_JNI(register_android_opengl_classes),   
  80. REG_JNI(register_android_bluetooth_Database),   
  81. REG_JNI(register_android_bluetooth_HeadsetBase),   
  82. REG_JNI(register_android_bluetooth_BluetoothAudioGateway),   
  83. REG_JNI(register_android_bluetooth_RfcommSocket),   
  84. REG_JNI(register_android_bluetooth_ScoSocket),   
  85. REG_JNI(register_android_server_BluetoothDeviceService),   
  86. REG_JNI(register_android_server_BluetoothEventLoop),   
  87. REG_JNI(register_android_server_BluetoothA2dpService),   
  88. REG_JNI(register_android_message_digest_sha1),   
  89. REG_JNI(register_android_ddm_DdmHandleNativeHeap),   
  90. REG_JNI(register_android_util_Base64),   
  91. REG_JNI(register_android_location_GpsLocationProvider),   
  92. };  

 

 

这些初始化的native方法是java代码与底层服务打交道的接口,因此我们必须初始化这些方法。

以app_process启动java class的另外一个好处是它会初始化IBinder,这样就可以在java代码和jni代码里面接收IBinder消息。

阅读更多
个人分类: android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭