返回顶部
首页 > 资讯 > 精选 >Didn‘t find class “androidx.core.app.CoreComponentFactory“核心库中类加载失败
  • 369
分享到

Didn‘t find class “androidx.core.app.CoreComponentFactory“核心库中类加载失败

androidxjavaandroid 2023-08-18 10:08:16 369人浏览 薄情痞子
摘要

开发过程中遇到一个问题,是Bugly上报的崩溃,应用首次安装第一次打开时出现崩溃,日志大致信息报错如下: 03-24 14:20:58.443 13845 13845 E LoadedApk: Unable to instantiate a

开发过程中遇到一个问题,是Bugly上报的崩溃,应用首次安装第一次打开时出现崩溃,日志大致信息报错如下:

03-24 14:20:58.443 13845 13845 E LoadedApk: Unable to instantiate appComponentFactory03-24 14:20:58.443 13845 13845 E LoadedApk: java.lang.ClassNotFoundException: Didn't find class "Androidx.core.app.CoreComponentFactory" on path: DexPathList[[zip file "/data/app/*******-9uPTAyghm4ueO6sjsBeCgA==/base.apk"],nativeLibraryDirectories=[/data/app/*******-9uPTAyghm4ueO6sJsBeCgA==/lib/arm64, /data/app/*******-9uPTAyghm4ueO6sJsBeCgA==/base.apk!/lib/arm64-v8a, /system/lib64]]03-24 14:20:58.443 13845 13845 E LoadedApk: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)03-24 14:20:58.443 13845 13845 E LoadedApk: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)03-24 14:20:58.443 13845 13845 E LoadedApk: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)03-24 14:20:58.443 13845 13845 E LoadedApk: at android.app.LoadedApk.createAppFactory(LoadedApk.java:226)

分析报错日志,我们可以看到是核心库加载失败,由BaseDexClassLoader抛出异常,这里我贴出BaseDexClassLoader#findClass(String name)方法的源码,感兴趣的可以看看。

 @Override    protected Class findClass(String name) throws ClassNotFoundException {        // First, check whether the class is present in our shared libraries.        if (sharedLibraryLoaders != null) {            for (ClassLoader loader : sharedLibraryLoaders) {                try {                    return loader.loadClass(name);                } catch (ClassNotFoundException ignored) {                }            }        }        // Check whether the class in question is present in the dexPath that        // this classloader operates on.        List suppressedExceptions = new ArrayList();        Class c = pathList.findClass(name, suppressedExceptions);        if (c != null) {            return c;        }        // Now, check whether the class is present in the "after" shared libraries.        if (sharedLibraryLoadersAfter != null) {            for (ClassLoader loader : sharedLibraryLoadersAfter) {                try {                    return loader.loadClass(name);                } catch (ClassNotFoundException ignored) {                }            }        }        if (c == null) {            ClassNotFoundException cnfe = new ClassNotFoundException(                    "Didn't find class \"" + name + "\" on path: " + pathList);            for (Throwable t : suppressedExceptions) {                cnfe.addSuppressed(t);            }            throw cnfe;        }        return c;    }

引起该错误的原因与压缩混淆有关,高版本的Gradle打包默认采用R8进行压缩,相关概念和原理可自行搜索,我这边的解决方案是:既然启动时没有加载到该类,那么我们就设置先加载该类,加载完毕之后再加载其他的类,因此我们需要做一些配置来解决该问题:

buildscript {    ext {        //R8混淆开启之后解决APP打开类加载时找不到类的问题,指定文件来放置启动时需要优先加载的类        MaiNDEXLIST = 'maindexlist.txt'    }

项目路径下下创建maindexlist.txt文件,并将报错的类文件的路径按如下方式加入文件中

androidx/core/app/CoreComponentFactory.classandroidx/coordinatorlayout/widget/CoordinatorLayout.class

在app模块对应的build.gradle文件的buildTypes闭包下的debug和realease类型下添加
multiDexKeepFile file(rootProject.ext.MAINDEXLIST)

buildTypes {    debug {        ......        multiDexKeepFile file(rootProject.ext.MAINDEXLIST)    }    release {        ......        multiDexKeepFile file(rootProject.ext.MAINDEXLIST)    }}

以上配置即可解决该问题。

来源地址:https://blog.csdn.net/weixin_42643321/article/details/129813132

--结束END--

本文标题: Didn‘t find class “androidx.core.app.CoreComponentFactory“核心库中类加载失败

本文链接: https://lsjlt.com/news/373839.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作