WEO啦

应用程序包的安装是android的特点x
收录时间:2022-11-25 21:38:59  浏览:0
应用程序包的安装是android的特点APK为AndroidPackage的缩写Android应用安装有如下四种方式:1/系统应用安装开机时完成,没有安装界面2/网络下载应用安装通过market应用完成,没有安装界面3/ADB工具安装没有安装界面。4/第三方应用安装通过SD卡里的APK文件安装,有安装界面,由 packageinstaller/apk应用处理安装及卸载过程的界面。应用安装的流程及路径应用安装涉及到如下几个目录:system/app-系统自带的应用程序,获得adbroot权限才能删除data/app-用户程序安装的目录。安装时把apk文件复制到此目录data/data-存放应用程序的数据data/dalvik-cache-将apk中的dex文件安装到dalvik-cache目录下(dex文件是dalvik虚拟机的可执行文件/其大小约为原始apk文件大小的四分之一)安装过程:复制APK安装包到data/app目录下,解压并扫描安装包,把dex文件(Dalvik字节码)保存到dalvik-cache目录,并data/data目录下创建对应的应用数据目录。卸载过程:删除安装过程中在上述三个目录下创建的文件及目录。安装应用的过程解析一/开机安装PackageManagerService处理各种应用的安装,卸载,管理等工作,开机时由systemServer启动此服务(源文件路径:androidframeworksbaseservicesjavacomandroidserverPackageManagerService/java)PackageManagerService服务启动的流程:1/首先扫描安装“systemframework”目录下的jar包 java view plaincopyprint?1/ /Findbaseframeworks(resourcepackageswithoutcode)/ 2/ mFrameworkInstallObserver=newAppDirObserver(3/ mFrameworkDir/getPath()/OBSERVER_EVENTS/true)/4/ mFrameworkInstallObserver/startWatching()/5/ scanDirLI(mFrameworkDir/PackageParser/PARSE_IS_SYSTEM6/ |PackageParser/PARSE_IS_SYSTEM_DIR/7/ scanMode|SCAN_NO_DEX/0)/2/扫描安装系统system/app的应用程序 java view plaincopyprint?1/ /Collectallsystempackages/ 2/ mSystemAppDir=newFile(Environment/getRootDirectory()/app)/3/ mSystemInstallObserver=newAppDirObserver(4/ mSystemAppDir/getPath()/OBSERVER_EVENTS/true)/5/ mSystemInstallObserver/startWatching()/6/ scanDirLI(mSystemAppDir/PackageParser/PARSE_IS_SYSTEM7/ |PackageParser/PARSE_IS_SYSTEM_DIR/scanMode/0)/3/制造商的目录下/vendor/app应用包 java view plaincopyprint?1/ /Collectallvendorpackages/ 2/ mVendorAppDir=newFile(/vendor/app)/3/ mVendorInstallObserver=newAppDirObserver(4/ mVendorAppDir/getPath()/OBSERVER_EVENTS/true)/5/ mVendorInstallObserver/startWatching()/6/ scanDirLI(mVendorAppDir/PackageParser/PARSE_IS_SYSTEM7/ |PackageParser/PARSE_IS_SYSTEM_DIR/scanMode/0)/4/扫描“dataapp”目录,即用户安装的第三方应用java view plaincopyprint?1/ scanDirLI(mAppInstallDir/0/scanMode/0)/5/扫描dataapp-private目录,即安装DRM保护的APK文件(一个受保护的歌曲或受保 护的***是使用DRM保护的文件)java view plaincopyprint?1/ scanDirLI(mDrmAppPrivateInstallDir/PackageParser/PARSE_FORWARD_LOCK/2/ scanMode/0)/扫描方法的代码清单java view plaincopyprint?1/ privatevoidscanDirLI(Filedir/intflags/intscanMode/longcurrentTime)2/ Stringfiles=dir/list()/3/ if(files=null)4/ Log/d(TAG/Nofilesinappdir+dir)/5/ return/6/ 7/ if(false)8/ Log/d(TAG/Scanningappdir+dir)/9/ 10/ inti/11/ for(i=0/ifiles/length/i+)12/ Filefile=newFile(dir/filesi)/13/ if(!isPackageFilename(filesi)14/ /Ignoreentrieswhicharenotapks 15/ continue/16/ 17/ PackageParser/Packagepkg=scanPackageLI(file/18/ flags|PackageParser/PARSE_MUST_BE_APK/scanMode/currentTime)/19/ /Dontmessaroundwithappsinsystempartition/ 20/ if(pkg=null&(flags&PackageParser/PARSE_IS_SYSTEM)=0&21/ mLastScanError=PackageManager/INSTALL_FAILED_INVALID_APK)22/ /Deletetheapk 23/ Slog/w(TAG/Cleaningupfailedinstallof+file)/24/ file/delete()/25/ 26/ 27/ 并且从该扫描方法中可以看出调用了scanPackageLI()privatePackageParser/PackagescanPackageLI(FilescanFile/intparseFlags/intscanMode/longcurrentTime)***scanPackageLI()方法后发现,程序经过很多次的ifelse的筛选,最后判定可以安装后调用了mInstaller/installjava view plaincopyprint?1/ if(mInstaller!=null)2/ intret=mInstaller/install(pkgName/useEncryptedFSDir/pkg/applicationInfo/uid/pkg/applicationInfo/uid)/3/ if(ret0)49/ HandlerParamsparams=mPendingInstalls/get(0)/50/ if(params!=null)51/ params/startCopy()/52/ 53/ else54/ /Shouldneverhappenideally/ 55/ Slog/w(TAG/Emptyqueue)/56/ 57/ break/58/ 59/ *省略若干*60/ 61/ publicfinalbooleansendMessage(Messagemsg)publicfinalbooleansendEmptyMessage(intwhat)两者参数有别。然后调用抽象类HandlerParams中的一个startCopy()方法abstractclassHandlerParamsfinalvoidstartCopy()*若干if语句判定否这打回handler消息*handleReturnCode()/handleReturnCode()复写了两次其中有一次是删除时要调用的,只列出安装调用的一个方法 java view plaincopyprint?1/ Override2/ voidhandleReturnCode()3/ /IfmArgsisnull/thenMCScouldntbereached/Whenit 4/ /reconnects/itwilltryagaintoinstall/Atthatpoint/this 5/ /willsucceed/ 6/ if(mArgs!=null)7/ processPendingInstall(mArgs/mRet)/8/ 9/ 这时可以清楚的看见processPendingInstall()被调用。其中run()方法如下java view plaincopyprint?1/ run()2/ synchronized(mInstallLock)3/ *省略*4/ installPackageLI(args/true/res)/5/ 6/ 7/ 8/ instaPacakgeLI()args/res参数分析/InstallArgs是在PackageService定义的staticabstractclassInstallArgs静态抽象类。java view plaincopyprint?1/ staticabstractclassInstallArgs2/ *3/ 其中定义了flag标志,packageURL,创建文件,拷贝apk,修改包名称,4/ 还有一些删除文件的清理,释放存储函数。5/ *6/ 7/ classPackageInstalledInfo8/ Stringname/9/ intuid/10/ PackageParser/Packagepkg/11/ intreturnCode/12/ PackageRemovedInforemovedInfo/13/ java view plaincopyprint?1/ privatevoidinstallPackageLI(InstallArgsargs/2/ booleannewInstall/PackageInstalledInfores)3/ intpFlags=args/flags/4/ StringinstallerPackageName=args/installerPackageName/5/ FiletmpPackageFile=newFile(args/getCodePath()/6/ booleanforwardLocked=(pFlags&PackageManager/INSTALL_FORWARD_LOCK)!=0)/7/ booleanonSd=(pFlags&PackageManager/INSTALL_EXTERNAL)!=0)/8/ booleanreplace=false/9/ intscanMode=(onSd?0/SCAN_MONITOR)|SCAN_FORCE_DEX|SCAN_UPDATE_SIGNATURE10/ |(newInstall?SCAN_NEW_INSTALL/0)/11/ /Resultobjecttobereturned 12/ res/returnCode=PackageManager/INSTALL_SUCCEEDED/13/ /RetrievePackageSettingsandparsepackage 14/ intparseFlags=PackageParser/PARSE_CHATTY|15/ (forwardLocked?PackageParser/PARSE_FORWARD_LOCK/0)|16/ (onSd?PackageParser/PARSE_ON_SDCARD/0)/17/ parseFlags|=mDefParseFlags/18/ PackageParserpp=newPackageParser(tmpPackageFile/getPath()/19/ pp/setSeparateProcesses(mSeparateProcesses)/20/ finalPackageParser/Packagepkg=pp/parsePackage(tmpPackageFile/21/ null/mMetrics/parseFlags)/22/ if(pkg=null)23/ res/returnCode=pp/getParseError()/24/ return/25/ 26/ StringpkgName=res/name=pkg/packageName/27/ if(pkg/applicationInfo/flags&ApplicationInfo/FLAG_TEST_ONLY)!=0)28/ if(pFlags&PackageManager/INSTALL_ALLOW_TEST)=0)29/ res/returnCode=PackageManager/INSTALL_FAILED_TEST_ONLY/30/ return/31/ 32/ 33/ if(GET_CERTIFICATES&!pp/collectCertificates(pkg/parseFlags)34/ res/returnCode=pp/getParseError()/35/ return/36/ 37/ /Getridofallreferencestopackagescanpathviaparser/ 38/ pp=null/39/ StringoldCodePath=null/40/ booleansystemApp=false/41/ synchronized(mPackages)42/ /Checkifinstallingalreadyexistingpackage 43/ if(pFlags&PackageManager/INSTALL_REPLACE_EXISTING)!=0)44/ StringoldName=mSettings/mRenamedPackages/get(pkgName)/45/ if(pkg/mOriginalPackages!=null46/ &pkg/mOriginalPackages/contains(oldName)47/ &mPackages/containsKey(oldName)48/ /Thispackageisderivedfromanoriginalpackage/ 49/ /andthisdevicehasbeenupdatingfromthatoriginal 50/ /name/Wemustcontinueusingtheoriginalname/so 51/ /renamethenewpackagehere/ 52/ pkg/setPackageName(oldName)/53/ pkgName=pkg/packageName/54/ replace=true/55/ elseif(mPackages/containsKey(pkgName)56/ /Thispackage/underitsofficialname/alreadyexists 57/ /onthedevice/weshouldreplaceit/ 58/ replace=true/59/ 60/ 61/ PackageSettingps=mSettings/mPackages/get(pkgName)/62/ if(ps!=null)63/ oldCodePath=mSettings/mPackages/get(pkgName)/codePathString/64/ if(ps/pkg!=null&ps/pkg/applicationInfo!=null)65/ systemApp=(ps/pkg/applicationInfo/flags&66/ ApplicationInfo/FLAG_SYSTEM)!=0/67/ 68/ 69/ 70/ if(systemApp&onSd)71/ /Disableupdatestosystemappsonsdcard 72/ Slog/w(TAG/Cannotinstallupdatestosystemappsonsdcard)/73/ res/returnCode=PackageManager/INSTALL_FAILED_INVALID_INSTALL_LOCATION/74/ return/75/ 76/ if(!args/doRename(res/returnCode/pkgName/oldCodePath)77/ res/returnCode=PackageManager/INSTALL_FAILED_INSUFFICIENT_STORAGE/78/ return/79/ 80/ /Setapplicationobjectspathexplicitlyaftertherename 81/ setApplicationInfoPaths(pkg/args/getCodePath()/args/getResourcePath()/82/ pkg/applicationInfo/nativeLibraryDir=args/getNativeLibraryPath()/83/ if(replace)84/ replacePackageLI(pkg/parseFlags/scanMode/85/ installerPackageName/res)/86/ else87/ installNewPackageLI(pkg/parseFlags/scanMode/88/ installerPackageName/res)/89/ 90/ 最后判断如果以前不存在那么调用installNewPackageLI()java view plaincopyprint?1/ privatevoidinstallNewPackageLI(PackageParser/Packagepkg/2/ intparseFlags/intscanMode/3/ StringinstallerPackageName/PackageInstalledInfores)4/ *省略若干*5/ PackageParser/PackagenewPackage=scanPackageLI(pkg/parseFlags/scanMode/6/ System/currentTimeMillis()/7/ *省略若干*8/ 最后终于回到了和开机安装一样的地方/与开机方式安装调用统一方法。三、从ADB工具安装其入口函数源文件为pm/java(源文件路径:androidframeworksbasecmdspmsrccomandroidcommandspmpm/java)其中systemframeworkpm/jar包管理库包管理脚本systembinpm解析showUsage就是使用方法java view plaincopyprint?1/ privatestaticvoidshowUsage()2/ System/err/println(usage/pmlist|path|install|uninstall)/3/ System/err/println(pmlistpackages-f)/4/ System/err/println(pmlistpermission-groups)/5/ System/err/println(pmlistpermissions-g-f-d-uGROUP)/6/ System/err/println(pmlistinstrumentation-fTARGET-PACKAGE)/7/ System/err/println(pmlistfeatures)/8/ System/err/println(pmpathPACKAGE)/9/ System/err/println(pminstall-l-r-t-iINSTALLER_PACKAGE_NAME-s-fPATH)/10/ System/err/println(pmuninstall-kPACKAGE)/11/ System/err/println(pmenablePACKAGE_OR_COMPONENT)/12/ System/err/println(pmdisablePACKAGE_OR_COMPONENT)/1
温馨提示:
1. WEO啦仅展示《应用程序包的安装是android的特点x》的部分公开内容,版权归原著者或相关公司所有。
2. 文档内容来源于互联网免费公开的渠道,若文档所含内容侵犯了您的版权或隐私,请通知我们立即删除。
3. 当前页面地址:https://www.weo.la/doc/2ec606fc3bdbbf8b.html 复制内容请保留相关链接。