Android资源管理中的SharedLibrary和Dynamic Reference-------之Framework的处理(三)

前面我们已经编译好了资源共享库lib-out.apk(包名:com.google.android.test.shared_library),已经引用这个共享库的应用app-out.apk(包名:com.google.android.test.lib_client),剩下的就是安装、运行起来了。这个自然不必说,但是运行的时候我们的这个App使如何加载我们的资源共享库的呢?还记不记得我们在上期的两个疑问,还有一个期待中的车祸现场?

简要说一下应用的启动过程
我们知道,当init进程起来后,它会解析init.****.rc文件,进行许多操作,比如挂载分区、权限控制等,还会启动许多服务和进程,比如service_manager、media_server、Zygote等。其中Zygote进程是system_server以及我们所有Android应用程序进程的父进程。它的代码就在frameworks/base/cmds/app_process下,我们这里不做过多介绍,后面会有文章专门讨论。Zygote进程起来后,会创建虚拟机,然后加载ZygoteInit.java,执行其main函数,然后就进入java世界,fork出systemServer,然后它就会创建socket,作为服务端等待并处理来自systemServer的fork出其它应用进程的请求。

    当我们的App进程被fork出来后,它会调用ActivityTread的main方法,在ActivityThread的main方法里,它会通过AttachApplication这个binder调用把自己的IApplicationThread接口传给systemServer(或者说AMS),这样就建立起了我们的应用进程和AMS的双向通信机制(跨进程的,一方是应用进程,一方是systemServer进程)。AMS在处理我们的应用进程的AttachApplication请求时,会再次通过IApplicationThread回调应用进程的bindApplication方法,并且会带上一大堆的参数。我们的应用进程则会根据这些参数,设置自己的进程名,创建Application,并执行它的onCreate方法,这就进入到我们熟悉的流程了。

从bindApplication的一个参数说起
从AMS的bindApplication方法传过来的参数有很多:

        public final void bindApplication(String processName, ApplicationInfo appInfo,
                ProviderInfoList providerList, ComponentName instrumentationName,
                ProfilerInfo profilerInfo, Bundle instrumentationArgs,
                IInstrumentationWatcher instrumentationWatcher,
                IUiAutomationConnection instrumentationUiConnection, int debugMode,
                boolean enableBinderTracking, boolean trackAllocation,
                boolean isRestrictedBackupMode, boolean persistent, Configuration config,
                CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
                String buildSerial, AutofillOptions autofillOptions,
                ContentCaptureOptions contentCaptureOptions, long[] disabledCompatChanges) {
   
   

我们现在只关注第二个,也就是ApplicationInfo。不知道大家是否还对《Android资源管理中的SharedLibrary和Dynamic Reference-------之资源共享库(一)》中介绍的ApplicationInfo有印象。我们一起回忆一下:

    /**
     * Full path to the base APK for this application.
     */
    public String sourceDir;
    /**
     * Full paths to the locations of extra resource packages (runtime overlays)
     * this application uses. This field is only used if there are extra resource
     * packages, otherwise it is null.
     *
     * {@hide}
     */
    @UnsupportedAppUsage
    public String[] resourceDirs;
        /**
     * Paths to all shared libraries this application is linked against.  This
     * field is only set if the {@link PackageManager#GET_SHARED_LIBRARY_FILES
     * PackageManager.GET_SHARED_LIBRARY_FILES} flag was used when retrieving
     * the structure.
     */
    public String[] sharedLibraryFiles;
    /**
     * Full path to the directory where native JNI libraries are stored.
     */
    public String nativeLibraryDir;

    是的,ApplicatonInfo的sharedLibraryFiles变量将会是我们的资源共享库包com.google.android.test.shared_library的路径。

ApplicatonInfo.sharedLibraryFiles的值从哪儿来

    既然这个sharedLibraryFiles就是我们的App引用的资源库的路径,那么系统是怎么获取到它的呢?其实这个很简单,包相关的信息都是PMS负责解析和保存的,这个肯定也不例外。我们在我们的App的AndroidManifest.xml里使用了uses-library标签,那么PMS肯定能解析到。我们简单跟一下:

private final boolean attachApplicationLocked(int pid) {
   
   
          .........

          ApplicationInfo appInfo = app.instrumentationInfo != null
                    ? app.instrumentationInfo : app.info;

          .........

          thread.bindApplication(processName, appInfo, providers, app.instrumentationClass,
                    profilerInfo, app.instrumentationArguments, app.instrumentationWatcher,
                    app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace,
                    isRestrictedBackupMode || !normalMode, app.persistent,
                    new Configuration(mConfiguration), app.compat,
                    getCommonServicesLocked(app.isolated),
                    mCoreSettingsObserver.getCoreSettingsLocked());

}

    这里的app是ProcessRecord的实例,而ProcessRecord在构造的时候会传入一个ApplicationInfo实例。这个ApplicationInfo实例是这么来的呢:
在这里插入图片描述
在这里插入图片描述

ActivityThread.java的实现:
在这里插入图片描述

     直接从ServiceManager里获取代理了,对应的实现类为PackageManagerService,也就是
PMS,跟进去:
在这里插入图片描述

这个比较简单,进入PackageParser.java。顾名思义,这个类就是用来解析包信息的:

    public static ApplicationInfo generateApplicationInfo(Package p, int flags,
            PackageUserState state, int userId) {
   
   
        if (p == null) return null;
        if (!checkUseInstalledOrHidden(flags, state)) {
   
   
            return null;
        }
        if (!copyNeeded(flags, p, state, null, userId)
                && (
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值