AndroidX: AndroidX.Core.Content.FileProvider Fails to Instantiate with Multi-Dex On

Xamarin.Android Version (eg: 6.0):

Microsoft Visual Studio Enterprise 2019 Version 16.7.2 VisualStudio.16.Release/16.7.2+30413.136 Xamarin 16.7.000.440 (d16-7@358f3c6) Xamarin.Android SDK 11.0.2.0 (d16-7/025fde9)

Operating System & Version (eg: Mac OSX 10.11):

Windows 10 1909 18363.778

Support Libraries Version (eg: 23.3.0):

Xamarin.AndroidX.Core 1.3.0.1

Describe your Issue:

When the Multi-Dex option is turned on and a file provider is defined in the manifest file, the app crashes upon startup.

Steps to Reproduce (with link to sample solution if possible):

Run the AndroidX app provided in the attached package.

Include any relevant Exception Stack traces, build logs, adb logs:

Java.Lang.RuntimeException: 
'Unable to get provider androidx.core.content.FileProvider: 
java.lang.InstantiationException: java.lang.Class<androidx.core.content.FileProvider> cannot be instantiated'

AndroidX.zip

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

I had the same initialization error. I changed the Code Shrinker property in Android Project Property from ProGuard and R8 to EMPTY. This solved the issue. Just remove the ProGuard property and it works. I will try to find another workaround later but for now, this is fine with me.

@moljac , I am confused why you closed the issue when you were able to replicate it but didn’t resolve it fully

Because issue is not caused by bindings itself.

I have proven that with proguard rules there is completely different error (No Activity) and I did not dive in deeper.

The class was bound (surfaced) and now additional tools come into play:

  • multidex
  • linker/optimizer (R8) which can be overly agressive
  • managed linker

I would suggest to open new issue in xamarin-android repo, with repro sample that uses my proguard rule and explanation, so the team can test with and without proguard/R8 rules.

I hope this reduces confusion a bit. It is not simple problem.

Repro succeded:

multidex -on => crash

multidex off => OK

[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: com.companyname.androidx, PID: 18912
[AndroidRuntime] java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.InstantiationException: java.lang.Class<androidx.core.content.FileProvider> cannot be instantiated
[AndroidRuntime] 	at android.app.ActivityThread.installProvider(ActivityThread.java:6396)
[AndroidRuntime] 	at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
[AndroidRuntime] 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
[AndroidRuntime] 	at android.app.ActivityThread.access$1100(ActivityThread.java:199)
[AndroidRuntime] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
[AndroidRuntime] 	at android.os.Handler.dispatchMessage(Handler.java:106)
[AndroidRuntime] 	at android.os.Looper.loop(Looper.java:193)
[AndroidRuntime] 	at android.app.ActivityThread.main(ActivityThread.java:6669)
[AndroidRuntime] 	at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
[AndroidRuntime] Caused by: java.lang.InstantiationException: java.lang.Class<androidx.core.content.FileProvider> cannot be instantiated
[AndroidRuntime] 	at java.lang.Class.newInstance(Native Method)
[AndroidRuntime] 	at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
[AndroidRuntime] 	at android.app.ActivityThread.installProvider(ActivityThread.java:6380)
[AndroidRuntime] 	... 10 more