sanengineer / react-native-v8

Opt-in V8 runtime for React Native Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version Build for Android

React Native with V8 Runtime

This project aims to support V8 replacement runtime for React Native. Designed as opt-in package, it is easy to integrate with existing React Native projects.

Installation for Expo projects (>= SDK 45)

For managed projects, you can install through the single command:

$ expo install react-native-v8 v8-android-jit expo-build-properties

For bare projects, you can run expo prebuild -p android --clean after the installation to prebuild again.

Installation for React Native >= 0.66

  1. Install react-native-v8 and a v8-android variant. For example, the v8-android-jit:
$ yarn add react-native-v8 v8-android-jit
  1. Exclude unused libraries to reduce APK size
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -161,11 +161,18 @@ android {
             }
         }
     }
+
+    packagingOptions {
+        // Make sure libjsc.so does not packed in APK
+        exclude "**/libjsc.so"
+    }
 }
  1. Setup V8 in the MainApplication.java.
--- a/android/app/src/main/java/com/rn067/MainApplication.java
+++ b/android/app/src/main/java/com/rn067/MainApplication.java
@@ -2,15 +2,20 @@ package com.rn067;

 import android.app.Application;
 import android.content.Context;
+
 import com.facebook.react.PackageList;
 import com.facebook.react.ReactApplication;
 import com.facebook.react.ReactInstanceManager;
 import com.facebook.react.ReactNativeHost;
 import com.facebook.react.ReactPackage;
+import com.facebook.react.bridge.JavaScriptExecutorFactory;
+import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
 import com.facebook.soloader.SoLoader;
 import java.lang.reflect.InvocationTargetException;
 import java.util.List;

+import io.csie.kudo.reactnative.v8.executor.V8ExecutorFactory;
+
 public class MainApplication extends Application implements ReactApplication {

   private final ReactNativeHost mReactNativeHost =
@@ -33,6 +39,14 @@ public class MainApplication extends Application implements ReactApplication {
         protected String getJSMainModuleName() {
           return "index";
         }
+
+        @Override
+        protected JavaScriptExecutorFactory getJavaScriptExecutorFactory() {
+          return new V8ExecutorFactory(
+              getApplicationContext(),
+              getPackageName(),
+              AndroidInfoHelpers.getFriendlyDeviceName(),
+              getUseDeveloperSupport());
+        }
       };

   @Override
  1. [OPTIONAL] If you encounter gradle errors from JVM Out-of-memory, please increase the JVM memory size
--- a/android/gradle.properties
+++ b/android/gradle.properties
@@ -11,6 +11,7 @@
 # The setting is particularly useful for tweaking memory settings.
 # Default value: -Xmx1024m -XX:MaxPermSize=256m
 # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
+org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m

 # When configured, Gradle will run in incubating parallel mode.
 # This option should only be used with decoupled projects. More details, visit
  1. yarn android

  2. You can verify whether it works by evaluating the global._v8runtime().version JavaScript statement.

Installation for React Native < 0.66

react-native-v8 v1 does not support previous react-native versions. Please check the installation guide from the previous version.

Builtin JavaScript object

global._v8runtime() has some builtin information such as v8 version.

console.log(`V8 version is ${global._v8runtime().version}`);

Please note that global._v8runtime() existed only for V8 enabled environment but not React Native remote debugging mode. For remote debugging mode, the JavaScript actually runs on Chrome from your host and there is no V8Runtime.

V8 Variants

react-native-v8 depends on a V8 shared library built from v8-android-buildscripts. v8-android-jit is the recommended V8 variant. This is a full featured V8 with both JIT and Intl. We provide other V8 variants to fulfill your needs. For instance, if you want to reduce memory usage, the non JIT variant, aka V8 lite mode. For detailed V8 features, please check the v8-android-buildscripts feature flags.

Package name JIT Intl
v8-android-jit Yes Yes
v8-android-jit-nointl Yes No
v8-android No Yes
v8-android-nointl No No

iOS Support (Experimented)

We did have experimented iOS support. To adopt V8 for Xcodeproj gets a little complicated, so we have a pre-shaped template. Please check react-native-template-v8 for more information.

FAQ

How to reduce APK size ?

The V8 currently bundled by default supports Intl and the ICU data costs about 7MiB per ABI. If you are not going to use Intl, you could use no-Intl version to reduce APK size. (jsc-android have no Intl by default)

TODO

About

Opt-in V8 runtime for React Native Android

License:MIT License


Languages

Language:C++ 71.7%Language:Java 11.5%Language:Python 5.6%Language:CMake 3.0%Language:Objective-C++ 2.9%Language:Objective-C 1.9%Language:Ruby 1.8%Language:JavaScript 0.8%Language:Starlark 0.4%Language:TypeScript 0.4%