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用不一樣的小方法去解析,他們的調用順序是: