AndroidAPK应⽤安装原理...
本⽂实例讲述了Android APK应⽤安装之AndroidManifest使⽤PackageParser.parserPackage原理。分享给⼤家供⼤家参考,具体如下:
Android 安装⼀个APK的时候⾸先会解析APK,这⾥要做很多事情,其中⼀个事情就是解析l⽂件,并将所有APK 的Manifest封装到各种对象中并保存在内存当中
解析Manifest的类是⾮常重要的,该类就是frameworks\base\core\java\android\content\pm\PackageParser
PackageManagerService会调⽤PackageParser.parserPackage⽅法来解析APK清单,下⾯开始分析PackageParser的实现:
PackageParser是使⽤的XMLPullParser⼯具来对XML进⾏解析的,然后分别通过t.pm下各种xxxInfo类来进⾏封装:
public Package parsePackage(File sourceFile, String destCodePath,
DisplayMetrics metrics, int flags) {
//最后要跑出的解析错误信息
mParseError = PackageManager.INSTALL_SUCCEEDED;
//获得要解析的⽂件的路径
mArchiveSourcePath = Path();
//如果要解析的不是⽂件类型就跳过并且返回该⽅法
if (!sourceFile.isFile()) {
Log.w(TAG, "Skipping dir: " + mArchiveSourcePath);
//更新错误信息
mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
return null;
}
/
/如果⽂件不是以.apk结尾并且flag没有确定⼀定是APK,那么也返回
if (!Name())
&& (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, "l");
//⾛到这⾥证明⼀切顺利
assetError = false;
} else {
Log.w(TAG, "Failed adding asset path:"+mArchiveSourcePath);
}
} catch (Exception e) {
Log.w(TAG, "Unable to l 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 "
+ PositionDescription()
+ "): " + errorText[0]);
}error parse new
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);
}
//安装的位置
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 = Depth();
//关键时刻到了,真正的开始解析了
while ((()) != parser.END_DOCUMENT
&& (type != parser.END_TAG || Depth() > outerDepth)) {
if (type == parser.END_TAG || type == parser.TEXT) {
continue;
}
String tagName = Name();
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);
...................................................
...................................................
.
..................................................篇幅有限
这⾥分别把每种不同的element⽤不同的⼩⽅法去解析,他们的调⽤顺序是:
这些⼩⽅法⾥其实还是有很多⼩技巧的,有兴趣的话可以细细品位
更多关于Android相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》希望本⽂所述对⼤家Android程序设计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论