React Native 是否将 JavaScript 编译成 Java for Android?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41124338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:10:10  来源:igfitidea点击:

Does React Native compile JavaScript into Java for Android?

javascriptjavaandroidreact-nativereact-native-android

提问by unlimited101

When I develop hybrid apps with React Native. Does the JavaScript code I write transform into Java-Code or Java-Bytecode for the Dalvik/ART Runtime when I create an Android-App from my React Native code? Or are just the UI components compiled into native UI components? Or does a library like the Fetch API compile the JavaScript code into Java-Code or Java-Bytecode?

当我使用 React Native 开发混合应用程序时。当我从 React Native 代码创建 Android 应用程序时,我编写的 JavaScript 代码是否会转换为 Dalvik/ART 运行时的 Java 代码或 Java 字节码?还是只是将 UI 组件编译为原生 UI 组件?或者像 Fetch API 这样的库是否将 JavaScript 代码编译成 Java-Code 或 Java-Bytecode?

采纳答案by Marc

Basically, you write Javascript. The Javascript communicates with native components (Java on Android, Objective C on iOS, C# on Windows).

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

The communication occurs through the so-called "bridge". If at any time you feel that this communication slows things down too much, you can choose to implement the Javascript functionality in Java, Objective C or C# respectively in order to run purely native. In this case, you are writing directly in native code, so there's no Javascript to native compilation.

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

This will sacrifice compatibility for performance. Normally, this is not necessary.

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

回答by Gurdev Singh

The code remains the JavaScript native code and is not converted into any other format. The hybrid apps run inside the native container app which invokes JavaScript run time engine which takes care of executing the JavaScript code. I hope this clarifies the question.

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

回答by N.SH

React Native works as a wrapper. For example: if you wanted to put a button in your layout, you'd simply add a buttontag. You then use a specific API from the UI module to render this on Android. You can easily create custom native modules to use in your React Native projects. However, code written natively is often faster.

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

回答by Morteza Ziyae

Based on "React Made Native Easy"book:

基于“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 可以被认为是一组 React 组件,其中每个组件代表对应的原生视图和组件。

Also there is two parts in React Native architechture:

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.

  1. 原生代码/模块:iOS 的大部分原生代码是用 Objective C 或 Swift 编写的,而在 Android 的情况下,它是用 Java 编写的。但是为了编写我们的 React Native 应用程序,我们几乎不需要为 iOS 或 Android 编写本机代码。

  2. Javascript VM:运行我们所有 JavaScript 代码的 JS 虚拟机。在 iOS/Android 模拟器和设备上,React Native 使用 JavaScriptCore,它是支持 Safari 的 JavaScript 引擎。JavaScriptCore 是最初为 WebKit 构建的开源 JavaScript 引擎。对于 iOS,React Native 使用 iOS 平台提供的 JavaScriptCore。它首次与 OS X Mavericks 一起在 iOS 7 中引入。

And for communication between these parts:

对于这些部分之间的通信:

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 Bridge:React Native 桥接器是一个 C++/Java 桥接器,负责原生和 Javascript 线程之间的通信。自定义协议用于消息传递。

回答by smoosh911

React Native

反应本机

React -> JavaScriptCore -> Native Code -> "What you see"

React -> JavaScriptCore -> Native Code -> “你看到的”

Hybrid App

混合应用

JavaScript -> Native WebView wrapper -> "What you see"

JavaScript -> Native WebView 包装器 -> “你所看到的”

回答by barak 109

The best explanation i saw-

我看到的最好的解释-

00:00 - 03:55

00:00 - 03:55

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

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."

“在编译后的本机应用程序中 - 所有 UI(按钮,文本...)都将被编译为本机代码(Java 或 Objective C),而 JavaScript 部分将保留 JavaScript。”

For full explanation -

完整的解释 -

08:06-14:10

08:06-14:10

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

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

enter image description here

在此处输入图片说明