开发过程中遇到一个问题,是Bugly上报的崩溃,应用首次安装第一次打开时出现崩溃,日志大致信息报错如下:
03-24 14:20:58.443 13845 13845 E LoadedApk: Unable to instantiate appComponentFactory
03-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<Throwable> suppressedExceptions = new ArrayList<Throwable>();
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.class
androidx/coordinatorlayout/widget/CoordinatorLayout.class
在app模块对应的build.gradle文件的buildTypes闭包下的debug和realease类型下添加
multiDexKeepFile file(rootProject.ext.MAINDEXLIST)文章来源:https://www.toymoban.com/news/detail-589603.html
buildTypes {
debug {
......
multiDexKeepFile file(rootProject.ext.MAINDEXLIST)
}
release {
......
multiDexKeepFile file(rootProject.ext.MAINDEXLIST)
}
}
以上配置即可解决该问题。文章来源地址https://www.toymoban.com/news/detail-589603.html
到了这里,关于Didn‘t find class “androidx.core.app.CoreComponentFactory“核心库中类加载失败的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!