Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hermes support to React Native on Android #25613

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion RNTester/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ project.ext.react = [
bundleAssetName: "RNTesterApp.android.bundle",
entryFile: file("../../js/RNTesterApp.android.js"),
root: "$rootDir",
inputExcludes: ["android/**", "./**", ".gradle/**"]
inputExcludes: ["android/**", "./**", ".gradle/**"],
composeSourceMapsPath: "$rootDir/scripts/compose-source-maps.js",
hermesCommand: "../../../node_modules/hermesvm/%OS-BIN%/hermes",
enableHermesForVariant: { def v -> v.name.contains("hermes") }
]

apply from: "../../../react.gradle"
Expand Down Expand Up @@ -105,6 +108,16 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}

flavorDimensions "vm"
productFlavors {
hermes {
dimension "vm"
}
jsc {
dimension "vm"
}
}

defaultConfig {
applicationId "com.facebook.react.uiapp"
minSdkVersion 16
Expand Down Expand Up @@ -138,6 +151,12 @@ android {
signingConfig signingConfigs.release
}
}
packagingOptions {
pickFirst '**/armeabi-v7a/libc++_shared.so'
pickFirst '**/x86/libc++_shared.so'
pickFirst '**/arm64-v8a/libc++_shared.so'
pickFirst '**/x86_64/libc++_shared.so'
}
}

dependencies {
Expand All @@ -146,6 +165,10 @@ dependencies {
// Build React Native from source
implementation project(':ReactAndroid')

def hermesPath = '$projectDir/../../../../node_modules/hermesvm/android/'
debugImplementation files(hermesPath + "hermes-debug.aar")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be wrapped in an if (enableHermes)? We don't need both hermes and jsc.

releaseImplementation files(hermesPath + "hermes-release.aar")

if (useIntlJsc) {
implementation 'org.webkit:android-jsc-intl:+'
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.react.views.text.ReactFontManager;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -44,6 +48,7 @@ public List<ReactPackage> getPackages() {
public void onCreate() {
ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik);
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}

@Override
Expand Down
19 changes: 18 additions & 1 deletion ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ task prepareFolly(dependsOn: dependenciesPath ? [] : [downloadFolly], type: Copy
into("$thirdPartyNdkDir/folly")
}

task prepareHermes() {
def hermesAAR = file("$projectDir/../node_modules/hermesvm/android/hermes-debug.aar")
if (!hermesAAR.exists()) {
// For an app to build from RN source, hermesvm is located at /path/to/app/node_modules
// and $projectDir is located at /path/to/app/node_modules/react-native/ReactAndroid
hermesAAR = file("$projectDir/../../hermesvm/android/hermes-debug.aar")
}
def soFiles = zipTree(hermesAAR).matching({ it.include "**/*.so" })

copy {
from soFiles
from "src/main/jni/first-party/hermes/Android.mk"
into "$thirdPartyNdkDir/hermes"
}
}

task downloadGlog(dependsOn: createNativeDepsDirectories, type: Download) {
src("https://github.com/google/glog/archive/v${GLOG_VERSION}.tar.gz")
onlyIfNewer(true)
Expand Down Expand Up @@ -232,7 +248,7 @@ def getNdkBuildFullPath() {
return ndkBuildFullPath
}

task buildReactNdkLib(dependsOn: [prepareJSC, prepareBoost, prepareDoubleConversion, prepareFolly, prepareGlog], type: Exec) {
task buildReactNdkLib(dependsOn: [prepareJSC, prepareHermes, prepareBoost, prepareDoubleConversion, prepareFolly, prepareGlog], type: Exec) {
inputs.dir("$projectDir/../ReactCommon")
inputs.dir("src/main/jni")
inputs.dir("src/main/java/com/facebook/react/modules/blob")
Expand Down Expand Up @@ -270,6 +286,7 @@ task packageReactNdkLibs(dependsOn: buildReactNdkLib, type: Copy) {
from("$buildDir/react-ndk/all")
into("$buildDir/react-ndk/exported")
exclude("**/libjsc.so")
exclude("**/libhermes.so")
}

task packageReactNdkLibsForBuck(dependsOn: packageReactNdkLibs, type: Copy) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library", "react_native_dep", "rn_xplat_cxx_library")

rn_android_library(
name = "instrumentation",
srcs = ["HermesMemoryDumper.java"],
visibility = ["PUBLIC"],
deps = [
react_native_dep("java/com/facebook/proguard/annotations:annotations"),
],
)

rn_android_library(
name = "impl",
srcs = ["HermesMemoryDumperImpl.java"],
visibility = ["PUBLIC"],
deps = [
react_native_dep("java/com/facebook/common/appstate:appstate"),
react_native_dep("java/com/facebook/common/errorreporting/memory:memory"),
react_native_dep("java/com/facebook/common/fileupload:fileupload"),
react_native_dep("java/com/facebook/common/identifiers:identifiers"),
react_native_dep("java/com/facebook/common/stringformat:stringformat"),
react_native_dep("java/com/facebook/common/time:time"),
react_native_dep("java/com/facebook/hermes/annotations:annotations"),
react_native_dep("java/com/facebook/inject:inject"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
":instrumentation",
],
)

rn_xplat_cxx_library(
name = "jni-interface",
srcs = [],
header_namespace = "hermes/jni/instrumentation",
exported_headers = ["HermesMemoryDumper.h"],
visibility = ["PUBLIC"],
deps = [
react_native_dep("native/fb:fb"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
#include <fb/fbjni.h>
#include <string>

namespace facebook {
namespace jsi {
namespace jni {

namespace jni = ::facebook::jni;

class HermesMemoryDumper : public jni::JavaClass<HermesMemoryDumper> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/facebook/hermes/instrumentation/HermesMemoryDumper;";

bool shouldSaveSnapshot() {
static auto shouldSaveSnapshotMethod =
javaClassStatic()->getMethod<jboolean()>("shouldSaveSnapshot");
return shouldSaveSnapshotMethod(self());
}

std::string getInternalStorage() {
static auto getInternalStorageMethod =
javaClassStatic()->getMethod<jstring()>("getInternalStorage");
return getInternalStorageMethod(self())->toStdString();
}

std::string getId() {
static auto getInternalStorageMethod =
javaClassStatic()->getMethod<jstring()>("getId");
return getInternalStorageMethod(self())->toStdString();
}

void setMetaData(std::string crashId) {
static auto getIdMethod =
javaClassStatic()->getMethod<void(std::string)>("setMetaData");
getIdMethod(self(), crashId);
}
};

} // namespace jni
} // namespace jsi
} // namespace facebook
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
package com.facebook.hermes.instrumentation;

public interface HermesMemoryDumper {
boolean shouldSaveSnapshot();
String getInternalStorage();
String getId();
void setMetaData(String crashId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
REACT_NATIVE := $(LOCAL_PATH)/../../../../../../../..

LOCAL_MODULE := hermes-executor-release

LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)

LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(REACT_NATIVE)/node_modules/hermesvm/android/include $(REACT_NATIVE)/../hermesvm/android/include

LOCAL_CPP_FEATURES := exceptions

LOCAL_STATIC_LIBRARIES := libjsireact libjsi
LOCAL_SHARED_LIBRARIES := libfolly_json libfb libreactnativejni libhermes

include $(BUILD_SHARED_LIBRARY)


include $(CLEAR_VARS)
REACT_NATIVE := $(LOCAL_PATH)/../../../../../../../..

LOCAL_MODULE := hermes-executor-debug
LOCAL_CFLAGS := -DHERMES_ENABLE_DEBUGGER=1

LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)

LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(REACT_NATIVE)/node_modules/hermesvm/android/include $(REACT_NATIVE)/../hermesvm/android/include

LOCAL_CPP_FEATURES := exceptions

LOCAL_STATIC_LIBRARIES := libjsireact libjsi libhermes-inspector
LOCAL_SHARED_LIBRARIES := libfolly_json libfb libreactnativejni libhermes

include $(BUILD_SHARED_LIBRARY)
124 changes: 124 additions & 0 deletions ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library", "react_native_dep", "react_native_target", "rn_xplat_cxx_library")

rn_android_library(
name = "reactexecutor",
srcs = [
"HermesExecutor.java",
"HermesExecutorFactory.java",
],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("java/com/facebook/hermes/instrumentation:instrumentation"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
":jni",
":runtimeconfig",
react_native_target("java/com/facebook/react/bridge:bridge"),
],
)

rn_android_library(
name = "reactexecutor-hermes-snapshot",
srcs = [
"HermesSnapshotExecutor.java",
"HermesSnapshotExecutorFactory.java",
],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("java/com/facebook/hermes/instrumentation:instrumentation"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
":jni-hermes-snapshot",
":runtimeconfig",
react_native_target("java/com/facebook/react/bridge:bridge"),
],
)

rn_android_library(
name = "runtimeconfig",
srcs = [
"RuntimeConfig.java",
],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("java/com/facebook/hermes/instrumentation:instrumentation"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
],
)

rn_xplat_cxx_library(
name = "jni-interface",
srcs = [],
header_namespace = "hermes/reactexecutor",
exported_headers = glob(["*.h"]),
visibility = ["PUBLIC"],
deps = [
"fbsource//xplat/hermes/public:public",
],
)

rn_xplat_cxx_library(
name = "jni-interface-snapshot",
srcs = [],
header_namespace = "hermes/reactexecutor",
exported_headers = glob(["*.h"]),
visibility = ["PUBLIC"],
deps = [
"fbsource//xplat/hermes-snapshot/public:public",
],
)

rn_xplat_cxx_library(
name = "jni",
srcs = ["OnLoad.cpp"],
headers = [],
header_namespace = "",
allow_jni_merging = True,
compiler_flags = ["-fexceptions"],
soname = "libhermes-executor.$(ext)",
visibility = [
react_native_dep("java/com/facebook/hermes/reactexecutor:reactexecutor"),
],
deps = [
react_native_dep("java/com/facebook/hermes/crashmanager:crashmanager"),
react_native_dep("java/com/facebook/hermes/instrumentation:jni-interface"),
react_native_dep("java/com/facebook/quicklog/jni:qpljsibindings"),
react_native_dep("native/fb:fb"),
"fbsource//xplat/MobileCoreHealth/fbsofterror:fbsofterror",
"fbsource//xplat/folly:memory",
"fbsource//xplat/hermes/API:HermesAPI",
":jni-interface",
react_native_target("jni/react/jni:jni"),
"fbsource//xplat/ReactNative/react/jsi:HermesExecutorFactory",
],
)

rn_xplat_cxx_library(
name = "jni-hermes-snapshot",
srcs = ["OnLoadSnapshot.cpp"],
headers = [],
header_namespace = "",
allow_jni_merging = True,
compiler_flags = ["-fexceptions"],
soname = "libhermes-executor-snapshot.$(ext)",
visibility = [
react_native_dep("java/com/facebook/hermes/reactexecutor:reactexecutor"),
],
deps = [
react_native_dep("java/com/facebook/hermes/instrumentation:jni-interface"),
react_native_dep("java/com/facebook/quicklog/jni:qpljsibindings"),
react_native_dep("native/fb:fb"),
"fbsource//xplat/MobileCoreHealth/fbsofterror:fbsofterror",
"fbsource//xplat/folly:memory",
"fbsource//xplat/hermes-snapshot/API:HermesAPI",
":jni-interface-snapshot",
react_native_target("jni/react/jni:jni"),
"fbsource//xplat/ReactNative/react/jsi:HermesSnapshotExecutorFactory",
],
)
Loading