问题描述
好吗?
我一直在尝试使用 Xamarin Essentials 媒体选择器,以便在我的应用中拍照和拍摄视频。文档非常简单,并没有对实现代码施加如此大的挑战。
虽然,我遇到了一些有关 Xamarin Essentials nuget 包版本和其他相关库的问题。
所以,这里简要说明我最近几天经历的过程:
重要的是要强调应用程序在此之前正常运行。
首先,我写了以下一段代码:
public async Task TakePicture(object param)
{
try
{
var photo = await MediaPicker.CapturePhotoAsync();
await LoadPhotoAsync(photo);
CanTakePhoto = false;
ShowRemakePhotoButton = CanRemake;
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature is Now supported on the device
}
catch (PermissionException pEx)
{
// Permissions not granted
}
catch (Exception ex)
{
Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}
}
async Task LoadPhotoAsync(FileResult photo)
{
// canceled
if (photo == null)
{
PhotoPath = null;
return;
}
// save the file into local storage
var newFile = Path.Combine(FileSystem.CacheDirectory,photo.FileName);
using (var stream = await photo.OpenReadAsync())
using (var newStream = File.OpenWrite(newFile))
await stream.copyToAsync(newStream);
PhotoPath = newFile;
}
正如预期的那样,我需要将 Xamarin.Essentials nuget 添加到我的 android 和共享项目中,以便能够访问 MediaPicker API。我这样做并安装了最新的可用版本(1.6.1),这导致编译时出现以下错误。
java/lang/String;Ljava/util/List;)[B : 无效的操作码 ba - 调用动态需要 --min-sdk-version >= 26(目前为 13)
com.android.dx.cf.code.SimException: androidx.browser.trusted.ConnectionHolder.getServiceWrapper 中的错误:()Lcom/google/common/util/concurrent/ListenableFuture;:无效的操作码 ba-invokedynamic需要 --min-sdk-version >= 26 (目前为 13) D:\minimo\repos\Terram\TerramAPP2.Android\COMPILetoDALVIK
com.android.dx.cf.code.SimException: androidx.browser.customtabs.CustomTabsService$1.newSessionInternal 中的错误:(Landroid/support/customtabs/ICustomTabsCallback;Landroid/app/PendingIntent;)Z:无效操作码 ba -invokedynamic 需要 --min-sdk-version >= 26(当前为 13) D:\minimo\repos\Terram\TerramAPP2.Android\COMPILetoDALVIK
我尝试了最明显的解决方案,即将 API 最低级别提高到 > 23。但这没有用。
经过一些研究,我发现一个可能的解决方案是将 dx 和 proguard 更改为 r8 和 d8 收缩器和 dex 编译器。
该操作不会导致构建错误,而是会导致运行时错误。错误发生在 MainActivity.cs 文件的“OnCreate”方法上,位于以下代码段的行上:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
Window.SetStatusBarColor(Android.Graphics.Color.Argb(255,15,17,22));
base.OnCreate(savedInstanceState);
UserDialogs.Init(this);
Xamarin.Essentials.Platform.Init(this,savedInstanceState);
Forms.SetFlags("swipeview_Experimental");
global::Xamarin.Forms.Forms.Init(this,savedInstanceState);
global::Rg.Plugins.Popup.Popup.Init(this,savedInstanceState);
Xamarin.FormsMaps.Init(this,savedInstanceState); //<- this is the line where the error occurs
Xamarin.FormsGoogleMaps.Init(this,savedInstanceState);
MobileBarcodeScanner.Initialize(this.Application);
LoadApplication(new App(new AndroidInitializer()));
MessagingCenter.Subscribe<Object>(this,"associes-se ao serviço",(sender) => StartBackgroundLocationService());
MessagingCenter.Subscribe<Object>(this,"desassocies-se ao serviço",(sender) => StopBackgroundLocationService());
}
显然,它与 googlePlayServices 相关,在下面的错误按摩中更详细地显示:
java.lang.classNotFoundException:“在路径上找不到类“com.google.android.gms.common.GooglePlayServicesUtil”:DexPathList[[zip 文件”/data/ app/com.minimo.agro-45tafg5QXIG3cUXxGmGhkw==/base.apk"],nativeLibraryDirectories=[/data/app/com.minimo.agro-45tafg5QXIG3cUXxGmGhkw==/lib/arm,/data/app/com.minimo.agro -45tafg5QXIG3cUXxGmGhkw==/base.apk!/lib/armeabi-v7a,/system/lib]]'
经过一些研究,我发现如果任何包与给定的 GooglePlayService 包相关,如果它们的版本不同,则可能会导致此错误。我一直在尝试将与 GooglePlayServices 有任何关系的所有 nuget 包设置为不会导致此冲突的特定版本,但也没有成功。
项目设置:
- VS 版本 16.10.2
- android 目标 API 29/30
- 测试了 dx/proguard 和 r8/d8
- Xamarin.Forms 5.0.0.2012
- 目标框架 - .NET Standard 2.0
希望有人能帮我解决这个问题!
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)