一、错误描述
最近把一个旧的项目从Eclipse迁移到Android Studio的时候,做适配26,遇到如下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.XXl.XXl, PID: 10251
java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.XXl.XXl-pBXoeCsw74hjKSQpT2YeKA==/base.apk"],nativeLibraryDirectories=[/data/app/com.XXl.XXl-pBXoeCsw74hjKSQpT2YeKA==/lib/x86, /system/lib, /vendor/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.XXl.XXl-pBXoeCsw74hjKSQpT2YeKA==/base.apk"],nativeLibraryDirectories=[/data/app/com.XXl.XXl-pBXoeCsw74hjKSQpT2YeKA==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.ActivityThread.installProvider(ActivityThread.java:622
二、原因分析
原因的英文如下:
Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException
就是android 的support包,有个类叫FileProvider没有找到。
三、网上的解决办法
网上搜索了一下,很多介绍的解决办法如下(对我的问题不适用):
可能是添加MultiDex分包,但未初始化的原因,在Application中重写attachBaseContext函数,对MultiDex初始化即可。
1)首先修改Gradle配置文件,启用MultiDex并包含MultiDex支持:
defaultConfig {
multiDexEnabled true
}
2)修改BaseApplication,重写attachBaseContext方法
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(base);
}
四、我的解决办法
根据原因分析,可以知道,缺少android 的support包的类FileProvider,那我们添加这个包就可以了。添加步骤如下:
![b400a8aebd0505b63cf55bc5d07e59bd.png](https://i-blog.csdnimg.cn/blog_migrate/f2c282f71ead6a5bd0f935317caaf572.jpeg)
一、菜单
![679780facc69bc26d83ce72a1ab874ba.png](https://i-blog.csdnimg.cn/blog_migrate/4e3df9a9170b873dade448f7a721aa74.jpeg)
二、选择依赖
![aca361471badc5acbf7f731e508d4eea.png](https://i-blog.csdnimg.cn/blog_migrate/4481e15807dd5c66a8c50fdcbc99378e.jpeg)
三、点击添加库
![e572b80dc6d85bcfce9e6830855cbb27.png](https://i-blog.csdnimg.cn/blog_migrate/95679bf6038e83e12712ea04b345cbc1.jpeg)
四,选择support包