Unable to get provider androidx.core.content.FileProvider ClassNotFoundException when trying to build an Android Plugin for Unity

Hks.Adohkd picture Hks.Adohkd · Dec 22, 2018 · Viewed 10.7k times · Source

I am trying to build an android plugin for unity3d that simply share a screenshot to another app via the FileProvider.
I migrated my project to androidx.
I have used androidx.core.content.FileProvider class.
My app crashes whenever I launch it, with the following Logcat:

2018-12-22 21:46:13.678 14755-14755/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anascompany.testandroidplugin, PID: 14755
java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk"],nativeLibraryDirectories=[/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/lib/x86, /data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk!/lib/x86, /system/lib]]
    at android.app.ActivityThread.installProvider(ActivityThread.java:6396)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
    at android.app.ActivityThread.access$1100(ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    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:858)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk"],nativeLibraryDirectories=[/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/lib/x86, /data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk!/lib/x86, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)

I have searched about the problem and tried the following:
1. MultiDex problem.
In order to include MultiDex easily in my project I changed the minimum SDK to API 23 Marshmallow 6.0.
Where the Android documentation said that to include MultiDex you need to add multiDexEnabled true in defaultConfig in the build.gradle file for the specified module.
Note: the documentation said that this is the only step required to include MultiDex if the minimum SDK is set to API 21 or higher.
2. wrong package problem (I guess that's the problem).
prior to androidx we used this package: com.android.support:support-v4 so according to this page you should replace the previous package with: androidx.legacy:legacy-support-v4:1.0.0 and also replaced android.support.v4.content.FileProvider with androidx.core.content.FileProvider.
here is my android manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anascompany.unity">
<application>
    <provider
        android:authorities="com.anascompany.unity.fileprovider"
        android:name="androidx.core.content.FileProvider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>
</application>


and here is my build.gradle file for my module:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    defaultConfig {
    minSdkVersion 23
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}

task copyPlugin(type: Copy){
    dependsOn assemble
    from ('build/outputs/aar')
    into ('../../Assets/Plugins/Android')
    include (project.name + '-release.aar')
}


Note: the target SDK is both in unity and android studio set to 28 "Android Pie".
Note: my question is different from this one because I have already included in the manifest androidx.core.content.FileProvider

Answer

Ahmer Afzal picture Ahmer Afzal · Jan 9, 2019

Change to

implementation 'androidx.core:core:1.0.1'

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