我正在尝试使用非零 arg 构造函数(基于 Dagger 的构造函数依赖注入)实现自定义AppComponentFactory
来创建我的Application
和实例,但我的应用程序无法启动,因为我的自定义子类型没有零 arg 构造函数,并且堆栈跟踪暗示我的没有使用自定义。Activity
Application
AppComponentFactory
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.domain.app, PID: 32670
java.lang.RuntimeException: Unable to instantiate application com.domain.app.CustomApplication: java.lang.InstantiationException: java.lang.Class<com.domain.app.CustomApplication> has no zero argument constructor
at android.app.LoadedApk.makeApplication(LoadedApk.java:1226)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431)
at android.app.ActivityThread.access$1300(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.InstantiationException: java.lang.Class<com.domain.app.CustomApplication> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
at android.app.Instrumentation.newApplication(Instrumentation.java:1155)
at android.app.LoadedApk.makeApplication(LoadedApk.java:1218)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431)
at android.app.ActivityThread.access$1300(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
更新:经过进一步检查,Logcat 中出现另一个错误,表明系统未能找到我的 custom AppComponentFactory
。
E/LoadedApk: Unable to instantiate appComponentFactory
java.lang.ClassNotFoundException: Didn't find class "com.domain.app.appname.CustomAppComponentFactory" on path: DexPathList[[zip file "/data/app/com.domain.app.appname-8VLkpKFTUtW8k-xEHX-oMQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.domain.app.appname-8VLkpKFTUtW8k-xEHX-oMQ==/lib/arm64, /system/lib64, /system/product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.LoadedApk.createAppFactory(LoadedApk.java:256)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:855)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:950)
at android.app.LoadedApk.getResources(LoadedApk.java:1188)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2462)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2454)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6343)
at android.app.ActivityThread.access$1300(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)