Android Native Code 的定义是什么?

What is the definition of Android Native Code?

我总是到处看到 android native code 这个词,但不确定它到底是什么。那么android的哪一部分是android native code呢? Application, Activity, Fragment, View, Looper android native code?

更多上下文:我试图了解 robolectric 测试中的阴影,并且在文档中说 "Android native code cannot execute on your development machine",所以我想知道 类 是否像ApplicationActivityandroid native code 在这里引用? http://robolectric.org/extending/

Android 本机代码不是 Java 或 Kotlin。它不是像 Activity 或 Fragment 这样的 类。 Android 本机代码是 C/C++。 Here 是一些关于 SDK(NDK) 的信息。 here 您可以找到 NDK(原生开发工具包)的总体概述。

希望对您有所帮助。

Android 开发中的 native 字词超载。

通过 link 您为 Robolectric 提供的内容:

There are limitations however:

  1. Native code - Android native code cannot execute on your development machine.
  2. Out of process calls - There are no Android system services running on your development machine.
  3. Inadequate testing APIs - Android includes next to no APIs suitable for testing

Roboletric fills these gaps with a set of classes known as Shadows...

所以在这种情况下,Android native code 引用要么是指:

  • Android 框架 类 像 Activity,Fragment,View 只有 Android SDK 应用程序需要模拟器或设备到 运行。但是 Roboletric 带来了自己的 Android 框架代码,可以 'enhanced' by 'Shadows' 用于测试应用程序。

  • Android Native Development Kit (NDK) which uses Java Native Interface (JNI) to allow Java/Kotlin code to access C/C++ code for performance or compatibility reasons. So with Roboletric you can't call into JNI code, or at least not without some effort.

稍后同一页:

Using byte code instrumentation Robolectric is able to weave in cross platform fake implementations to substitute for native code and add additional APIs to make testing possible.

substitute for native code 指的是属于 Android 框架的 Java/Kotlin API,Roboletric 正在替代该框架以提供测试环境。同样,这些将是您所指的 ActivityFragmentView 等。

术语 'native' 在这种情况下的用法类似于开发人员使用第三方应用程序构建框架,如 'React Native'、'Ionic'、'Flutter'、'Xamarian',或'Cordova/Phonegap',他们可能会使用用Java/Kotlin编写的自定义组件作为native component来实现一些只能通过与[=100=直接交互才能完成的功能] 框架,但不在第三方框架的 language/API 中,例如 Javascript、Dart 或 C#。

Java 及其同类(Kotlin、Scala 等)指的是通过 Java 将 C/C++ 代码调用为 native Native 接口 (JNI) 和 Android 由 Native 开发工具包 (NDK) 提供便利。位于移动框架之上的第三方应用程序开发框架会将对原始移动框架的调用称为 "native"。

遗憾的是,由于这是移动开发中使用的术语的一部分,因此需要仔细阅读 "native" 这个词的用法。

就我个人而言,如果使用 native 一词的文档包含 native Java/Kotlin APIsnative C/C++ APIs 之类的语言,因为链接页面中的第一个实例让我来回讨论作者的意思。

跟进评论中的问题

  1. You mentioned that "they may use a custom component written in Java/Kotlin as a native component", you are referring to Activity, Fragment etc when saying custom component, right?

在该特定部分中,我指的是调用 类 的第三方应用程序框架是 Android 框架或直接调用其中的一部分。通常那些第三方应用程序框架已经 wrap/expose Activity、View 等,但作为开发人员,您可能需要一个库或其他可以由第三方调用的自定义 Java/Kotlin 代码应用程序框架语言(Javascript、Dart、C#)。从第三方应用程序框架的角度来看,'wrapped Java/Kotlin library' 是一个 native component,因为它对移动环境来说是 "native"。包装的库代码不是用 Javascript、Dart 或 C# 编写的。 "native" 的含义再次被重载。

  1. In the first paragraph of link, the author is emphasizing that we will run "real Android code" in robolectric. But as we analyzed, robolectric is shadowing the basic building block like Activity, Fragment, which seems contradictory to me, so the only explanation I can think of is that the ShadowActivity is wrapping the original implementation of real Activity, do you think that is the case?

是的,ShadowActivity 是 "wrapping" 真正 Activity 的原始实现,我会注意到作者声明:Shadow objects are not quite Proxies, not quite Fakes, not quite Mocks or Stubs.

It is important that shadow methods are implemented on the corresponding shadow of the class in which they were originally defined. Otherwise Robolectric’s lookup mechanism will not find them (even if they have been declared on a shadow subclass.)

Shadow class inheritance hierarchy does not always mirror that of their associated Android classes, it is sometimes necessary to make calls through these real objects so that the Robolectric runtime will have the opportunity to route them to the correct Shadow class based on the actual class of the object

如此常规的 Java 继承对于 Shadows 来说并不是正确的心智模型。