Tinker ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context) = false 原因

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_18824545/article/details/83018548

最近在集成tinker,集成到最后一步,

	TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), path);
	protected int patchCheck(String path, String patchMd5) {
        Tinker manager = Tinker.with(context);
        //check SharePreferences also
        if (!manager.isTinkerEnabled() || !ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)) {
            return ShareConstants.ERROR_PATCH_DISABLE;
        }
        File file = new File(path);

        if (!SharePatchFileUtil.isLegalFile(file)) {
            return ShareConstants.ERROR_PATCH_NOTEXIST;
        }

        //patch service can not send request
        if (manager.isPatchProcess()) {
            return ShareConstants.ERROR_PATCH_INSERVICE;
        }

        //if the patch service is running, pending
        if (TinkerServiceInternals.isTinkerPatchServiceRunning(context)) {
            return ShareConstants.ERROR_PATCH_RUNNING;
        }
        if (ShareTinkerInternals.isVmJit()) {
            return ShareConstants.ERROR_PATCH_JIT;
        }

        Tinker tinker = Tinker.with(context);

        if (tinker.isTinkerLoaded()) {
            TinkerLoadResult tinkerLoadResult = tinker.getTinkerLoadResultIfPresent();
            if (tinkerLoadResult != null && !tinkerLoadResult.useInterpretMode) {
                String currentVersion = tinkerLoadResult.currentVersion;
                if (patchMd5.equals(currentVersion)) {
                    return ShareConstants.ERROR_PATCH_ALREADY_APPLY;
                }
            }
        }

        if (!UpgradePatchRetry.getInstance(context).onPatchListenerCheck(patchMd5)) {
            return ShareConstants.ERROR_PATCH_RETRY_COUNT_LIMIT;
        }

        return ShareConstants.ERROR_PATCH_OK;
    }

执行到

	if (!manager.isTinkerEnabled() || !ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)) {
            return ShareConstants.ERROR_PATCH_DISABLE;
    }

ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)总是返回false,导致patch失败,debug发现是因为context=null。

怎么可能呢,我getApplicationContext()方法是这样写的

	private static Context getApplicationContext() {
        if (mAppLike != null) {
            return mAppLike.getApplication().getApplicationContext();
        }
        return null;
    }

debug看了mAppLike不为null,getApplication也不为null。搞了半天,就只不知道问题在哪里。就回去重新查看了一下配置文件,是不是哪里配置错了,也没什么问题。

就又回到这个方法里面继续debug,这回把整个

mAppLike.getApplication().getApplicationContext()

查看了一下,发现是null的!!!!! why???mAppLike.getApplication()不为空,为什么getApplicationContext是null的呢,最后,我把getApplicationContext()去掉了,可以顺利更新了。

猜你喜欢

转载自blog.csdn.net/qq_18824545/article/details/83018548