xamarin.forms - Xamarin 形式:NoClassDefFoundError Landroidx/appcompat/app/AppCompatActivity
问题描述
我正在尝试使用此处提供的名为“ImageCropper”的原生 android 库:https ://github.com/ArthurHub/Android-Image-Cropper
我创建了 android 绑定,但是当我尝试从 XF 自定义渲染中使用它时,它会抛出一条错误消息:“java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/appcompat/app/AppCompatActivity”
我正在编译我的 XF 应用程序,目标版本设置为 API 29。不确定我缺少什么或如何继续解决此问题。任何建议都非常感谢。谢谢。
PS:XF 自定义渲染器:https ://pastebin.com/85Mdsy8c
public class ImagePickerService : IImagePickerService
{
public IImageSourceUtility ImageSourceUtility => new ImageSourceUtility();
private void StartActivity()
{
var currentActivity = MainActivity.AppActivity;
if (currentActivity != null)
{
var cropImageOptions = new CropImageOptions();
cropImageOptions.MultiTouchEnabled = true;
cropImageOptions.Guidelines = CropImageView.Guidelines.On;
cropImageOptions.AspectRatioX = 1;
cropImageOptions.AspectRatioY = 1;
cropImageOptions.FixAspectRatio = true;
cropImageOptions.Validate();
var intent = new Intent();
intent.SetClass(currentActivity, Class.FromType(typeof(ImagePickerOnResultActivity)));
intent.PutExtra(CropImage.CropImageExtraSource, null as global::Android.Net.Uri); // Image Uri
intent.PutExtra(CropImage.CropImageExtraOptions, cropImageOptions);
currentActivity.StartActivity(intent);
}
else
{
throw new InvalidOperationException("Could not get current activity.");
}
}
public Task<ImageSource> PickImageAsync()
{
StartActivity();
return Task.Run(() =>
{
_waitHandle.WaitOne();
var result = _pickAsyncResult;
_pickAsyncResult = null;
return result;
});
}
private static ImageSource _pickAsyncResult;
private static EventWaitHandle _waitHandle = new AutoResetEvent(false);
// if crashes(in release mode after linking), see plugin desc for proguard config change required for this theme
[Activity(Label = "CropImageActivity", Theme = "@style/Base.Theme.AppCompat")]
//[Activity(Theme = "@style/Base.Theme.AppCompat")]
public class ImagePickerOnResultActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
//base.OnCreate(savedInstanceState);
// start activity
var x = CropImage.Activity();
x.SetGuidelines(CropImageView.Guidelines.On)
.Start(this);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
if (requestCode == CropImage.CropImageActivityRequestCode)
{
CropImage.ActivityResult result = CropImage.GetActivityResult(data);
if (resultCode == Result.Ok)
{
var croppedFileUri = new Uri(result.Uri.ToString());
_pickAsyncResult = ImageSource.FromFile(croppedFileUri.LocalPath);
_waitHandle.Set();
}
else if ((int)resultCode == CropImage.CropImageActivityResultErrorCode)
{
Java.Lang.Exception error = result.Error;
}
}
}
}
}
org GH repo(但此处使用的绑定已过时):https ://github.com/matheusneder/Xamarin.Android.ImageCropper/tree/master/Source/Xamarin.Android.ImageCropper.App
解决方案
好的,即使目标版本设置为 API 级别 29,由于某种原因,androidX 包也没有包含在内。因此,通过在 android 项目的 csproj 文件中添加以下包引用来手动包含 AndroidX 包消除了上述错误。
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.2.0.1" />
<PackageReference Include="Xamarin.AndroidX.Browser" Version="1.2.0.1" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.0.0.1" />
<PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0.1" />
该库需要以下附加包:
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.1.0.1" />
<PackageReference Include="Xamarin.AndroidX.ExifInterface" Version="1.1.0.1" />
更好的方法是将这些依赖项包含在绑定库本身中,但我不知道该怎么做。
无论如何,这个解决方案现在有效,我能够编译我的 XF 项目并按预期使用这个库中的功能。
推荐阅读
- angular - 通过@Input 传递枚举类名
- javascript - 当我将 HTTPS 设置为 true 时,我的反应应用程序停止工作?
- visual-studio-code - 无法覆盖自定义 VS CODE 主题(Material Theme)令牌(tokenColorCustomizations)
- mysql - 从选择中插入失败并出现奇怪的错误
- javascript - 创建新元素时只拼接一个元素
- c++ - 应该接受不同输入参数的纯虚函数 - 这应该如何实现?
- python-3.x - CSV 阅读器在文本中插入括号
- android - 使用 React native 检测设备何时开机
- parallel-processing - MPI:协调许多发送和接收的最佳方式
- vue.js - 无法加载 pdf - Vue