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

开发过程中遇到一个问题,是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)

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

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

猜你喜欢

转载自blog.csdn.net/weixin_42643321/article/details/129813132