{"record":{"id":"d3e3f90a6a087979","repo":"Blankj/AndroidUtilCode","slug":"reflect-failed","errorCode":null,"errorMessage":"reflect failed.","messagePattern":"reflect failed\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"critical","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java","lineNumber":72,"sourceCode":"            return;\n        }\n        if (sApp.equals(app)) return;\n        UtilsBridge.unInit(sApp);\n        sApp = app;\n        UtilsBridge.init(sApp);\n    }\n\n    /**\n     * Return the Application object.\n     * <p>Main process get app by UtilsFileProvider,\n     * and other process get app by reflect.</p>\n     *\n     * @return the Application object\n     */\n    public static Application getApp() {\n        if (sApp != null) return sApp;\n        init(UtilsBridge.getApplicationByReflect());\n        if (sApp == null) throw new NullPointerException(\"reflect failed.\");\n        Log.i(\"Utils\", UtilsBridge.getCurrentProcessName() + \" reflect app success.\");\n        return sApp;\n    }\n\n    ///////////////////////////////////////////////////////////////////////////\n    // interface\n    ///////////////////////////////////////////////////////////////////////////\n\n    public abstract static class Task<Result> extends ThreadUtils.SimpleTask<Result> {\n\n        private Consumer<Result> mConsumer;\n\n        public Task(final Consumer<Result> consumer) {\n            mConsumer = consumer;\n        }\n\n        @Override\n        public void onSuccess(Result result) {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java#L54-L90","documentation":"Utils.getApp() returns the cached Application (sApp); if it is null, it attempts to recover via UtilsBridge.getApplicationByReflect() and re-init. If after that sApp is still null, it throws NullPointerException(\"reflect failed.\"). This means the library was never initialized AND reflection could not locate the Application — a fatal, unrecoverable library state.","triggerScenarios":"Calling any util that internally calls Utils.getApp() (e.g. ScreenUtils.getScreenWidth) before Utils.init() was invoked, in a context where reflection cannot find the Application (e.g. unit tests running on the JVM without an Android runtime, multi-process setups without UtilsFileProvider, or proguard/R8 stripping the Application class metadata).","commonSituations":"Running pure JUnit unit tests (no Robolectric/Android manifest) so reflection finds no ActivityThread.currentApplication(); forgetting Utils.init(this) in Application.onCreate; obfuscation stripping the ActivityThread access path; instantiating utils in a ContentProvider that runs before Application.onCreate on cold start.","solutions":["Call Utils.init(application) explicitly in your Application.onCreate() before using any util.","For unit tests on the JVM, use Robolectric or mock Utils.getApp() rather than relying on reflection.","Keep the library's UtilsFileProvider registered in the manifest so init happens automatically on cold start, or ensure your ContentProvider init does not race ahead of Application creation.","Add proguard/R8 keep rules for the Application class and the ActivityThread reflection path so getApplicationByReflect() succeeds in release builds."],"exampleFix":"// before\n// Application.onCreate does not init the library\nint w = ScreenUtils.getScreenWidth(); // -> Utils.getApp() -> reflect failed.\n\n// after\npublic class App extends Application {\n  @Override public void onCreate() {\n    super.onCreate();\n    Utils.init(this);\n  }\n}","handlingStrategy":"validation","validationCode":"// Guard any code path that will call Utils.getApp().\npublic static Application requireApp() {\n  Application app = Utils.getApp(); // will throw if reflect fails\n  return app;\n}\n// Better: ensure init happened before first use.\nif (Utils.getApp() == null) {\n  // fallback only if you intentionally control init timing\n}","typeGuard":null,"tryCatchPattern":"try {\n  Application app = Utils.getApp();\n} catch (NullPointerException e) {\n  // reflect failed: report init/r8/manifest problem; do NOT silently continue\n  throw new IllegalStateException(\"AndroidUtilCode not initialized\", e);\n}","preventionTips":["Always call Utils.init(this) in Application.onCreate before using any util.","Register UtilsFileProvider in the manifest for auto-init on cold start.","Add ProGuard/R8 keep rules for the Application class and ActivityThread reflection path.","In unit tests, use Robolectric or mock Utils.getApp(); never rely on reflection on a bare JVM.","Do not use util classes inside ContentProviders that run before Application.onCreate without ensuring init."],"tags":["android","init","reflection","application","lifecycle"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}