React Native 会为 Android 将 JavaScript 编译成 Java 吗?

Does React Native compile JavaScript into Java for Android?

当我使用 React Native 开发混合应用程序时。当我从我的 React 创建一个 Android-App 时,我编写的 JavaScript 代码是否转换为 Java-Code 或 Java-Bytecode for the Dalvik/ART Runtime本机代码?或者只是将 UI 组件编译成本机 UI 组件?还是像 Fetch API 这样的库将 JavaScript 代码编译成 Java-Code 或 Java-Bytecode?

代码仍然是 JavaScript 本机代码,未转换为任何其他格式。调用 JavaScript 运行 时间引擎的本机容器应用程序中的混合应用程序 运行 负责执行 JavaScript 代码。我希望这能澄清问题。

基本上,您编写 Java脚本。 Java脚本与本机组件通信(Java 在 Android、Objective C 在 iOS、C# 在 Windows)。

通信通过所谓的 "bridge" 进行。如果任何时候您觉得这种通信速度太慢,您可以选择分别在 Java、Objective C 或 C# 中实现 Java 脚本功能,以便 运行 纯原生。在这种情况下,您直接用本机代码编写,因此没有 Java本机编译脚本。

这将牺牲兼容性以换取性能。通常,这不是必需的。

  • 进一步阅读

Understanding React Native bridge concept

React Native 用作包装器。例如:如果您想在布局中放置一个按钮,只需添加一个 button 标签即可。然后,您使用 UI 模块中的特定 API 在 Android 上呈现它。您可以轻松创建自定义本机模块以在您的 React Native 项目中使用。但是,本地编写的代码通常更快。

基于"React Made Native Easy"本书:

Essentially, React Native can be considered as a set of React components, where each component represents the corresponding native views and components.

在 React Native 架构中也有两个部分:

  1. Native Code/Modules: Most of the native code in case of iOS is written in Objective C or Swift, while in the case of Android it is written in Java. But for writing our React Native app, we would hardly ever need to write native code for iOS or Android.

  2. Javascript VM: The JS Virtual Machine that runs all our JavaScript code. On iOS/Android simulators and devices React Native uses JavaScriptCore, which is the JavaScript engine that powers Safari. JavaScriptCore is an open source JavaScript engine originally built for WebKit. In case of iOS, React Native uses the JavaScriptCore provided by the iOS platform. It was first introduced in iOS 7 along with OS X Mavericks.

以及这些部分之间的通信:

React Native Bridge: React Native bridge is a C++/Java bridge which is responsible for communication between the native and Javascript thread. A custom protocol is used for message passing.

React Native

React -> JavaScript核心 -> 本机代码 -> "What you see"

混合应用程序

JavaScript -> 本机 WebView 包装器 -> "What you see"

我看到的最好的解释-

00:00 - 03:55

https://www.youtube.com/watch?v=6ZnfsJ6mM5c&t=1228s

"In react native app after compiled - all the UI(Buttons,Text...) going to get compiled to native code(Java or Objective C) and the JavaScript part is going to stay JavaScript."

完整解释 -

08:06-14:10

https://www.youtube.com/watch?v=qSRrxpdMpVc