網(wǎng)站首頁 編程語言 正文
引言
目前,最新版本的React Native(0.70.0及以上版本)已經(jīng)默認開啟了Hermes引擎。而Hermes則是專門針對React Native應用而優(yōu)化的全新JavaScript引擎,啟用Hermes引擎可以優(yōu)化啟動時間,減少內(nèi)存占用以及空間占用。
一、啟用 Hermes 引擎
1.1 Android
如果我們打開React Native項目的Android源碼,會在app/build.gradle文件中會看到如下的代碼。
if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
implementation jscFlavor
}
上面代碼的含義是,如果開啟了就采用Hermes引擎,如果未開啟則使用以前的jsc引擎。所以,如果需要開啟Hermes引擎,只需要將enableHermes屬性值設置成true即可,如下所示。
project.ext.react = [
entryFile: "index.js",
- enableHermes: false // clean and rebuild if changing
+ enableHermes: true // clean and rebuild if changing
]
另外,如果您正在使用ProGuard,還需要在ProGuard -rules.pro中添加如下的混淆規(guī)則。
-keep class com.facebook.hermes.unicode.** { *; }
-keep class com.facebook.jni.** { *; }
然后,使用如下的命令重新編譯項目,并運行項目。
cd android && ./gradlew clean
npx react-native run-android
1.2 iOS
從React Native 0.64開始,Hermes也支持在iOS上運行。要啟用Hermes for iOS,需要打開iOS/Podfile文件,并進行如下更改。
use_react_native!(
:path => config[:reactNativePath],
# to enable hermes on iOS, change `false` to `true` and then install pods
# By default, Hermes is disabled on Old Architecture, and enabled on New Architecture.
# You can enable/disable it manually by replacing `flags[:hermes_enabled]` with `true` or `false`.
- :hermes_enabled => flags[:hermes_enabled],
+ :hermes_enabled => true
)
如果需要在iOS中開啟Hermes引擎,需要將上面的hermes_enabled 的值修改為 true。完成上述配置之后,再使用下面的命令重新運行項目。
cd ios && pod install
npx react-native run-ios
如果iOS使用的是Mac M1的架構(gòu),可能還會遇到Cocoapods 的一些兼容問題。如果在安裝 pods依賴時出現(xiàn)問題,可以嘗試運行下面的命令:
sudo arch -x86_64 gem install ffi
arch -x86_64 pod install
以上命令會安裝ffi包,用于在安裝和裝載 pods 時調(diào)用合適的系統(tǒng)架構(gòu)。
二、Hermes 引擎使用
2.1 檢查 Hermes 引擎是否啟用
如果我們創(chuàng)建了一個新的React Native應用程序,想要在應用中查看是否啟用Hermes,那么可以使用下面的方式。
const isHermes = () => !!global.HermesInternal;
想要體驗開啟Hermes帶來的好處,可以嘗試重新運行項目來進行對比。
npx react-native run-android --variant release
npx react-native run-ios --configuration Release
2.2 綁定Hermes
從React Native 0.69開始,每個版本的React Native都會綁定一個Hermes版本。每當我們發(fā)布React Native的新版本時,官方都會為開發(fā)者構(gòu)建一個Hermes的版本,目的是確保使用的Hermes版本與React Native版本的完全兼容。
這是因為,從歷史上版本來看,我們在匹配Hermes的版本和React Native的版本時遇到過兼容性問題。為了完全消除這類問題,我們?yōu)殚_發(fā)者提供了一個與特定React Native版本兼容的JS引擎。
同時,這些更改對React Native開發(fā)者是完全透明的,您仍然可以根據(jù)配置來啟用/禁用Hermes引擎。
2.3 使用DevTools在Hermes上調(diào)試JS
Hermes通過實現(xiàn)Chrome檢查器協(xié)議來支持Chrome調(diào)試器,這意味著,我們可以直接使用Chrome的DevTools工具來直接調(diào)試運行JavaScript代碼。
? 同時,Chrome通過Metro連接運行在設備上的Hermes,所以我們需要知道Metro監(jiān)聽的端口。通常,默認的監(jiān)聽地址是localhost:8081,不過這都是可以配置的。當運行yarn啟動時,該地址在啟動時被寫入stdout。
? 一旦我們知道了Metro服務器監(jiān)聽的端口,接下來就可以使用以下步驟連接Chrome:
- 在Chrome瀏覽器中輸入chrome://inspect。
- 然后在Chrome瀏覽器中輸入localhost:8081開啟調(diào)試。
現(xiàn)在,會看到一個帶有“inspect”鏈接的“Hermes React Native”目標,可以用來調(diào)出調(diào)試器;如果沒有看到“inspect”鏈接,請確保Metro服務器正在運行。
現(xiàn)在,我們就可以使用Chrome調(diào)試工具進行代碼調(diào)試了,如下圖所示。
原文鏈接:https://juejin.cn/post/7145404043785928711
相關(guān)推薦
- 2022-11-28 ContentProvider客戶端處理provider邏輯分析_Android
- 2022-10-16 Qt實現(xiàn)TCP網(wǎng)絡編程_C 語言
- 2023-01-05 Kotlin?高階函數(shù)與Lambda表達式示例詳解_Android
- 2023-03-16 PostgreSQL?復制表的?5?種方式詳解_PostgreSQL
- 2022-12-09 Python網(wǎng)絡編程之Python編寫TCP協(xié)議程序的步驟_python
- 2022-02-07 出現(xiàn)報錯nginx: [emerg] unknown directive nginx.htacces
- 2022-11-21 基于C++實現(xiàn)一個日期計算器_C 語言
- 2022-11-05 Nginx負載均衡之upstream模塊簡介與使用詳解_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支