dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Crash when using FileProvider on Android in Release Mode: "Couldn't find meta-data for provider with authority" #20898

Open mlxyz opened 8 months ago

mlxyz commented 8 months ago

Description

This is the same issue as the one reported on Stackoverflow. I am opening this issue as the proposed solution (disabling linking) is not acceptable for me.

The app crashes using this code:

var fileUri = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileProvider", new Java.IO.File(filePath));
using (var intent = new Intent(Intent.ActionView))
{
    intent.SetDataAndType(fileUri, "text/plain");
    intent.AddFlags(ActivityFlags.NewTask);
    intent.AddFlags(ActivityFlags.GrantReadUriPermission);
    intent.PutExtra(Intent.ExtraNotUnknownSource, true);

    Platform.CurrentActivity.StartActivity(intent);
}

with this error message Java.Lang.IllegalArgumentException: Couldn't find meta-data for provider with authority *.fileProvider.

This only happens in Release mode, in Debug everything works. Most likely, this is related to trimming/linking.

Installed Workload Id Manifest Version Installation Source

maccatalyst 17.2.8004/8.0.100 VS 17.9.34414.90, VS 17.8.34601.278 ios 17.2.8004/8.0.100 VS 17.9.34414.90, VS 17.8.34601.278 maui-windows 8.0.6/8.0.100 VS 17.9.34414.90, VS 17.8.34601.278 android 34.0.52/8.0.100 VS 17.9.34414.90, VS 17.8.34601.278

Steps to Reproduce

  1. Create MAUI app and add the following code:
    
    var context = Android.App.Application.Context;
    var path = FileSystem.Current.CacheDirectory;
    var filePath = Path.Combine(path, "test.txt");
    File.AppendAllText(filePath, "test");

var fileUri = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileProvider", new Java.IO.File(filePath)); using (var intent = new Intent(Intent.ActionView)) { intent.SetDataAndType(fileUri, "text/plain"); intent.AddFlags(ActivityFlags.NewTask); intent.AddFlags(ActivityFlags.GrantReadUriPermission); intent.PutExtra(Intent.ExtraNotUnknownSource, true);

Platform.CurrentActivity.StartActivity(intent);

}

2. Deploy the app in Release mode on Android

### Link to public reproduction project repository

https://github.com/mlxyz/file-provider-release-android-repro

### Version with bug

Unknown/Other

### Is this a regression from previous behavior?

Not sure, did not test other versions

### Last version that worked well

Unknown/Other

### Affected platforms

Android

### Affected platform versions

_No response_

### Did you find any workaround?

See [this answer on SO](https://stackoverflow.com/a/75381940). The solution is not acceptable for me as disabling linking (and AOT) will decrease app performance significantly.

### Relevant log output

```shell
Time    Device Name Type    PID Tag Message
02-28 16:24:11.125  LENOVO TB128FU  Info    32743   MonoDroid   java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.companyname.fileproviderreleaserepro.fileProvider
    at androidx.core.content.FileProvider.getFileProviderPathsMetaData(FileProvider.java:664)
    at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:695)
    at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:645)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:449)
    at crc64fcf28c0e24b4cc31.ButtonHandler_ButtonClickListener.n_onClick(Native Method)
    at crc64fcf28c0e24b4cc31.ButtonHandler_ButtonClickListener.onClick(ButtonHandler_ButtonClickListener.java:31)
    at android.view.View.performClick(View.java:7515)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1211)
    at android.view.View.performClickInternal(View.java:7492)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:29356)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:8163)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:946)
jaosnz-rep commented 7 months ago

Verified on 17.10.0 Preview 2.0, can repro on .NET 8 Android platform with sample project. https://github.com/mlxyz/file-provider-release-android-repro