由于未找到FileProvider Class,因此在重新安装模拟器后应用程序崩溃

时间:2016-08-25 08:23:14

标签: android android-studio android-emulator android-fileprovider

当我在模拟器上运行应用程序时,它总是在第一次尝试时运行。但是,当应用程序已经安装在模拟器上时,它通常会在应用程序启动时导致崩溃。 Android Studio 2.0开始出现此问题。 它不会出现在设备上,因此它不是那么重要,但只是烦人,因为我总是需要在安装新版本之前从模拟器中删除应用程序。

由于我完全不知道是什么导致了这个问题,在研究过程中无法找到类似的问题,我希望有人可以帮助我。

错误讯息:

08-25 09:55:35.023 4214-4214/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mls.Search.Abbott, PID: 4214
    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.mls.Search.Abbott-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mls.Search.Abbott-2/lib/x86, /data/app/com.mls.Search.Abbott-2/base.apk!/lib/x86, /vendor/lib, /system/lib]]
        at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
        at android.app.ActivityThread.-wrap1(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.mls.Search.Abbott-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mls.Search.Abbott-2/lib/x86, /data/app/com.mls.Search.Abbott-2/base.apk!/lib/x86, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at android.app.ActivityThread.installProvider(ActivityThread.java:5141)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688) 
        at android.app.ActivityThread.-wrap1(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[dex file "/data/data/com.mls.Search.Abbott/files/instant-run/dex/slice-slice_3-classes.dex"],nativeLibraryDirectories=[/data/app/com.mls.Search.Abbott-2/lib/x86, /data/app/com.mls.Search.Abbott-2/base.apk!/lib/x86, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
        at com.android.tools.fd.runtime.IncrementalClassLoader.findClass(IncrementalClassLoader.java:62)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
              ... 12 more
            Suppressed: java.lang.ClassNotFoundException: android.support.v4.content.FileProvider
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 13 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

清单中的文件提供程序:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

2 个答案:

答案 0 :(得分:0)

如果您在模拟器中使用Android 5.0或更低版本(api level 21), 然后将multidex支持库添加到您的项目中:

  1. 如果你的minSdkVersion设置为21或更高,你需要做的就是在你的模块级build.gradle文件中将multiDexEnabled设置为true,如下所示:

    android {
        defaultConfig {
            ...
            minSdkVersion 21 
            targetSdkVersion 26
            multiDexEnabled true
        }
        ...
    }
    
    1. 如果您的minSdkVersion设置为20或更低,则必须使用multidex支持库,如下所示:
    2. 修改模块级build.gradle文件以启用multidex并将multidex库添加为依赖项,如下所示:

      android {
          defaultConfig {
              ...
              minSdkVersion 15 
              targetSdkVersion 26
              multiDexEnabled true
          }
          ...
      }
      

      依赖{   编译'com.android.support:multidex:1.0.1' }

      看看 https://developer.android.com/studio/build/multidex.html#mdex-gradle

答案 1 :(得分:0)

如果您使用的是 androidx 库,请在 AndroidManifest.xml 中进行以下更改:

android:name="android.support.v4.content.FileProvider"

对此:

android:name="androidx.core.content.FileProvider"