86

I'm trying to move to migrate to androidx. I used the migration tool in Android Studio. When I do this I get the following stacktrace when I run my app.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.peerke.outdoorpuzzlegame.debug, PID: 10901
    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.peerke.outdoorpuzzlegame.debug-IBtFsngoLqc-cQb_hOO5wQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.peerke.outdoorpuzzlegame.debug-IBtFsngoLqc-cQb_hOO5wQ==/lib/x86, /system/lib]]
        at android.app.ActivityThread.installProvider(ActivityThread.java:6376)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5932)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5847)
        at android.app.ActivityThread.access$1000(ActivityThread.java:198)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6649)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.peerke.outdoorpuzzlegame.debug-IBtFsngoLqc-cQb_hOO5wQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.peerke.outdoorpuzzlegame.debug-IBtFsngoLqc-cQb_hOO5wQ==/lib/x86, /system/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.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
        at androidx.core.app.CoreComponentFactory.instantiateProvider(CoreComponentFactory.java:62)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6360)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5932) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5847) 
        at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6649) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 

The exception is correct. android.support.v4.content.FileProvider doesn't exist in my app. But androidx.core.content.FileProvider is included in my app. The big question is why does it want to load the old version of FileProvider?

Peter Fortuin
  • 5,041
  • 8
  • 41
  • 69

3 Answers3

211

Thanks to @CommonsWare

More explanation:

What to do, find the android.support.v4.FileProvider in your <provider> in AndroidManifest.xml.

Change it to androidx.core.content.FileProvider

Yosi Pramajaya
  • 3,895
  • 2
  • 12
  • 34
99

In manifiest.xml file simply change this

<provider
        android:name="android.support.v4.content.FileProvider"
      .....
</provider>

To this one

<provider
    android:name="androidx.core.content.FileProvider"
    ......
</provider>

Or Simply

  • Go to Refactor (Studio -> Menu -> Refactor)
  • Click the Migrate to AndroidX.
  • it's working.
Attaullah
  • 3,856
  • 3
  • 48
  • 63
70

why does it want to load the old version of FileProvider?

Based on the stack trace, perhaps you are still using the old package name in the <provider> element in the manifest.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I have provider in Manifest as - @CommonsWare – Arnold Brown Sep 18 '18 at 11:36
  • @CommonsWare ------ Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" and App Force closed – Arnold Brown Sep 18 '18 at 11:38
  • @ArnoldBrown: You are missing the dependency that has that class, apparently. See in your External Libraries list in Android Studio (bottom of the project tree) contains `com.android.support:support-compat` for some version. If it does not, add a dependency on that, for whatever version of the Support Library you are using. It you already have `com.android.support:support-compat` in External Libraries, but you are using multidex, you will need to configure multidex such that `FileProvider` is in the main DEX file. – CommonsWare Sep 18 '18 at 11:52
  • We don't have old package name in our manifest. We are still facing this issue – Nishant Shah Apr 15 '19 at 11:00