英文:
.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>
您还可以指示链接器保留一些重要的程序集。
操作步骤如下:
- 在您的Android项目中添加名为
Linker.xml
的XML文件。 - 在解决方案资源管理器中右键单击
Linker.xml
,然后选择属性。 - 将构建操作更改为
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 :
<PropertyGroup>
<AndroidLinkMode>None</AndroidLinkMode>
</PropertyGroup>
And you can also instruct the linker to keep some of the important assemblies .
The steps are as follows: :
- Add an XML file named
Linker.xml
to your Android project. - Right click
Linker.xml
in your Solution Explorer then select Properties. - Change Build Action to
LinkDescription
.
There is a simimar thread,you can check it here Linker.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论