這篇文章將為大家詳細講解有關(guān)Android APK中AndroidManifest使用PackageParser.parserPackage的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比察布查爾錫伯網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式察布查爾錫伯網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋察布查爾錫伯地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
Android 安裝一個APK的時候首先會解析APK,這里要做很多事情,其中一個事情就是解析Manifest.xml文件,并將所有APK的Manifest封裝到各種對象中并保存在內(nèi)存當(dāng)中
解析Manifest的類是非常重要的,該類就是frameworks\base\core\java\android\content\pm\PackageParser
PackageManagerService會調(diào)用PackageParser.parserPackage方法來解析APK清單,下面開始分析PackageParser的實現(xiàn):
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結(jié)尾并且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中找到標識對應(yīng)資源中的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調(diào)用了重載的另外一個parserPackage
private Package parsePackage( Resources res, XmlResourceParser parser, int flags, String[] outError) throws XmlPullParserException, IOException { AttributeSet attrs = parser; //每次調(diào)用這個方法時候清空這些變量 mParseInstrumentationArgs = null; mParseActivityArgs = null; mParseServiceArgs = null; mParseProviderArgs = null; //這里調(diào)用這個方法獲得包名 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的數(shù)組 TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifest); //繼續(xù)挖掘出版本號 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) { //驗證包名是否符合規(guī)則 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(); //關(guān)鍵時刻到了,真正的開始解析了 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用不同的小方法去解析,他們的調(diào)用順序是:
關(guān)于“Android APK中AndroidManifest使用PackageParser.parserPackage的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。