android.support.v4.content.FileProvider class not found in android Kitkat
Asked Answered
S

9

11

I am using FileProvider in my app. As usual I declared <Provider> tag in AndroidManifest.xml file as below.

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.jk.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

When I run it on android device which has lollipop version it works fine. when I try it on kitkat version it shows following errors:

FATAL EXCEPTION: main
              Process: com.jk.android.perfectphotoeditor2018, PID: 24992
              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.jk.android.perfectphotoeditor2018-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.jk.android.perfectphotoeditor2018-2, /system/lib]]
                  at android.app.ActivityThread.installProvider(ActivityThread.java:5071)
                  at android.app.ActivityThread.installContentProviders(ActivityThread.java:4648)
                  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4588)
                  at android.app.ActivityThread.access$1500(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5299)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.jk.android.perfectphotoeditor2018-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.jk.android.perfectphotoeditor2018-2, /system/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
                  at android.app.ActivityThread.installProvider(ActivityThread.java:5056)
                  at android.app.ActivityThread.installContentProviders(ActivityThread.java:4648) 
                  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4588) 
                  at android.app.ActivityThread.access$1500(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5299) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748) 
                  at dalvik.system.NativeStart.main(Native Method)

I had tried many solutions like this but it doesn't work for me. so, help me for solve this problem.

build.gradle dependencies:

dependencies {
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
    testCompile 'junit:junit:4.12'
    /* Add the CSDK framework dependencies (Make sure these version numbers are correct) */
    //    compile 'com.aviary.android.feather.sdk:aviary-sdk:3.6.3'
    compile 'com.android.support:appcompat-v7:25.4.0'
    compile 'com.adobe.creativesdk.foundation:auth:0.9.1251'
    compile 'com.adobe.creativesdk:image:4.8.4'
    compile 'com.localytics.android:library:4.0.1'
    compile 'com.android.support:design:25.4.0'
    compile 'com.android.support:multidex:1.0.2'
    compile 'com.android.support:support-v4:25.4.0'
    compile 'com.intuit.sdp:sdp-android:1.0.4'
    compile 'com.github.bumptech.glide:glide:4.3.1'
    compile 'com.google.android.gms:play-services-ads:9.4.0'
    implementation 'com.android.support:support-v4:25.4.0'
}
Substantiate answered 19/12, 2017 at 13:31 Comment(12)
post your build.gradle file (dependencies section only) - also what is the complete stacktrace?Deserving
checkout edited question for dependencies i have used @DeservingSubstantiate
"when I try it on kitkat version it shows ClassnotfoundException ." -- please edit your question and post the entire Java stack trace.Tripalmitin
checkout again...i edited questions as you said @TripalmitinSubstantiate
Do you have multidex enabled? If so, you may need to take some steps to ensure that FileProvider is in the main DEX file.Tripalmitin
Yes, i have enabled multidex. which steps i should have to take? @TripalmitinSubstantiate
developer.android.com/studio/build/multidex.html#keepTripalmitin
so, i need to make 'multidex-config.txt' file in same directory as 'build.gradle' but i have two doubts.1) directory should be same as app level build.gradle or project level build.gradle? and 2) in 'multidex-config.txt' file i need to specify path of FileProvider class. so, what's the path of this file? is it 'android/support/v4/content/FileProvider'?@TripalmitinSubstantiate
i'd used 'android\support\v4\content\FileProvider.class' path and tried by putting this file in both build.gradle file directories alternatively but it doesn't work. is my file path wrong? @TripalmitinSubstantiate
I do not use multidex much, and so I have no idea, sorry.Tripalmitin
it's ok! No problem and thanks for helping!Substantiate
try this - https://mcmap.net/q/540471/-didn-39-t-find-class-quot-androidx-core-content-fileprovider-quotReifel
D
31

If you have migrated to AndroidX you have to change the name in AndroidManifest.xml to androidx.core.content.FileProvider

That was the crash in my case.

Dispatcher answered 7/4, 2019 at 13:30 Comment(0)
T
3

I have the same question, and this documentation solve my problem. https://developer.android.com/studio/build/multidexandroid multidex

before android 5.0, you must use follow ways to multidex:

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

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

and then modify application, select any one of three ways:

if you not implement application:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

if you have custom application, you can modify like this:

public class MyApplication extends MultiDexApplication { ... }

if you have replace base application, you can modify like this:

public class MyApplication extends SomeOtherApplication {
   @Override
   protected void attachBaseContext(Context base) {
      super.attachBaseContext(base);
      MultiDex.install(this);
   }
}

Hope this can solve you problem.

Taught answered 11/2, 2019 at 6:6 Comment(0)
F
3

The following code works for me (I've migrated to AndroidX):

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:ignore="WrongManifestParent">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
</provider>
Ferree answered 3/9, 2019 at 13:43 Comment(0)
R
2

in my project when set "multiDexEnabled" false, error fixed. go to manifest and add

android {

defaultConfig {
    multiDexEnabled false
}}

hope this is helpful

Roscoe answered 25/4, 2018 at 12:37 Comment(2)
you can delete it, also.Behalf
multiDexEnabled true is required to me, any other solutionSpermophile
S
2

Just delete the intermediates folder.

Click here for structure

Stockbreeder answered 27/9, 2018 at 6:0 Comment(0)
E
2

After thorough reading of Android Multidex documentation, I found that there is one configuration we generally miss which causes java.lang.NoClassDefFoundError. If you have done regular multidex configuration for 65K reference limit and and still unable to get rid of java.lang.NoClassDefFoundError then you must follow these steps.

  1. Create a file called multidex-config.txt at app level(same directory as the build.gradle file)
  2. Add following line (the absolute class name for which you are getting exception)

    android.support.v4.content.FileProvider
    
  3. Modify your build.gradle as follow

    android {
        buildTypes {
            release {
                multiDexKeepFile file('multidex-config.txt')
                ...
            }
        }
    } 
    

    Note : You can add it for specific build type or both by declaring it in defaultConfig.

What happens when you build a multidex enabled app :

The Android build tools construct a primary DEX file (classes.dex) and supporting DEX files (classes2.dex, classes3.dex, and so on) as needed. The build system then packages all DEX files into your APK.

At runtime, the multidex APIs use a special class loader to search all of the available DEX files for your methods (instead of searching only in the main classes.dex file).

Cause of Exception :

When building each DEX file for a multidex app, the build tools perform complex decision-making to determine which classes are needed in the primary DEX file so that your app can start successfully. If any class that's required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError.

This shouldn't happen for code that's accessed directly from your app code because the build tools recognize those code paths, but it can happen when the code paths are less visible such as when a library you use has complex dependencies. For example, if the code uses introspection or invocation of Java methods from native code, then those classes might not be recognized as required in the primary DEX file.

So if you receive java.lang.NoClassDefFoundError, then you must manually specify these additional classes as required in the primary DEX file by declaring them with the multiDexKeepFile (shown above) or the multiDexKeepProguard (refer documentation) property in your build type. If a class is matched in either the multiDexKeepFile or the multiDexKeepProguard file, then that class is added to the primary DEX file.

For complete documentation on multidex refer https://developer.android.com/studio/build/multidex

Endo answered 15/10, 2018 at 9:22 Comment(2)
This does not resolve the issue for me. Taking a closer look the exception asked for is a java.lang.ClassNotFoundException vs. java.lang.NoClassDefFoundError in this answer / on the multidex docs.Hagio
@Hagio Can you post the stacktrace?Endo
C
1

Click to AndroidManifest.xml look at the bottom of the files page you gonna see "Text" and "Merged Manifes" then click to "Merged Manifest" -> Search for "android.support.v4.content.FileProvider" then change it with "androidx.core.content.FileProvider"

Thats it, exact solution.

Confirmed answered 23/7, 2019 at 23:53 Comment(0)
G
1

change

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

to

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

tested and works for me

Gewgaw answered 9/5, 2021 at 18:6 Comment(0)
B
-2

FileProvider was introduced with Android API Level 22 which is Lollipop, that is why you did not face the ClassNotFoundException when testing on a Lollipop device. Android Kitkat does not have the FileProvider class and hence you are facing the exception. If you are looking to access files this post should help you out

Burse answered 19/12, 2017 at 13:36 Comment(1)
FileProvider is from the support libraries. It was added to the support libraries in version 22.1.0 of the libraries. It works back to at least Android 4.0.3.Tripalmitin

© 2022 - 2024 — McMap. All rights reserved.