您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android APK中AndroidManifest使用PackageParser.parserPackage的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Android 安裝一個APK的時候首先會解析APK,這里要做很多事情,其中一個事情就是解析Manifest.xml文件,并將所有APK的Manifest封裝到各種對象中并保存在內存當中
解析Manifest的類是非常重要的,該類就是frameworks\base\core\java\android\content\pm\PackageParser
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] = "<manifest> 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] = "<manifest> has more than one <application>"; mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED; return null; } else { Log.w(TAG, "<manifest> has more than one <application>"); 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用不同的小方法去解析,他們的調用順序是:
關于“Android APK中AndroidManifest使用PackageParser.parserPackage的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。