问题描述
我正在尝试使用一个名为“ ImageCropper”的本地android库,可在此处使用:https://github.com/ArthurHub/Android-Image-Cropper
我创建了android绑定,但是当我尝试从呈现的XF自定义中使用它时,它抛出一条错误消息:“ java.lang.NoClassDefFoundError:无法解决以下问题:Landroidx / appcompat / app / AppCompatActivity”
我正在编译目标版本设置为API 29的XF应用程序。不知道我缺少什么或者如何继续解决此问题。衷心感谢任何建议。谢谢。
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包,可以摆脱上述错误。
<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项目并使用该库中的功能。