tinker icon indicating copy to clipboard operation
tinker copied to clipboard

SampleApplication的context问题

Open zzwwws opened this issue 10 years ago • 15 comments

我注意到在1.6.1版本中有以下改动:

SampleApplicationContext.context = getApplication().getApplicationContext() 这一段改成:
SampleApplicationContext.context = getApplication();

之前的context是null,现在context虽然不是null,但context.getApplicationContext是null,这可能在一些第三方的库中出错,比如Umeng 推送 @shwenzhang 帮忙看一下

zzwwws avatar Sep 27 '16 08:09 zzwwws

不会是空阿,这个就是系统的application对象。

shwenzhang avatar Sep 27 '16 08:09 shwenzhang

你可以调试下,在onBaseContextAttached域内还是null image

zzwwws avatar Sep 27 '16 08:09 zzwwws

我猜测是不是和这个SampleApplication是动态生成的有关

zzwwws avatar Sep 27 '16 08:09 zzwwws

应该没关系,我打印出来不是空的。你打印出来看看

shwenzhang avatar Sep 27 '16 08:09 shwenzhang

image

zzwwws avatar Sep 27 '16 08:09 zzwwws

这里没太大关系的,我只是直接拿到原来的application而已

shwenzhang avatar Sep 27 '16 09:09 shwenzhang

大概知道为什么getApplicationContext为null了

    @Override
    public Context getApplicationContext() {
        return (mPackageInfo != null) ?
                mPackageInfo.getApplication() : mMainThread.getApplication();
    }

这里的mPackageInfo是LoadedApk的实例,它里面的mApplication是在makeApplication方法里初始化的,但是在TinkerApplication attach的时候,SampleApplication还没被生成,所以,makeApplication会抛出Unable to instantiate application的异常,导致mApplication没被初始化。

    public Application makeApplication(boolean forceDefaultAppClass,
                                       Instrumentation instrumentation) {
        if (mApplication != null) {
            return mApplication;
        }

        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")) {
                initializeJavaContextClassLoader();
            }
            ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
            app = mActivityThread.mInstrumentation.newApplication(
                    cl, appClass, appContext);
            appContext.setOuterContext(app);
        } catch (Exception e) {
            if (!mActivityThread.mInstrumentation.onException(app, e)) {
                throw new RuntimeException(
                        "Unable to instantiate application " + appClass
                                + ": " + e.toString(), e);
            }
        }
        mActivityThread.mAllApplications.add(app);
        mApplication = app;

        if (instrumentation != null) {
            try {
                instrumentation.callApplicationOnCreate(app);
            } catch (Exception e) {
                if (!instrumentation.onException(app, e)) {
                    throw new RuntimeException(
                            "Unable to create application " + app.getClass().getName()
                                    + ": " + e.toString(), e);
                }
            }
        }

        // Rewrite the R 'constants' for all library apks.
        SparseArray<String> packageIdentifiers = getAssets(mActivityThread)
                .getAssignedPackageIdentifiers();
        final int N = packageIdentifiers.size();
        for (int i = 0; i < N; i++) {
            final int id = packageIdentifiers.keyAt(i);
            if (id == 0x01 || id == 0x7f) {
                continue;
            }

            rewriteRValues(getClassLoader(), packageIdentifiers.valueAt(i), id);
        }

        return app;
    }

所以在上面提到的onBaseContextAttached方法内,context.getApplicationContext()是空的。

zzwwws avatar Sep 27 '16 10:09 zzwwws

这个有少量机器的确会,但是即使你不使用Tinker也是一样的情况的

shwenzhang avatar Sep 27 '16 10:09 shwenzhang

这里是我理解错了,其实onBaseContextAttached里本来就不应该放第三方库初始化的东西,因为这时候application attach都还没完成,mApplication还是空的。应该放到onCreate里去,所以我建议SampleApplicationLike里Override onCreate方法,把SampleApplicationContext.context放在onCreate里去。

zzwwws avatar Sep 27 '16 10:09 zzwwws

没问题,现在我只是把application的引用传进去,后面再取就没问题

shwenzhang avatar Sep 27 '16 10:09 shwenzhang

@zzwwws 我今天也遇到这个问题。在attach 中获取到的context也是空·,

15189611 avatar Sep 27 '16 15:09 15189611

@zzwwws mLoadedApk = ContextImpl.getImpl(context).mPackageInfo; 是在Attach中初始化的

15189611 avatar Sep 27 '16 15:09 15189611

@zzwwws 分析不错,谢谢

15189611 avatar Sep 27 '16 15:09 15189611

#36

orionoxe avatar Sep 28 '16 02:09 orionoxe

模拟器上报这个错

nysin avatar Aug 17 '18 15:08 nysin