自定义 AppComponentFactory 实例化应用失败

Custom AppComponentFactory Fails to Instantiate Application

我正在尝试实现自定义 AppComponentFactory 以使用非零 arg 构造函数(基于 Dagger 的构造函数依赖注入)创建我的 ApplicationActivity 实例,但我的应用程序失败启动是因为我的自定义 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.access00(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.access00(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) 

UPDATE: 经进一步检查,Logcat 中出现另一个错误,显示系统未能找到我的自定义 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.access00(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)

在我的例子中,这是由于未在清单中使用完全限定的 class 名称造成的。看起来,当您对 android:appComponentFactory 属性使用 shorthand .ClassName 语法时,系统会在前面添加 applicationId 而不是清单元素中声明的包。为了让系统识别我的 CustomAppComponentFactory 我必须将清单更改为:

<application
    android:appComponentFactory=".CustomAppComponentFactory"
    ....
    >

对此:

<application
    android:appComponentFactory="com.domain.app.CustomAppComponentFactory"
    ....
    >

请注意,我还必须将 tools:replace="appComponentFactory" 添加到清单中的 application 元素才能完成所有这些工作,但是有一条关于 [=17 的清单合并错误的明确错误消息=] 建议添加替换属性。