最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android APK应用安装原理解析之AndroidManifest使用PackageParser.parserPackage原理分析
时间:2022-06-25 23:07:35 编辑:袖梨 来源:一聚教程网
本文实例讲述了Android APK应用安装之AndroidManifest使用PackageParser.parserPackage原理。分享给大家供大家参考,具体如下:
Android 安装一个APK的时候首先会解析APK,这里要做很多事情,其中一个事情就是解析Manifest.xml文件,并将所有APK的Manifest封装到各种对象中并保存在内存当中
解析Manifest的类是非常重要的,该类就是frameworksbasecorejavaandroidcontentpmPackageParser
PackageManagerService会调用PackageParser.parserPackage方法来解析APK清单,下面开始分析PackageParser的实现:
PackageParser是使用的XMLPullParser工具来对XML进行解析的,然后分别通过android.content.pm下各种xxxInfo类来进行封装:
public Package parsePackage(File sourceFile, String destCodePath, DisplayMetrics metrics, int flags) { //最后要跑出的解析错误信息 mParseError = PackageManager.INSTALL_SUCCEEDED; //获得要解析的文件的路径 mArchiveSourcePath = sourceFile.getPath(); //如果要解析的不是文件类型就跳过并且返回该方法 if (!sourceFile.isFile()) { Log.w(TAG, "Skipping dir: " + mArchiveSourcePath); //更新错误信息 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK; return null; } //如果文件不是以.apk结尾并且flag没有确定一定是APK,那么也返回 if (!isPackageFilename(sourceFile.getName()) && (flags&PARSE_MUST_BE_APK) != 0) { if ((flags&PARSE_IS_SYSTEM) == 0) { // We expect to have non-.apk files in the system dir, // so don't warn about them. Log.w(TAG, "Skipping non-package file: " + mArchiveSourcePath); } //更新错误信息 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK; return null; } if ((flags&PARSE_CHATTY) != 0 && Config.LOGD) Log.d( TAG, "Scanning package: " + mArchiveSourcePath); XmlResourceParser parser = null; AssetManager assmgr = null; boolean assetError = true; try { assmgr = new AssetManager(); //将一个文件添加到AssetManager中并返回一个唯一标识 int cookie = assmgr.addAssetPath(mArchiveSourcePath); if(cookie != 0) { //通过标识去AssetManager中找到标识对应资源中的Manifest清单文件,并返回一个XML的解析器 parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml"); //走到这里证明一切顺利 assetError = false; } else { Log.w(TAG, "Failed adding asset path:"+mArchiveSourcePath); } } catch (Exception e) { Log.w(TAG, "Unable to read AndroidManifest.xml of " + mArchiveSourcePath, e); } if(assetError) { if (assmgr != null) assmgr.close(); mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST; return null; } String[] errorText = new String[1]; Package pkg = null; Exception errorException = null; try { // XXXX todo: need to figure out correct configuration. Resources res = new Resources(assmgr, metrics, null); //这个是真正在解析的package的方法,是private method pkg = parsePackage(res, parser, flags, errorText); } catch (Exception e) { errorException = e; mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION; } if (pkg == null) { if (errorException != null) { Log.w(TAG, mArchiveSourcePath, errorException); } else { Log.w(TAG, mArchiveSourcePath + " (at " + parser.getPositionDescription() + "): " + errorText[0]); } parser.close(); assmgr.close(); if (mParseError == PackageManager.INSTALL_SUCCEEDED) { mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED; } return null; }
parserPackage调用了重载的另外一个parserPackage
private Package parsePackage( Resources res, XmlResourceParser parser, int flags, String[] outError) throws XmlPullParserException, IOException { AttributeSet attrs = parser; //每次调用这个方法时候清空这些变量 mParseInstrumentationArgs = null; mParseActivityArgs = null; mParseServiceArgs = null; mParseProviderArgs = null; //这里调用这个方法获得包名 String pkgName = parsePackageName(parser, attrs, flags, outError); if (pkgName == null) { mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME; return null; } int type; final Package pkg = new Package(pkgName); boolean foundApp = false; //从资源里获得AndroidManifest的数组 TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifest); //继续挖掘出版本号 pkg.mVersionCode = sa.getInteger( com.android.internal.R.styleable.AndroidManifest_versionCode, 0); //获取版本名 pkg.mVersionName = sa.getNonConfigurationString( com.android.internal.R.styleable.AndroidManifest_versionName, 0); if (pkg.mVersionName != null) { pkg.mVersionName = pkg.mVersionName.intern(); } //获得sharedUserId String str = sa.getNonConfigurationString( com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0); if (str != null && str.length() > 0) { //验证包名是否符合规则 String nameError = validateName(str, true); if (nameError != null && !"android".equals(pkgName)) { outError[0] = "specifies bad sharedUserId name "" + str + "": " + nameError; mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID; return null; } pkg.mSharedUserId = str.intern(); pkg.mSharedUserLabel = sa.getResourceId( com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0); } sa.recycle(); //安装的位置 pkg.installLocation = sa.getInteger( com.android.internal.R.styleable.AndroidManifest_installLocation, PARSE_DEFAULT_INSTALL_LOCATION); // Resource boolean are -1, so 1 means we don't know the value. int supportsSmallScreens = 1; int supportsNormalScreens = 1; int supportsLargeScreens = 1; int resizeable = 1; int anyDensity = 1; int outerDepth = parser.getDepth(); //关键时刻到了,真正的开始解析了 while ((type=parser.next()) != parser.END_DOCUMENT && (type != parser.END_TAG || parser.getDepth() > outerDepth)) { if (type == parser.END_TAG || type == parser.TEXT) { continue; } String tagName = parser.getName(); if (tagName.equals("application")) { if (foundApp) { if (RIGID_PARSER) { outError[0] = " has more than one "; mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED; return null; } else { Log.w(TAG, " has more than one "); XmlUtils.skipCurrentTag(parser); continue; } } foundApp = true; if (!parseApplication(pkg, res, parser, attrs, flags, outError)) { return null; } } else if (tagName.equals("permission-group")) { if (parsePermissionGroup(pkg, res, parser, attrs, outError) == null) { return null; } } else if (tagName.equals("permission")) { if (parsePermission(pkg, res, parser, attrs, outError) == null) { return null; } } else if (tagName.equals("permission-tree")) { if (parsePermissionTree(pkg, res, parser, attrs, outError) == null) { return null; } } else if (tagName.equals("uses-permission")) { sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestUsesPermission); // Note: don't allow this value to be a reference to a resource // that may change. String name = sa.getNonResourceString( com.android.internal.R.styleable.AndroidManifestUsesPermission_name); sa.recycle(); ................................................... ................................................... ...................................................篇幅有限
这里分别把每种不同的element用不同的小方法去解析,他们的调用顺序是:
这些小方法里其实还是有很多小技巧的,有兴趣的话可以细细品位
相关文章
- 《燕云十六声》红尘无眼完成图文攻略 12-25
- 《燕云十六声》阴阳如影完成图文攻略 12-25
- 《燕云十六声》悬檐之下四架椽屋图文攻略 12-25
- 《燕云十六声》2024最新公测时间介绍 12-25
- 《燕云十六声》有没有藏宝阁 12-25
- 《燕云十六声》制作公司介绍 12-25