AndroidAPK应用安装原理...

更新时间:2023-05-12 23:25:07 阅读: 评论:0

AndroidAPK应⽤安装原理...
本⽂实例讲述了Android APK应⽤安装之AndroidManifest使⽤PackageParr.parrPackage原理。分享给⼤家供⼤家参考,具体如下:
Android 安装⼀个APK的时候⾸先会解析APK,这⾥要做很多事情,其中⼀个事情就是解析l⽂件,并将所有APK 的Manifest封装到各种对象中并保存在内存当中
解析Manifest的类是⾮常重要的,该类就是frameworks\ba\core\java\android\content\pm\PackageParr
PackageManagerService会调⽤PackageParr.parrPackage⽅法来解析APK清单,下⾯开始分析PackageParr的实现:
PackageParr是使⽤的XMLPullParr⼯具来对XML进⾏解析的,然后分别通过t.pm下各种xxxInfo类来进⾏封装:
public Package parPackage(File sourceFile, String destCodePath,
DisplayMetrics metrics, int flags) {
//最后要跑出的解析错误信息
mParError = PackageManager.INSTALL_SUCCEEDED;
//获得要解析的⽂件的路径
mArchiveSourcePath = Path();
//如果要解析的不是⽂件类型就跳过并且返回该⽅法
if (!sourceFile.isFile()) {
Log.w(TAG, "Skipping dir: " + mArchiveSourcePath);
//更新错误信息
mParError = 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);
}
//更新错误信息
mParError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
return null;
}
if ((flags&PARSE_CHATTY) != 0 && Config.LOGD) Log.d(
TAG, "Scanning package: " + mArchiveSourcePath);
XmlResourceParr parr = null;
AstManager assmgr = null;
boolean astError = true;
try {
assmgr = new AstManager();
//将⼀个⽂件添加到AstManager中并返回⼀个唯⼀标识
int cookie = assmgr.addAstPath(mArchiveSourcePath);
if(cookie != 0) {
/
/通过标识去AstManager中找到标识对应资源中的Manifest清单⽂件,并返回⼀个XML的解析器
parr = assmgr.openXmlResourceParr(cookie, "l");
//⾛到这⾥证明⼀切顺利
astError = fal;
} el {
Log.w(TAG, "Failed adding ast path:"+mArchiveSourcePath);
}
} catch (Exception e) {
Log.w(TAG, "Unable to l of "
+ mArchiveSourcePath, e);
}
if(astError) {
if (assmgr != null) assmgr.clo();
mParError = 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 = parPackage(res, parr, flags, errorText);
} catch (Exception e) {
errorException = e;
mParError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION; }
if (pkg == null) {
if (errorException != null) {
Log.w(TAG, mArchiveSourcePath, errorException);
} el {
Log.w(TAG, mArchiveSourcePath + " (at "
+ PositionDescription()
+ "): " + errorText[0]);
}
parr.clo();
assmgr.clo();
if (mParError == PackageManager.INSTALL_SUCCEEDED) {
mParError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;  }
return null;
}
parrPackage调⽤了重载的另外⼀个parrPackage
private Package parPackage(
Resources res, XmlResourceParr parr, int flags, String[] outError)
throws XmlPullParrException, IOException {游戏乐趣
AttributeSet attrs = parr;
//每次调⽤这个⽅法时候清空这些变量
mParInstrumentationArgs = null;
mParActivityArgs = null;
mParServiceArgs = null;
mParProviderArgs = null;
//这⾥调⽤这个⽅法获得包名
String pkgName = parPackageName(parr, attrs, flags, outError);
if (pkgName == null) {
mParError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;      return null;
}
int type;
final Package pkg = new Package(pkgName);
boolean foundApp = fal;
//从资源⾥获得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();
}
//获得sharedUrId
String str = sa.getNonConfigurationString(
com.android.internal.R.styleable.AndroidManifest_sharedUrId, 0);
if (str != null && str.length() > 0) {
//验证包名是否符合规则
String nameError = validateName(str, true);
if (nameError != null && !"android".equals(pkgName)) {
outError[0] = "<manifest> specifies bad sharedUrId name \""
+ str + "\": " + nameError;
mParError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;        return null;
}
pkg.mSharedUrId = str.intern();
pkg.mSharedUrLabel = sa.getResourceId(
com.android.internal.R.styleable.AndroidManifest_sharedUrLabel, 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 ((()) != parr.END_DOCUMENT
&& (type != parr.END_TAG || Depth() > outerDepth)) {
if (type == parr.END_TAG || type == parr.TEXT) {
continue;什么表最好排名
}
String tagName = Name();
清远旅游必去十大景点
if (tagName.equals("application")) {
if (foundApp) {
if (RIGID_PARSER) {
outError[0] = "<manifest> has more than one <application>";
mParError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
return null;
} el {
Log.w(TAG, "<manifest> has more than one <application>");
XmlUtils.skipCurrentTag(parr);
continue;
}
}
foundApp = true;
if (!parApplication(pkg, res, parr, attrs, flags, outError)) {
return null;
}
} el if (tagName.equals("permission-group")) {
if (parPermissionGroup(pkg, res, parr, attrs, outError) == null) {
return null;
}
} el if (tagName.equals("permission")) {
if (parPermission(pkg, res, parr, attrs, outError) == null) {
return null;
}
} el if (tagName.equals("permission-tree")) {
if (parPermissionTree(pkg, res, parr, attrs, outError) == null) {
return null;
}
} el if (tagName.equals("us-permission")) {
sa = res.obtainAttributes(attrs,
com.android.internal.R.styleable.AndroidManifestUsPermission);
描写烟花的作文// 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.AndroidManifestUsPermission_name);
...................................................
...................................................
.
..................................................篇幅有限
这⾥分别把每种不同的element⽤不同的⼩⽅法去解析,他们的调⽤顺序是:
这些⼩⽅法⾥其实还是有很多⼩技巧的,有兴趣的话可以细细品位
更多关于Android相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》希望本⽂所述对⼤家Android程序设计有所帮助。

本文发布于:2023-05-12 23:25:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/605819.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:技巧   读者   解析   安装   原理
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图