Android APK资源加载流程

final InstrumentationInfo ii;

// 创建 mInstrumentation 实例

if (ii != null) {

final ApplicationInfo instrApp = new ApplicationInfo();

ii.copyTo(instrApp);

instrApp.initForUser(UserHandle.myUserId());

final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,

appContext.getClassLoader(), false, true, false);

final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);

try {

final ClassLoader cl = instrContext.getClassLoader();

mInstrumentation = (Instrumentation)

cl.loadClass(data.instrumentationName.getClassName()).newInstance();

} catch (Exception e) {

}

} else {

mInstrumentation = new Instrumentation();

}

Application app;

// 创建 Application 实例

try {

app = data.info.makeApplication(data.restrictedBackupMode, null);

mInitialApplication = app;

try {

mInstrumentation.callApplicationOnCreate(app);

} catch (Exception e) {

}

} finally {

}

}

// http://androidxref.com/8.1.0_r33/xref/frameworks/base/core/java/android/app/LoadedApk.java#959

public Application makeApplication(boolean forceDefaultAppClass,

Instrumentation instrumentation) {

try {

//注释1

ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);

app = mActivityThread.mInstrumentation.newApplication(

cl, appClass, appContext);

appContext.setOuterContext(app);

} catch (Exception e) {

}

return app;

}

static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {

if (packageInfo == null) throw new IllegalArgumentException(“packageInfo”);

return new ContextImpl(null, mainThread,

packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);

}

这个方法我们只留下了最核心的内容,我们看下注释1, ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);这个方法会直接new一个新的ContextImpl

private ContextImpl(ContextImpl container, ActivityThread mainThread,

LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,

Display display, Configuration overrideConfiguration, int createDisplayWithId) {

//LoadApk赋值

mPackageInfo = packageInfo;

mResourcesManager = ResourcesManager.getInstance();

//通过LoadApk.getResources获取Resources对象

Resources resources = packageInfo.getResources(mainThread);

if (resources != null) {

if (displayId != Display.DEFAULT_DISPLAY

|| overrideConfiguration != null

|| (compatInfo != null && compatInfo.applicationScale

!= resources.getCompatibilityInfo().applicationScale)) {

if (container != null) {

// This is a nested Context, so it can’t be a base Activity context.

// Just create a regular Resources object associated with the Activity.

resources = mResourcesManager.getResources(

activityToken,

packageInfo.getResDir(),

packageInfo.getSplitResDirs(),

packageInfo.getOverlayDirs(),

packageInfo.getApplicationInfo().sharedLibraryFiles,

displayId,

overrideConfiguration,

compatInfo,

packageInfo.getClassLoader());

} else {

// This is not a nested Context, so it must be the root Activity context.

// All other nested Contexts will inherit the configuration set here.

resources = mResourcesManager.createBaseActivityResources(

activityToken,

packageInfo.getResDir(),

packageInfo.getSplitResDirs(),

packageInfo.getOverlayDirs(),

packageInfo.getApplicationInfo().sharedLibraryFiles,

displayId,

overrideConfiguration,

compatInfo,

packageInfo.getClassLoader());

}

}

}

//为mResources变量赋值

mResources = resources;

}

packageInfo.getResources,packageInfo是LoadApk类型的,我们看下这个方法

LoadApk#getResources

public Resources getResources(ActivityThread mainThread) {

if (mResources == null) {

mResources = mainThread.getTopLevelResources(mResDir, mSplitResDirs, mOverlayDirs,

mApplicationInfo.sharedLibraryFiles, Display.DEFAULT_DISPLAY, this);

}

return mResources;

}

其中调用了ActivityThread的getTopLevelResources方法,我们继续看一下

ActivityThread#getTopLevelResources

Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,

String[] libDirs, int displayId, LoadedApk pkgInfo) {

return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,

displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());

}

继续调用了mResourcesManager的getResources方法,我么继续跟下去

ResourcesManager#getResources

public @NonNull Resources getResources(@Nullable IBinder activityToken,

@Nullable String resDir,

@Nullable String[] splitResDirs,

@Nullable String[] overlayDirs,

@Nullable String[] libDirs,

int displayId,

@Nullable Configuration overrideConfig,

@NonNull CompatibilityInfo compatInfo,

@Nullable ClassLoader classLoader) {

try {

Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, “ResourcesManager#getResources”);

final ResourcesKey key = new ResourcesKey(

resDir,

splitResDirs,

overlayDirs,

libDirs,

displayId,

overrideConfig != null ? new Configuration(overrideConfig) : null, // Copy

compatInfo);

classLoader = classLoader != null ? classLoader : ClassLoader.getSystemClassLoader();

return getOrCreateResources(activityToken, key, classLoader);

} finally {

Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);

}

}

private @NonNull Resources getOrCreateResources(@Nullable IBinder activityToken,

@NonNull ResourcesKey key, @NonNull ClassLoader classLoader) {

// 创建ResourcesImpl

ResourcesImpl resourcesImpl = createResourcesImpl(key);

final Resources resources;

if (activityToken != null) {

resources = getOrCreateResourcesForActivityLocked(activityToken, classLoader,

resourcesImpl);

} else {

resources = getOrCreateResourcesLocked(classLoader, resourcesImpl);

}

return resources;

}

}

private @NonNull ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) {

final DisplayAdjustments daj = new DisplayAdjustments(key.mOverrideConfiguration);

daj.setCompatibilityInfo(key.mCompatInfo);

final AssetManager assets = createAssetManager(key);

final DisplayMetrics dm = getDisplayMetrics(key.mDisplayId, daj);

final Configuration config = generateConfig(key, dm);

final ResourcesImpl impl = new ResourcesImpl(assets, dm, config, daj);

if (DEBUG) {

Slog.d(TAG, “- creating impl=” + impl + " with key: " + key);

}

return impl;

}

protected @NonNull AssetManager createAssetManager(@NonNull final ResourcesKey key) {

AssetManager assets = new AssetManager();

// resDir can be null if the ‘android’ package is creating a new Resources object.

// This is fine, since each AssetManager automatically loads the ‘android’ package

// already.

if (key.mResDir != null) {

if (assets.addAssetPath(key.mResDir) == 0) {

throw new Resources.NotFoundException("failed to add asset path " + key.mResDir);

}

}

if (key.mSplitResDirs != null) {

for (final String splitResDir : key.mSplitResDirs) {

if (assets.addAssetPath(splitResDir) == 0) {

throw new Resources.NotFoundException(

"failed to add split asset path " + splitResDir);

}

}

}

if (key.mOverlayDirs != null) {

for (final String idmapPath : key.mOverlayDirs) {

assets.addOverlayPath(idmapPath);

}

}

if (key.mLibDirs != null) {

for (final String libDir : key.mLibDirs) {

if (libDir.endsWith(“.apk”)) {

// Avoid opening files we know do not have resources,

// like code-only .jar files.

if (assets.addAssetPathAsSharedLibrary(libDir) == 0) {

Log.w(TAG, “Asset path '” + libDir +

“’ does not exist or contains no resources.”);

}

}

}

}

return assets;

结尾

如何才能让我们在面试中对答如流呢?

答案当然是平时在工作或者学习中多提升自身实力的啦,那如何才能正确的学习,有方向的学习呢?为此我整理了一份Android学习资料路线:

这里是一份BAT大厂面试资料专题包:

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
+

“’ does not exist or contains no resources.”);

}

}

}

}

return assets;

结尾

如何才能让我们在面试中对答如流呢?

答案当然是平时在工作或者学习中多提升自身实力的啦,那如何才能正确的学习,有方向的学习呢?为此我整理了一份Android学习资料路线:

[外链图片转存中…(img-lLN7mRRH-1714383947899)]

这里是一份BAT大厂面试资料专题包:

[外链图片转存中…(img-OIT1Iluw-1714383947901)]

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值