.NET MAUI FileProvider 在发布模式下不起作用。

huangapple go评论53阅读模式
英文:

.NET MAUI FileProvider on Android does not work in release mode

问题

以下是您要翻译的内容:

"I am attempting to install an apk file using .NET MAUI on an Android device like so:

var file = Path.Combine(FileSystem.Current.CacheDirectory, "app.apk");
File.WriteAllBytes(file, bytes);

var context = Android.App.Application.Context; 
Java.IO.File file = new Java.IO.File(path);
using (Android.Content.Intent install = new Android.Content.Intent(Android.Content.Intent.ActionView))
{
     var uri = Microsoft.Maui.Storage.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileProvider", file);
     install.SetDataAndType(uri, "application/vnd.android.package-archive");
     install.AddFlags(Android.Content.ActivityFlags.NewTask);
     install.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
     install.AddFlags(Android.Content.ActivityFlags.ClearTop);
     install.PutExtra(Android.Content.Intent.ExtraNotUnknownSource, true);
     Platform.CurrentActivity.StartActivity(install);
}

This works perfectly in debug mode, but in release mode I get the following exception: Java.Lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.mycompany.mobile.fileProvider

I thought maybe I needed to add a [ContentProvider] attribute to the application, but then it no longer builds because of duplicate content providers for androidx.core.content.FileProvider, so I assume MAUI is including this content provider automatically?"

英文:

I am attempting to install an apk file using .NET MAUI on an Android device like so:

var bytes = DownloadApkFile();
var file = Path.Combine(FileSystem.Current.CacheDirectory, "app.apk");
File.WriteAllBytes(file, bytes);

var context = Android.App.Application.Context; 
Java.IO.File file = new Java.IO.File(path);
using (Android.Content.Intent install = new Android.Content.Intent(Android.Content.Intent.ActionView))
{
	 var uri = Microsoft.Maui.Storage.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileProvider", file);
	 install.SetDataAndType(uri, "application/vnd.android.package-archive");
	 install.AddFlags(Android.Content.ActivityFlags.NewTask);
	 install.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
	 install.AddFlags(Android.Content.ActivityFlags.ClearTop);
	 install.PutExtra(Android.Content.Intent.ExtraNotUnknownSource, true);
	 Platform.CurrentActivity.StartActivity(install);
}

This works perfectly in debug mode, but in release mode I get the following exception: Java.Lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.mycompany.mobile.fileProvider

I thought maybe I needed to add a [ContentProvider] attribute to the application, but then it no longer builds because of duplicate content providers for androidx.core.content.FileProvider, so I assume MAUI is including this content provider automatically?

答案1

得分: 0

Android/iOS的链接器由于大量使用反射而删除了您的应用程序的程序集的一些部分。您可以通过在csproj文件中添加以下代码来进行验证:

<PropertyGroup>
    <AndroidLinkMode>None</AndroidLinkMode>
</PropertyGroup>

您还可以指示链接器保留一些重要的程序集。

操作步骤如下:

  1. 在您的Android项目中添加名为Linker.xml的XML文件。
  2. 在解决方案资源管理器中右键单击Linker.xml,然后选择属性。
  3. 将构建操作更改为LinkDescription

这里有一个类似的帖子,您可以在此处查看。

英文:

The Android/iOS linkers are removing chunks of the assemblies of your app due to the heavy use of reflection. You can verify this by adding code as follows in the csproj file as follows :

&lt;PropertyGroup&gt;
    &lt;AndroidLinkMode&gt;None&lt;/AndroidLinkMode&gt;
&lt;/PropertyGroup&gt;

And you can also instruct the linker to keep some of the important assemblies .

The steps are as follows: :

  1. Add an XML file named Linker.xml to your Android project.
  2. Right click Linker.xml in your Solution Explorer then select Properties.
  3. Change Build Action to LinkDescription.

There is a simimar thread,you can check it here Linker.

huangapple
  • 本文由 发表于 2023年2月8日 08:33:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定