{"record":{"id":"081a34f81f0c94cb","repo":"Tencent/matrix","slug":"you-must-init-matrix-sdk-first","errorCode":null,"errorMessage":"you must init Matrix sdk first","messagePattern":"you must init Matrix sdk first","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/Matrix.java","lineNumber":77,"sourceCode":"    }\n\n    public static Matrix init(Matrix matrix) {\n        if (matrix == null) {\n            throw new RuntimeException(\"Matrix init, Matrix should not be null.\");\n        }\n        synchronized (Matrix.class) {\n            if (sInstance == null) {\n                sInstance = matrix;\n            } else {\n                MatrixLog.e(TAG, \"Matrix instance is already set. this invoking will be ignored\");\n            }\n        }\n        return sInstance;\n    }\n\n    public static Matrix with() {\n        if (sInstance == null) {\n            throw new RuntimeException(\"you must init Matrix sdk first\");\n        }\n        return sInstance;\n    }\n\n    public void startAllPlugins() {\n        for (Plugin plugin : plugins) {\n            plugin.start();\n        }\n    }\n\n    public void stopAllPlugins() {\n        for (Plugin plugin : plugins) {\n            plugin.stop();\n        }\n    }\n\n    public void destroyAllPlugins() {\n        for (Plugin plugin : plugins) {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/Matrix.java#L59-L95","documentation":"Matrix.with() returns the installed Matrix singleton; if the SDK was never initialized (or init has not completed), sInstance is null and the library throws. This guards all subsequent plugin access against an uninitialized SDK.","triggerScenarios":"Calling Matrix.with() before Matrix.init(...), after init failed, or from a process (e.g. :push, webview process) where init was never executed.","commonSituations":"Accessing Matrix from a ContentProvider or multi-process code path that runs before Application.onCreate; forgetting Matrix.init in a new Application class; app crash-restart paths where init is skipped.","solutions":["Call Matrix.init(new Matrix.Builder(application)...build()) in Application.onCreate before any Matrix.with() usage.","Gate all Matrix.with() calls behind Matrix.isInstalled().","Ensure every process that uses Matrix runs its own initialization."],"exampleFix":"// before\nMatrix.with().startAllPlugins();\n// after\nif (Matrix.isInstalled()) {\n    Matrix.with().startAllPlugins();\n}","handlingStrategy":"type-guard","validationCode":"if (!Matrix.isInstalled()) {\n    Matrix.init(new Matrix.Builder(app).build());\n}","typeGuard":"boolean matrixReady() { return Matrix.isInstalled(); }","tryCatchPattern":"try {\n    Matrix.with().startAllPlugins();\n} catch (RuntimeException e) {\n    Log.e(TAG, \"Matrix not initialized\", e);\n}","preventionTips":["Call Matrix.init in Application.onCreate on every cold start","Initialize Matrix in every process that uses it","Check Matrix.isInstalled() before any Matrix.with() call"],"tags":["android","initialization","sdk-lifecycle"],"backgroundTag":"module-init-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}