【Java开源代码栏目提醒】:网学会员Java开源代码为您提供Android应用程序安装过程源代码分析 - 电子设计参考,解决您在Android应用程序安装过程源代码分析 - 电子设计学习中工作中的难题,参考学习。
Android应用程序安装过程源
代码分析 Android系统在启动的过程中会启动一个应用程序管理服务PackageManagerService这个服务负责扫描系统中特定的目录找到里面的应用程序文件即以Apk为后缀的文件然后对这些文件进解析得到应用程序的相关信息完成应用程序的安装过程本文将详细分析这个过程。
应用程序管理服务PackageManagerService安装应用程序的过程其实就是解析析应用程序配置文件AndroidManifest.xml的过程并从里面得到得到应用
程序的相关信息例如得到应用程序的组件Activity、Service、Broadcast Receiver和Content Provider等信息有了这些信息后通过ActivityManagerService这个服务我们就可以在系统中正常地使用这些应用程序了。
应用程序管理服务PackageManagerService是系统启动的时候由SystemServer组件启动的启后它就会执行应用程序安装的过程因此本文将从SystemServer启动PackageManagerService服务的过程开始分析
系统中的应用程序安装的过程。
应用程序管理服务PackageManagerService从启动到安装应用程序的过程如下图所示 下面我们具体分析每一个步骤。
Step 1. SystemServer.main 这个函数定义在frameworks/base/services/
java/com/android/server/SystemServer.
java文件中
java view plaincopy 1. public class SystemServer 2. 3. ...... 4. 5. native public static void init1String args 6. 7. ...... 8. 9. public static void mainString args 10. ...... 11. 12. init1args 13. 14. ...... 15. 16. 17. ...... 18. SystemServer组件是由Zygote进程负责启动的启动的时候就会调用它的main函数这个函数主要调用了JNI方法init1来做一些系统初始化的
工作。
Step 2. SystemServer.init1 这个函数是一个JNI方法实现在 frameworks/base/services/jni/com_android_server_SystemServer.cpp文件中 cpp view plaincopy 1. namespace android 2. 3. extern quotCquot int system_init 4. 5. static void android_server_SystemServer_init1JNIEnv env jobject clazz 6. 7. system_init 8. 9. 10. / 11. JNI registration. 12. / 13. static JNINativeMethod gMethods 14. / name signature funcPtr / 15. quotinit1quot quotLjava/lang/StringVquot void android_server_SystemServer_init1 16. 17. 18. int register_android_server_SystemServerJNIEnv env 19. 20. return jniRegisterNativeMethodsenv quotcom/android/server/SystemServerquot 21. gMethods NELEMgMethods 22. 23. 24. // namespace android 这个函数很简单只是调用了system_init函数来进一步执行操作。
Step 3. libsystem_server.system_init 函数system_init实现在libsystem_server库中源
代码位于frameworks/base/cmds/system_server/library/system_init.cpp文件中 cpp view plaincopy 1. extern quotCquot status_t system_init 2. 3. LOGIquotEntered system_initquot 4. 5. spltProcessStategt procProcessState::self 6. 7. spltIServiceManagergt sm defaultServiceManager 8. LOGIquotServiceManager: pnquot sm.get 9. 10. spltGrimReapergt grim new GrimReaper 11. sm-gtasBinder-gtlinkToDeathgrim grim.get 0 12. 13. char propBufPROPERTY_VALUE_MAX 14. property_getquotsystem_init.startsurfaceflingerquot propBuf quot1quot 15. if strcmppropBuf quot1quot 0 16. // Start the SurfaceFlinger 17. SurfaceFlinger::instantiate 18. 19. 20. // Start the sensor service 21. SensorService::instantiate 22. 23. // On the simulator audioflinger et al dont get started the 24. // same way as on the device and we need to start them here 25. if proc-gtsupportsProcesses 26. 27. // Start the AudioFlinger 28. AudioFlinger::instantiate 29. 30. // Start the media playback service 31. MediaPlayerService::instantiate 32. 33. // Start the camera service 34. CameraService::instantiate 35. 36. // Start the audio policy service 37. AudioPolicyService::instantiate 38. 39. 40. // And now start the Android runtime. We have to do this bit 41. // of nastiness because the Android runtime initialization requires 42. // some of the core system services to already be started. 43. // All other servers should just start the Android runtime at 44. // the beginning of their processess main before calling 45. // the init function. 46. LOGIquotSystem server: starting Android runtime.nquot 47. 48. AndroidRuntime runtime AndroidRuntime::getRuntime 49. 50. LOGIquotSystem server: starting Android services.nquot 51. runtime-gtcallStaticquotcom/android/server/SystemServerquot quotinit2quot 52. 53. // If running in our own process just go into the thread 54. // pool. Otherwise call the initialization finished 55. // func to let this process continue its initilization. 56. if proc-gtsupportsProcesses 57. LOGIquotSystem server: entering thread pool.nquot 58. ProcessState::self-gtstartThreadPool 59. IPCThreadState::self-gtjoinThreadPool 60. LOGIquotSystem server: exiting thread pool.nquot 61. 62. 63. return NO_ERROR 64. 这个函数首先会初始化SurfaceFlinger、SensorService、AudioFlinger、MediaPlayerService、CameraService和AudioPolicyService这几个服务然后就通过系统全局唯一的AndroidRuntime实例变量runtime的callStatic来调用SystemServer的init2函数了。
关于这个AndroidRuntime实例变量runtime的相关资料可能参考前面一篇文章Android应用程序进程启动过程的源
代码分析一文。
Step 4. AndroidRuntime.callStatic 这个函数定义在frameworks/base/core/jni/AndroidRuntime.cpp文件中 cpp view plaincopy 1. / 2. Call a static
Java Programming Language function that takes no arguments and returns void. 3. / 4. status_t AndroidRuntime::callStaticconst char className const char methodName 5. 6. JNIEnv env 7. jclass clazz 8. jmethodID methodId 9. 10. env getJNIEnv 11. if env NULL 12. return UNKNOWN_ERROR 13. 14. clazz findClassenv className 15. if clazz NULL 16. LOGEquotERROR: could not find class snquot className 17. return UNKNOWN_ERROR 18. 19. methodId env-gtGetStaticMethodIDclazz methodName quotVquot 20. if methodId NULL 21. LOGEquotERROR: could not find method s.snquot className methodName 22. return UNKNOWN_ERROR 23. 24. 25. env-gtCallStaticVoidMethodclazz methodId 26. 27. return NO_ERROR 28. 这个函数调用由参数className指定的
java类的静态成员函数这个静态成员函数是由参数methodName指定的。
上面传进来的参数className的值为quotcom/android/server/SystemServerquot而参数methodName的值为quotinit2quot因此接下来就会调用SystemServer类的init2函数了。
Step 5. SystemServer.init2 这个函数定义在frameworks/base/services/
java/com/android/server/SystemServer.
java文件中
java view plaincopy 1. public class SystemServer 2. 3. ...... 4. 5. public static final void init2 6. Slog.iTAG quotEntered the Android system serverquot 7. Thread thr new ServerThread 8. thr.setNamequotandroid.server.ServerThreadquot 9. thr.start 10. 11. 这个函数创建了一个ServerThread线程PackageManagerService服务就是这个线程中启动的了。
这里调用了ServerThread实例thr的start函数之后下面就会执行这个实例的run函数了。
Step 6. ServerThread.run 这个函数定义在frameworks/base/services/
java/com/android/server/SystemServer.
java文件中
java view plaincopy 1. class ServerThread extends Thread 2. ...... 3. 4. Override 5. public void run 6. ...... 7. 8. IPackageManager pm null 9. 10. ...... 11. 12. // Critical services... 13. try 14. ...... 15. 16. Slog.iTAG quotPackage Managerquot 17. pm PackageManagerService.maincontext 18. factoryTest SystemServer.FACTORY_TEST_OFF 19. 20. ...... 21. catch RuntimeException e 22. Slog.equotSystemquot quotFailure starting core servicequot e 23. 24. 25. ...... 26. 27. 28. ...... 29. 这个函数除了启动PackageManagerService服务之外还启动了其它很多的服务例如在前面学习Activity和Service的几篇文章中经常看到的ActivityManagerService服务有兴趣的读者可以自己研究一下。
Step 7. PackageManagerService.main 这个函数定义在frameworks/base/services/
java/com/android/server/PackageManagerService.
java文件中 cpp view plaincopy 1. class PackageManagerService extends IPackageManager.Stub 2. ...... 3. 4. public static final IPackageManager mainContext context boolean factoryTest 5. PackageManagerService m new PackageManagerServicecontext factoryTest 6. ServiceManager.addServicequotpackagequot m 7. return m 8. 9. 10. ...... 11. 这个函数创建了一个PackageManagerService服务实例然后把这个服务添加到ServiceManager中去ServiceManager是Android系统Binder进程间通信机制的守护进程负责
管理系统中的Binder对象具体可以参考Service Manager成为Android进程间
通信IPC机制Binder守护进程之路一文。
在创建这个PackageManagerService服务实例时会在PackageManagerService类的构造函数中开始执行安装应用程序的过程
java view plaincopy 1. class PackageManagerService extends IPackageManager.Stub 2. ...... 3. 4. public PackageManagerServiceContext context boolean factoryTest 5. ...... 6. 7. synchronized mInstallLock 8. synchronized mPackages 9. ...... 10. 11. File dataDir Environment.getDataDirectory 12. mAppDataDir new FiledataDir quotdataquot 13. mSecureAppDataDir new FiledataDir quotsecure/dataquot 14. mDrmAppPrivateInstallDir new FiledataDir quotapp-privatequot 15. 16. ...... 17. 18. mFrameworkDir new FileEnvironment.getRootDirectory quotframeworkquot 19. mDalvikCacheDir new FiledataDir quotdalvik-cachequot 20. 21. ...... 22. 23. // Find base frameworks resource packages without code. 24. mFrameworkInstallObserver new AppDirObserver 25. mFrameworkDir.getPath OBSERVER_EVENTS true 26. mFrameworkInstallObserver.startWatching 27. scanDirLImFrameworkDir PackageParser.PARSE_IS_SYSTEM 28. PackageParser.PARSE_IS_SYSTEM_DIR 29. scanMode SCAN_NO_DEX 0 30. 31. // Collect all system packages. 32. mSystemAppDir new FileEnvironment.getRootDirectory quotappquot 33. mSystemInstallObserver new AppDirObserver 34. mSystemAppDir.getPath OBSERVER_EVENTS true 35. mSystemInstallObserver.startWatching 36. scanDirLImSystemAppDir PackageParser.PARSE_IS_SYSTEM 37. PackageParser.PARSE_IS_SYSTEM_DIR scanMode 0 38. 39. // Collect all vendor packages. 40. mVendorAppDir new Filequot/vendor/appquot 41. mVendorInstallObserver new AppDirObserver 42. mVendorAppDir.getPath OBSERVER_EVENTS true 43. mVendorInstallObserver.startWatching 44. scanDirLImVendorAppDir PackageParser.PARSE_IS_SYSTEM 45. PackageParser.PARSE_IS_SYSTEM_DIR scanMode 0 46. 47. 48. mAppInstallObserver new AppDirObserver 49. mAppInstallDir.getPath OBSERVER_EVENTS false 50. mAppInstallObserver.startWatching 51. scanDirLImAppInstallDir 0 scanMode 0 52. 53. mDrmAppInstallObserver new AppDirObserver 54. mDrmAppPrivateInstallDir.getPath OBSERVER_EVENTS false 55. mDrmAppInstallObserver.startWatching 56. scanDirLImDrmAppPrivateInstallDir PackageParser.PARSE_FORWARD_LOCK 57. scanMode 0 58. 59. ...... 60. 61. 62. 63. 64. ...... 65. 这里会调用scanDirLI函数来扫描移动设备上的下面这五个目录中的Apk文件 /system/framework /system/app /vendor/app /data/app /data/app-private Step 8. PackageManagerService.scanDirLI 这个函数定义在frameworks/base/services/
java/com/android/server/PackageManagerService.
java文件中
java view plaincopy 1. class PackageManagerService extends IPackageManager.Stub 2. ...... 3. 4. private void scanDirLIFile dir int flags int scanMode long currentTime 5. String files dir.list 6. ...... 7. 8. int i 9. for i0 iltfiles.length i 10. File file new Filedir filesi 11. if isPackageFilenamefilesi 12. // Ignore entries which are not apks 13. continue 14. 15. PackageParser.Package pkg scanPackageLIfile 16. flagsPackageParser.PARSE_MUST_BE_APK scanMode currentTime 17. // Dont mess around with apps in system partition. 18. if pkg null ampamp flags amp PackageParser.PARSE_IS_SYSTEM 0 ampamp 19. mLastScanError PackageManager.INSTALL_FAILED_INVALID_APK 20. // Delete the apk 21. Slog.wTAG quotCleaning up failed install of quot file 22. file.delete 23. 24. 25. 26. 27. 28. ...... 29. 对于目录中的每一个文件如果是以后Apk作为后缀名那么就调用scanPackageLI函数来对它进行解析和安装。
Step 9. PackageManagerService.scanPackageLI 这个函数定义在frameworks/base/services/
java/com/android/server/PackageManagerService.
java文件中
java view plaincopy 1. class PackageManagerService extends IPackageManager.Stub 2. ...... 3. 4. private PackageParser.Package scanPackageLIFile scanFile 5. int parseFlags int scanMode long currentTime 6. ...... 7. 8. String scanPath scanFile.getPath 9. parseFlags mDefParseFlags 10. PackageParser pp new PackageParserscanPath 11. 12. ...... 13. 14. final PackageParser.Package pkg pp.parsePackagescanFile 15. scanPath mMetrics parseFlags 16. 17. ...... 18. 19. return scanPackageLIpkg parseFlags scanMode SCAN_UPDATE_SIGNATURE currentTime 20. 21. 22. ...... 23. 这个函数首先会为这个Apk文件创建一个PackageParser实例接着调用这个实例的parsePackage函数来对这个Apk文件进行解析。
这个函数最后还会调用另外一个版本的scanPackageLI函数把来解析后得到的应用程序信息保存在PackageManagerService中。
Step 10. PackageParser.parsePackage 这个函数定义在frameworks/base/core/
java/android/content/pm/PackageParser.
java文件中
java view plaincopy 1. public class PackageParser 2. ...... 3. 4. public Package parsePackageFile sourceFile String destCodePath 5. DisplayMetrics metrics int flags 6. ...... 7. 8. mArchiveSourcePath sourceFile.getPath 9. 10. ...... 11. 12. XmlResourceParser parser null 13. AssetManager assmgr null 14. boolean assetError true 15. try 16. assmgr new AssetManager 17. int cookie assmgr.addAssetPathmArchiveSourcePath 18. ifcookie 0 19. parser assmgr.openXmlResourceParsercookie quotAndroidManifest.
xmlquot 20. assetError false 21. else 22. .