Xamarin 表单:NoClassDefFoundError Landroidx/appcompat/app/AppCompatActivity

Xamarin forms: NoClassDefFoundError Landroidx/appcompat/app/AppCompatActivity

我正在尝试使用名为“ImageCropper”的本机 android 库:https://github.com/ArthurHub/Android-Image-Cropper

我创建了 android 绑定,但是当我尝试从 XF 自定义渲染中使用它时,它会抛出一条错误消息:“java.lang.NoClassDefFoundError:解析失败:Landroidx/appcompat/app/AppCompatActivity”。 =15=]

我正在编译我的 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 存储库(但此处使用的绑定已过时):https://github.com/matheusneder/Xamarin.Android.ImageCropper/tree/master/Source/Xamarin.Android.ImageCropper.App

好吧,即使目标版本设置为 API 级别 29,androidX 软件包由于某种原因仍未包含在内。因此,通过在 android 项目的 csproj 文件中添加以下包引用来手动包含 AndroidX 包摆脱了 above-mentioned 错误。

<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 项目并按预期使用此库中的功能。