了解如何提高React Native 应用程序的性能、缩短启动时间、减少包大小和内存使用量。
如何提高React Native App的性能
1、Hermes的使用
Hermes是一个针对 React Native 优化的开源 JavaScript 引擎。Hermes 集成有助于缩短启动时间、减少内存使用并缩小应用程序大小。
使用 Hermes,您的应用程序将以 2 倍的速度启动,内存使用率将降低至 30%,Android 应用程序包大小减少 30%,iOS 应用程序性能将得到提升。
让我们看看如何整合爱马仕。Hermes 0.64 版本后支持iOS。因此,请确保使用React Native Upgrade Helper升级您的应用程序。
启用Hermes
Android -使用方法
在 android/app/build.gradle 中添加以下代码行
project.ext.react = [
entryFile: "index.js",
- enableHermes: false // clean and rebuild if changing
+ enableHermes: true // clean and rebuild if changing
]
如果您使用 ProGuard,请在 proguard-rules.pro 中添加以下规则:
-keep class com.facebook.hermes.unicode.** {
*; }
-keep class com.facebook.jni.** {
*; }
清理构建
cd android && ./gradlew clean
iOS -使用方法
使用以下代码行编辑 ios/Podfile 文件 -
use_react_native!(
:path => config[:reactNativePath],
# to enable hermes on iOS, change false to true and then install pods
-