Xamarin.Android 绑定无效操作码 [英] Xamarin.Android binding invalid opcode

查看:24
本文介绍了Xamarin.Android 绑定无效操作码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近收到了一个新的 Android SDK (aar) 以在 Xamarin 中进行绑定.最初开始绑定时,我收到错误

Recently received a new Android SDK (aar) to bind in Xamarin. When initially starting the binding I receive the error

COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)

推荐答案

此错误是由于 SDK 包含 Java 8 (v1.8) 字节码,需要与 Java 7 (v1.7) 兼容才能支持较低的 Android API 版本.

This error is due to the SDK containing Java 8 (v1.8) byte code and it needs to be compatible with Java 7 (v1.7) to support lower API versions of Android.

在您的 Android 项目(不是绑定项目)中,添加以下属性.

In your Android project (not the binding project), add the following property.

<AndroidEnableDesugar>True</AndroidEnableDesugar>

或者如果您使用的是 VS 2019+,您可以打开 D8,默认情况下启用此功能.

or if you are in VS 2019+ you can turn on D8 which enables this by default.

<AndroidDexTool>d8</AndroidDexTool>

脱糖是将 Java 8 字节码转换为 Java 7 兼容字节码的过程.这是执行转换的 Google 进程,是 Xamarin.Android 构建进程的一部分.

Desugaring is the process of allowing Java 8 byte code to be converted into Java 7 compatible byte code. This is a Google process that performs the conversion and is part of the Xamarin.Android build process.

那么如果 SDK 没有任何依赖项,它应该都可以工作.如果您有 EmbeddedReferenceJars,那么事情会变得更加复杂.

Then if the SDK doesn't have any dependencies, it should all work. If you have EmbeddedReferenceJars, then things get more complicated.

在 Visual Studio 2017 中,您会遇到类似于

In Visual Studio 2017 you will experience errors similar to

Error: java.lang.TypeNotPresentException :  Type io.reactivex.functions.Consumer not present

将无法在参考 jar 中找到这些类型,因为脱糖处理无法正常工作.从 Github 问题你最终找到原因:

It will be unable to find these types in reference jars, because the desugaring processing isn't working properly. From Github Issues you eventually find the reason:

The first fix here is to add the `--classpath_entry` flag for every
`--input`, for some reason `Desugar` is not treating `--input` jars as
classpath entries

该错误已在 Visual Studio 2019 预览版 2 中进行跟踪和修复.

The bug is already tracked and fixed in Visual Studio 2019 Preview 2.

现在您切换到 Visual Studio 2019 预览版 2,您将遇到此错误.

Now you switch to Visual Studio 2019 Preview 2 and you will come across this error.

Java.Lang.NoClassDefFoundError: Failed resolution of: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension;

脱糖处理再次失败,因为它需要这个类来帮助与脱糖代码进行通信.

The desugaring processing is failing again because it needs this class to help communicate to the desugared code.

实际上找到了 ThrowableExtension 类:https://github.com/bazelbuild/bazel/blob/master/src/tools/android/java/com/google/devtools/build/android/desugar/runtime/ThrowableExtension.java

The class ThrowableExtension is actually found: https://github.com/bazelbuild/bazel/blob/master/src/tools/android/java/com/google/devtools/build/android/desugar/runtime/ThrowableExtension.java

然后,您可以使用该 Java 类并将其编译为 jar.一个快速的方法是将该 java 文件复制到一个文件夹中.然后在该文件夹中创建一个名为 output 的文件夹.

You then take that Java class and you can compile it into a jar. A quick way to do it is copy that java file into a folder. Then inside that folder create a folder called output.

调用这个命令

javac -d ./output ThrowableExtension.java

然后进入输出目录并调用这个命令

Then move into the output directory and call this command

jar cvf desugar.jar *

它将创建 desugar.jar.在您的 Android 绑定项目中将其添加为 EmbeddedReferenceJar.您现在可以通过脱糖与 Java 8 字节代码进行绑定.

It will create desugar.jar. Add that as an EmbeddedReferenceJar in your Android binding project. Your binding with Java 8 byte code with desugaring should now work.

希望这可能会在未来的 Visual Studio 版本中得到解决,因此所有这些步骤都不是必需的,但在那之前,至少您知道发生了什么以及如何修复它.

Hopefully this might be resolved in a future Visual Studio version, so all these steps aren't necessary, but until then, at least you know what is happening and how to fix it.

这篇关于Xamarin.Android 绑定无效操作码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆