0

我正在尝试使用非零 arg 构造函数(基于 Dagger 的构造函数依赖注入)实现自定义AppComponentFactory来创建我的Application和实例,但我的应用程序无法启动,因为我的自定义子类型没有零 arg 构造函数,并且堆栈跟踪暗示我的没有使用自定义。ActivityApplicationAppComponentFactory

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)
4

1 回答 1

0

在我的情况下,这是由于未在清单中使用完全限定的类名造成的。看来,当您使用属性的简写.ClassName语法时,android:appComponentFactory系统会预先添加 applicationId 而不是清单元素中声明的包。为了让系统识别我CustomAppComponentFactory,我必须从以下更改我的清单:

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

对此:

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

请注意,我还必须添加tools:replace="appComponentFactory"application清单中的元素以使所有这些工作,但是有一个关于清单合并错误的明确错误消息androidx.core.app.CoreComponentFactory,建议添加替换属性。

于 2020-04-04T02:45:51.803 回答