问题背景

最近跟群友评论一个技术问题:

一个app到底会创建多少个Application对象

一个使用开启了多进程,终究到底会创立几个application目标,履行几回onCreate()办法?

有的群友根据自己的主意给出了猜测

一个app到底会创建多少个Application对象

乃至有的群友直接咨询起了ChatGPT

一个app到底会创建多少个Application对象

但至始至终都没有一个终究的定论。所以乎,为了弄清这个问题,我决定先写个demo测验得出定论,然后从源码着手分析原因

Demo验证

首先创立了一个app项目,开启多进程

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:name=".DemoApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo0307"
        tools:targetApi="31">
        <!--android:process 开启多进程并设置进程名-->
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

然后在DemoApplication的onCreate()办法打印application目标的地址,当时进程称号

public class DemoApplication extends Application {
    private static final String TAG = "jasonwan";
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Demo application onCreate: " + this + ", processName=" + getProcessName(this));
    }
    private String getProcessName(Application app) {
        int myPid = Process.myPid();
        ActivityManager am = (ActivityManager) app.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo runningAppProcess : runningAppProcesses) {
            if (runningAppProcess.pid == myPid) {
                return runningAppProcess.processName;
            }
        }
        return "null";
    }
}

运转,得到的日志如下

2023-03-07 11:15:27.785 19563-19563/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote

检查当时使用一切进程

一个app到底会创建多少个Application对象

说明此刻app只要一个进程,且只要一个application目标,目标地址为@fb06c2d

现在咱们将进程增加到多个,看看情况如何

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:name=".DemoApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo0307"
        tools:targetApi="31">
        <!--android:process 开启多进程并设置进程名-->
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TwoActivity"
            android:process=":remote2" />
        <activity
            android:name=".ThreeActivity"
            android:process=":remote3" />
        <activity
            android:name=".FourActivity"
            android:process=":remote4" />
        <activity
            android:name=".FiveActivity"
            android:process=":remote5" />
    </application>
</manifest>

逻辑是点击MainActivity发动TwoActivity,点击TwoActivity发动ThreeActivity,以此类推。最终咱们运转,发动一切Activity得到的日志如下

2023-03-07 11:25:35.433 19955-19955/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote
2023-03-07 11:25:43.795 20001-20001/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote2
2023-03-07 11:25:45.136 20046-20046/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote3
2023-03-07 11:25:45.993 20107-20107/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote4
2023-03-07 11:25:46.541 20148-20148/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote5

检查当时使用一切进程

一个app到底会创建多少个Application对象

此刻app有5个进程,但application目标地址均为@fb06c2d,地址相同意味着它们是同一个目标。

那是不是就能够得出定论,不管发动多少个进程都只会创立一个application目标呢?并不能妄下此定论,咱们将MainActivityprocess属性去掉再运转,得到的日志如下

2023-03-07 11:32:10.156 20318-20318/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@5d49e29, processName=com.jason.demo0307
2023-03-07 11:32:15.143 20375-20375/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote2
2023-03-07 11:32:16.477 20417-20417/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote3
2023-03-07 11:32:17.582 20463-20463/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote4
2023-03-07 11:32:18.882 20506-20506/com.jason.demo0307 D/jasonwan: Demo application onCreate: com.jason.demo0307.DemoApplication@fb06c2d, processName=com.jason.demo0307:remote5

检查当时使用一切进程

一个app到底会创建多少个Application对象

此刻app有5个进程,但有2个application目标,目标地址为@5d49e29和@fb06c2d,且子进程的application目标都相同。

上述一切进程的父进程ID为678,而此进程正是zygote进程

根据上面的测验成果咱们目前能得出的定论:

  • 定论1:单进程只创立一个Application目标,履行一次onCreate()办法;
  • 定论2:多进程至少创立2个Application目标,履行多次onCreate()办法,几个进程就履行几回;

定论2为什么说至少创立2个,因为我在集成了JPush的商业项目中测验发现,JPush创立的进程跟我自己创立的进程,Application地址是不同的。

一个app到底会创建多少个Application对象

这儿三个进程,分别创立了三个Application目标,目标地址分别是@f31ba9d,@2c586f3,@fb06c2d

源码分析

这儿需求先了解App的发动流程,详细能够参考《App发动流程》

Application的创立位于frameworks/base/core/java/android/app/ActivityThread.javahandleBindApplication()办法中

	@UnsupportedAppUsage
    private void handleBindApplication(AppBindData data) {
        long st_bindApp = SystemClock.uptimeMillis();
        //省掉部分代码
        // Note when this process has started.
        //设置进程发动时刻
        Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
        //省掉部分代码
        // send up app name; do this *before* waiting for debugger
        //设置进程称号
        Process.setArgV0(data.processName);
        //省掉部分代码
        // Allow disk access during application and provider setup. This could
        // block processing ordered broadcasts, but later processing would
        // probably end up doing the same disk access.
        Application app;
        final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
        final StrictMode.ThreadPolicy writesAllowedPolicy = StrictMode.getThreadPolicy();
        try {
            // If the app is being launched for full backup or restore, bring it up in
            // a restricted environment with the base application class.
            //此处开端创立application目标,注意参数2为null
            app = data.info.makeApplication(data.restrictedBackupMode, null);
            //省掉部分代码
            try {
                if ("com.jason.demo0307".equals(app.getPackageName())){
                    Log.d("jasonwan", "execute app onCreate(), app=:"+app+", processName="+getProcessName(app)+", pid="+Process.myPid());
                }
                //履行application的onCreate办法()
                mInstrumentation.callApplicationOnCreate(app);
            } catch (Exception e) {
                if (!mInstrumentation.onException(app, e)) {
                    throw new RuntimeException(
                      "Unable to create application " + app.getClass().getName()
                      + ": " + e.toString(), e);
                }
            }
        } finally {
            // If the app targets < O-MR1, or doesn't change the thread policy
            // during startup, clobber the policy to maintain behavior of b/36951662
            if (data.appInfo.targetSdkVersion < Build.VERSION_CODES.O_MR1
                    || StrictMode.getThreadPolicy().equals(writesAllowedPolicy)) {
                StrictMode.setThreadPolicy(savedPolicy);
            }
        }
        //省掉部分代码
    }

实际创立过程在frameworks/base/core/java/android/app/LoadedApk.java中的makeApplication()办法中,LoadedApk望文生义便是加载好的Apk文件,里边包括Apk一切信息,像包名、Application目标,app地点的目录等,这儿直接看application的创立过程

	@UnsupportedAppUsage
    public Application makeApplication(boolean forceDefaultAppClass,
            Instrumentation instrumentation) {
        if ("com.jason.demo0307".equals(mApplicationInfo.packageName)) {
            Log.d("jasonwan", "makeApplication: mApplication="+mApplication+", pid="+Process.myPid());
        }
        //假如已经创立过了就不再创立
        if (mApplication != null) {
            return mApplication;
        }
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "makeApplication");
        Application app = null;
        String appClass = mApplicationInfo.className;
        if (forceDefaultAppClass || (appClass == null)) {
            appClass = "android.app.Application";
        }
        try {
            java.lang.ClassLoader cl = getClassLoader();
            if (!mPackageName.equals("android")) {
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
                        "initializeJavaContextClassLoader");
                initializeJavaContextClassLoader();
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
            }
            ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
            //反射创立application目标
            app = mActivityThread.mInstrumentation.newApplication(
                    cl, appClass, appContext);
            if ("com.jason.demo0307.DemoApplication".equals(appClass)){
                Log.d("jasonwan", "create application, app="+app+", processName="+mActivityThread.getProcessName()+", pid="+Process.myPid());
            }
            appContext.setOuterContext(app);
        } catch (Exception e) {
            Log.d("jasonwan", "fail to create application, "+e.getMessage());
            if (!mActivityThread.mInstrumentation.onException(app, e)) {
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                throw new RuntimeException(
                    "Unable to instantiate application " + appClass
                    + ": " + e.toString(), e);
            }
        }
        mActivityThread.mAllApplications.add(app);
        mApplication = app;
        if (instrumentation != null) {
            try {
                //第一次发动创立时,instrumentation为null,不会履行onCreate()办法
                instrumentation.callApplicationOnCreate(app);
            } catch (Exception e) {
                if (!instrumentation.onException(app, e)) {
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    throw new RuntimeException(
                        "Unable to create application " + app.getClass().getName()
                        + ": " + e.toString(), e);
                }
            }
        }
        // 省掉部分代码
        return app;
    }

为了看清application到底被创立了几回,我在关键地方埋下了log,TAG为jasonwan的log是我自己加的,编译验证,得到如下log

发动app,进入MainActivity
03-08 17:20:29.965  4069  4069 D jasonwan: makeApplication: mApplication=null, pid=4069
//创立application目标,地址为@c2f8311,当时进程id为4069
03-08 17:20:29.967  4069  4069 D jasonwan: create application, app=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307, pid=4069
03-08 17:20:29.988  4069  4069 D jasonwan: execute app onCreate(), app=:com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307, pid=4069
03-08 17:20:29.989  4069  4069 D jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307, pid=4069
03-08 17:20:36.614  4069  4069 D jasonwan: makeApplication: mApplication=com.jason.demo0307.DemoApplication@c2f8311, pid=4069
点击MainActivity,跳转到TwoActivity
03-08 17:20:39.686  4116  4116 D jasonwan: makeApplication: mApplication=null, pid=4116
//创立application目标,地址为@c2f8311,当时进程id为4116
03-08 17:20:39.687  4116  4116 D jasonwan: create application, app=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote2, pid=4116
03-08 17:20:39.688  4116  4116 D jasonwan: execute app onCreate(), app=:com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote2, pid=4116
03-08 17:20:39.688  4116  4116 D jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote2, pid=4116
03-08 17:20:39.733  4116  4116 D jasonwan: makeApplication: mApplication=com.jason.demo0307.DemoApplication@c2f8311, pid=4116
点击TwoActivity,跳转到ThreeActivity
03-08 17:20:41.473  4147  4147 D jasonwan: makeApplication: mApplication=null, pid=4147
//创立application目标,地址为@c2f8311,当时进程id为4147
03-08 17:20:41.475  4147  4147 D jasonwan: create application, app=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote3, pid=4147
03-08 17:20:41.475  4147  4147 D jasonwan: execute app onCreate(), app=:com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote3, pid=4147
03-08 17:20:41.476  4147  4147 D jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote3, pid=4147
03-08 17:20:41.519  4147  4147 D jasonwan: makeApplication: mApplication=com.jason.demo0307.DemoApplication@c2f8311, pid=4147
点击ThreeActivity,跳转到FourActivity
03-08 17:20:42.966  4174  4174 D jasonwan: makeApplication: mApplication=null, pid=4174
//创立application目标,地址为@c2f8311,当时进程id为4174
03-08 17:20:42.968  4174  4174 D jasonwan: create application, app=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote4, pid=4174
03-08 17:20:42.969  4174  4174 D jasonwan: execute app onCreate(), app=:com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote4, pid=4174
03-08 17:20:42.969  4174  4174 D jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote4, pid=4174
03-08 17:20:43.015  4174  4174 D jasonwan: makeApplication: mApplication=com.jason.demo0307.DemoApplication@c2f8311, pid=4174
点击FourActivity,跳转到FiveActivity
03-08 17:20:44.426  4202  4202 D jasonwan: makeApplication: mApplication=null, pid=4202
//创立application目标,地址为@c2f8311,当时进程id为4202
03-08 17:20:44.428  4202  4202 D jasonwan: create application, app=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote5, pid=4202
03-08 17:20:44.429  4202  4202 D jasonwan: execute app onCreate(), app=:com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote5, pid=4202
03-08 17:20:44.430  4202  4202 D jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@c2f8311, processName=com.jason.demo0307:remote5, pid=4202
03-08 17:20:44.473  4202  4202 D jasonwan: makeApplication: mApplication=com.jason.demo0307.DemoApplication@c2f8311, pid=4202

成果很震惊,咱们在5个进程中创立的application目标,地址均为@c2f8311,也便是至始至终创立的都是同一个Application目标,那么上面的定论2显然并不建立,仅仅测验的偶然性导致的。

可真的是这姿态的吗,这也太颠覆我的三观了,为此我跟群友评论了这个问题:

不同进程中的多个目标,内存地址相同,是否代表这些目标都是同一个目标?

群友的主意是,java中获取的都是虚拟内存地址,虚拟内存地址相同,不代表是同一个目标,有必要物理内存地址相同,才表明是同一块内存空间,也就意味着是同一个目标,物理内存地址和虚拟内存地址存在一个映射关系,一起给出了java中获取物理内存地址的办法Android获取目标地址,主要是使用Unsafe这个类来操作,这个类有一个作用便是直接访问体系内存资源,详细描述见Java中的魔法类-Unsafe,因为这种操作是不安全的,所以被标为了私有,但咱们能够经过反射去调用此API, 然后我又去请教了部门搞寄存器的大佬,大佬肯定了群友的主意,所以我添加代码,尝试获取目标的物理内存地址,看看是否相同

public class DemoApplication extends Application {
    public static final String TAG = "jasonwan";
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "DemoApplication=" + this + ", address=" + addressOf(this) + ", pid=" + Process.myPid());
    }
    //获取目标的实在物理地址
    public static long addressOf(Object o) {
        Object[] array = new Object[]{o};
        long objectAddress = -1;
        try {
            Class cls = Class.forName("sun.misc.Unsafe");
            Field field = cls.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            Object unsafe = field.get(null);
            Class unsafeCls = unsafe.getClass();
            Method arrayBaseOffset = unsafeCls.getMethod("arrayBaseOffset", Object.class.getClass());
            int baseOffset = (int) arrayBaseOffset.invoke(unsafe, Object[].class);
            Method size = unsafeCls.getMethod("addressSize");
            int addressSize = (int) size.invoke(unsafe);
            switch (addressSize) {
                case 4:
                    Method getInt = unsafeCls.getMethod("getInt", Object.class, long.class);
                    objectAddress = (int) getInt.invoke(unsafe, array, baseOffset);
                    break;
                case 8:
                    Method getLong = unsafeCls.getMethod("getLong", Object.class, long.class);
                    objectAddress = (long) getLong.invoke(unsafe, array, baseOffset);
                    break;
                default:
                    throw new Error("unsupported address size: " + addressSize);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objectAddress;
    }
}

运转后得到如下日志

2023-03-10 11:01:54.043 6535-6535/com.jason.demo0307 D/jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@930d275, address=8050489105119022792, pid=6535
2023-03-10 11:02:22.610 6579-6579/com.jason.demo0307 D/jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@331b3b9, address=8050489105119027136, pid=6579
2023-03-10 11:02:36.369 6617-6617/com.jason.demo0307 D/jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@331b3b9, address=8050489105119029912, pid=6617
2023-03-10 11:02:39.244 6654-6654/com.jason.demo0307 D/jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@331b3b9, address=8050489105119032760, pid=6654
2023-03-10 11:02:40.841 6692-6692/com.jason.demo0307 D/jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@331b3b9, address=8050489105119036016, pid=6692
2023-03-10 11:02:52.429 6729-6729/com.jason.demo0307 D/jasonwan: DemoApplication=com.jason.demo0307.DemoApplication@331b3b9, address=8050489105119038720, pid=6729

能够看到,虽然Application的虚拟内存地址相同,都是331b3b9,但它们的实在物理地址却不同,至此,咱们能够得出终究定论

  • 单进程,创立1个application目标,履行一次onCreate()办法
  • 多进程(N),创立N个application目标,履行N次onCreate()办法