Custom AppComponentFactory无法实例化应用程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Custom AppComponentFactory无法实例化应用程序相关的知识,希望对你有一定的参考价值。
我正在尝试使用非零arg构造函数(基于Dagger的构造函数依赖注入)来实现自定义AppComponentFactory
以创建我的Application
和Activity
实例,但是我的应用无法启动,因为我的自定义Application
子类型没有零arg构造函数,并且堆栈跟踪表明未使用我的自定义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)
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.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)
在我的情况下,这是由于清单中未使用完全限定的类名引起的。看来,当对.ClassName
属性使用简写android:appComponentFactory
语法时,系统会预先加上applicationId而不是清单元素中声明的包。为了使系统识别我的CustomAppComponentFactory
,我必须从此更改清单:
<application
android:appComponentFactory=".CustomAppComponentFactory"
....
>
对此:
<application
android:appComponentFactory="com.domain.app.CustomAppComponentFactory"
....
>
[请注意,我还必须在清单中的tools:replace="appComponentFactory"
元素上添加application
才能完成所有这些工作,但是有一个明确的错误消息,涉及与androidx.core.app.CoreComponentFactory
的清单合并错误,建议添加replace属性。
以上是关于Custom AppComponentFactory无法实例化应用程序的主要内容,如果未能解决你的问题,请参考以下文章
salesforce Custom Setting VS Custom metadata type(CMT)
Introducing Custom API – The New Way of Creating Custom Actions in Dataverse
windows 分辨率任意定制;windows resolution custom - Custom Resolution Utility (CRU)
Dynamics CRM 2015/2016 Web API:Unbound Custom Action 和 Bound Custom Action