Can't find getUriForFile method from FileProvider (Android)

I’m trying to update native sharing to use FileProvider. I’ve followed and checked multiple instructions from web. I’ve added provider tags to application-part of manifest and created provider paths xml file. In Unity I get the application context, authority string, File object and FileProvider class without problem:

AndroidJavaClass unity = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);

AndroidJavaObject activity = unity.GetStatic(“currentActivity”);

AndroidJavaObject context = activity.Call(“getApplicationContext”);

string auth = context.Call(“getPackageName”) + “.provider”;

AndroidJavaObject file = new AndroidJavaObject(“java.io.File”, imagePath);

AndroidJavaClass fileProvider = new AndroidJavaClass(“androidx.core.content.FileProvider”);

But as last step when trying to get the URI object:

uriObject = fileProvider.CallStatic(“getUriForFile”, context, auth, file);

I just get exception:

java.lang.NoSuchMethodError: no static method with name=‘getUriForFile’ signature=‘(Landroidx.multidex.MultiDexApplication;Ljava/lang/String;Ljava.io.File;)Ljava/lang/Object;’ in class Landroidx.core.content.FileProvider;

Like whole method doesn’t exist. I thought maybe first parameter is wrong type, can it create similar error? There’s been mixed information what it should be, I’ve also tried to give ‘activity’ directly as context for ‘getUriForFile’. But same error message follows with just different signature.

I couldn’t understand what could be wrong with your method call, as the call looks correct according to the official documentation.

What I can suggest is the Unity Native Share Plugin. It’s a great plugin and I’ve used it successfully.

I made a post about the plugin and an extension I made: Gamedev Utility Belt: Unity Native Share Plugin | Giacomelli | C# programmer and Unity 3D gamedev

Well, it’s quite easy to explain the issue. Unity’s wrapper classes do all the work for you when it comes to the JNI interface. However it builds the signature string for the method based on the marshalled / converted parameter types you pass to the “CallStatic” method. You pass 3 parameters and their types are

Landroidx.multidex.MultiDexApplication;
Ljava/lang/String;
Ljava.io.File;

However the method requires a Landroid.content.Context as first parameter. If you look up the actual class instance you’re passing you will notice that it’s not even derived from the Context class and therefore not compatible at all. So there simply is no method that can take the parameters you passed.

I haven’t really used Android a lot so I can’t really tell what kind of Context the method expects. However since the Activity class is actually derived from Context you may simply pass the main activity / current activity as context.