Skip to content

Commit 5d7c8b7

Browse files
committed
feat: Upgrade Android to new arch C++ only
1 parent 23b06b3 commit 5d7c8b7

File tree

13 files changed

+1895
-2510
lines changed

13 files changed

+1895
-2510
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ const worklet = () => {
1313
}
1414
```
1515

16-
> [!NOTE]
17-
> In most cases, react-native-worklets-core shouldn't be used as a standalone dependency but rather as a peer-dependency for other modules such as [react-native-vision-camera](https://github.com/mrousavy/react-native-vision-camera), [react-native-wishlist](https://github.com/margelo/react-native-wishlist), or [react-native-skia](https://github.com/Shopify/react-native-skia).
18-
1916
## Installation
2017

2118
1. Install the library from npm:
@@ -37,6 +34,12 @@ const worklet = () => {
3734
yarn start --reset-cache
3835
```
3936

37+
## Requirements
38+
39+
- Requires react-native 0.74 or higher
40+
- Requires the [new architecture](https://reactnative.dev/docs/the-new-architecture/landing-page) to be enabled
41+
- Requires [Hermes](https://reactnative.dev/docs/hermes)
42+
4043
## Usage
4144

4245
See [USAGE.md](docs/USAGE.md)

package/android/CMakeLists.txt

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,41 @@
1-
project("rnworklets")
2-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.9.0)
2+
project(ReactNativeWorklets)
33

44
set(CMAKE_VERBOSE_MAKEFILE ON)
55
set(CMAKE_CXX_STANDARD 17)
6+
set(PACKAGE_NAME "react-native-worklets-core")
67

7-
if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
8-
string(APPEND CMAKE_CXX_FLAGS " -DDEBUG")
9-
endif()
10-
11-
set(PACKAGE_NAME "rnworklets")
12-
13-
14-
# Pre-set Folly flags from React Native core
15-
include("${REACT_NATIVE_DIR}/ReactAndroid/cmake-utils/folly-flags.cmake")
16-
add_compile_options(${folly_FLAGS})
17-
18-
# Consume shared libraries and headers from prefabs
19-
find_package(fbjni REQUIRED CONFIG)
20-
find_package(ReactAndroid REQUIRED CONFIG)
21-
22-
if(${JS_RUNTIME} STREQUAL "hermes")
23-
find_package(hermes-engine REQUIRED CONFIG)
24-
endif()
25-
26-
file(GLOB_RECURSE SOURCES_COMMON CONFIGURE_DEPENDS "../cpp/**.cpp")
27-
8+
# Compile sources
289
add_library(
29-
${PACKAGE_NAME}
30-
SHARED
31-
${SOURCES_COMMON}
32-
cpp-adapter.cpp
10+
${PACKAGE_NAME}
11+
SHARED
12+
../cpp/base/WKTJsiHostObject.cpp
13+
../cpp/base/WKTRuntimeLifecycleMonitor.cpp
14+
../cpp/dispatch/WKTDispatchQueue.cpp
15+
../cpp/wrappers/WKTJsiPromiseWrapper.cpp
16+
../cpp/wrappers/WKTJsiWrapper.cpp
17+
../cpp/WKTJsiWorkletApi.cpp
18+
../cpp/WKTJsiWorkletContext.cpp
3319
)
3420

35-
# includes
36-
target_include_directories(
37-
${PACKAGE_NAME}
38-
PRIVATE
39-
../cpp
40-
../cpp/base
41-
../cpp/decorators
42-
../cpp/dispatch
43-
../cpp/sharedvalues
44-
../cpp/wrappers
45-
"${REACT_NATIVE_DIR}/ReactAndroid/src/main/jni/react/turbomodule"
46-
"${REACT_NATIVE_DIR}/ReactCommon"
47-
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
48-
"${REACT_NATIVE_DIR}/ReactCommon/runtimeexecutor"
49-
)
21+
# Add headers search paths
22+
target_include_directories(${PACKAGE_NAME} PUBLIC ../cpp)
5023

51-
# build shared lib
52-
set_target_properties(${PACKAGE_NAME} PROPERTIES LINKER_LANGUAGE CXX)
24+
# Add Hermes (from com.facebook.react:hermes-android)
25+
find_package(hermes-engine REQUIRED CONFIG)
5326

27+
# Link everything together
5428
target_link_libraries(
55-
${PACKAGE_NAME}
56-
log
57-
android
29+
${PACKAGE_NAME}
30+
ReactAndroid::jsi # <-- JSI core
31+
react_codegen_RNWorkletsSpec # <-- Generated Specs from CodeGen
32+
hermes-engine::libhermes # <-- Hermes Prefab
5833
)
5934

60-
target_link_libraries(
61-
${PACKAGE_NAME}
62-
ReactAndroid::folly_runtime
63-
ReactAndroid::glog
64-
ReactAndroid::jsi
65-
ReactAndroid::reactnativejni
66-
fbjni::fbjni
67-
)
68-
69-
if(${JS_RUNTIME} STREQUAL "hermes")
70-
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_HERMES=1")
71-
72-
# From prefab from module `com.facebook.react:hermes-android`
73-
set(HERMES_LIB hermes-engine::libhermes)
74-
35+
# Add Hermes Debugger if enabled in Gradle CMake options
36+
if(${HERMES_ENABLE_DEBUGGER})
7537
target_link_libraries(
7638
${PACKAGE_NAME}
77-
${HERMES_LIB}
39+
ReactAndroid::hermes_executor
7840
)
79-
80-
if(${HERMES_ENABLE_DEBUGGER})
81-
set(HERMES_EXECUTOR_LIB ReactAndroid::hermes_executor)
82-
83-
target_link_libraries(
84-
${PACKAGE_NAME}
85-
${HERMES_EXECUTOR_LIB}
86-
)
87-
endif()
88-
elseif(${JS_RUNTIME} STREQUAL "jsc")
89-
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_JSC=1")
90-
91-
set(JSEXECUTOR_LIB ReactAndroid::jscexecutor)
92-
93-
target_link_libraries(${PACKAGE_NAME} ${JSEXECUTOR_LIB})
94-
else()
95-
message(FATAL_ERROR "Unknown JS runtime ${JS_RUNTIME}.")
9641
endif()

package/android/build.gradle

Lines changed: 40 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,29 @@
1-
import java.nio.file.Paths
2-
3-
def getExtOrDefault(name) {
4-
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Worklets_" + name]
5-
}
6-
7-
def getExtOrIntegerDefault(name) {
8-
return getExtOrDefault(name).toInteger()
9-
}
10-
11-
def isNewArchitectureEnabled() {
12-
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
13-
}
14-
15-
static def findNodeModules(baseDir) {
16-
def basePath = baseDir.toPath().normalize()
17-
// Node's module resolution algorithm searches up to the root directory,
18-
// after which the base path will be null
19-
while (basePath) {
20-
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
21-
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
22-
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
23-
return nodeModulesPath.toString()
24-
}
25-
basePath = basePath.getParent()
1+
buildscript {
2+
repositories {
3+
google()
4+
mavenCentral()
265
}
27-
throw new GradleException("react-native-worklets-core: Failed to find node_modules/ path!")
28-
}
296

30-
def JS_RUNTIME = {
31-
// Override JS runtime with environment variable
32-
if (System.getenv("JS_RUNTIME")) {
33-
return System.getenv("JS_RUNTIME")
7+
dependencies {
8+
classpath "com.android.tools.build:gradle:7.2.1"
349
}
35-
36-
// Check if Hermes is enabled in app setup
37-
def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
38-
if (appProject?.hermesEnabled?.toBoolean()) {
39-
return "hermes"
40-
}
41-
42-
// Use JavaScriptCore (JSC) by default
43-
return "jsc"
44-
}.call()
45-
46-
def nodeModules = findNodeModules(projectDir)
47-
48-
def reactNativeArchitectures() {
49-
def value = project.getProperties().get("reactNativeArchitectures")
50-
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
5110
}
5211

5312
apply plugin: "com.android.library"
13+
apply plugin: "com.facebook.react"
5414

55-
if (isNewArchitectureEnabled()) {
56-
apply plugin: "com.facebook.react"
57-
}
58-
59-
task prepareHeaders(type: Copy) {
60-
from fileTree('../cpp').filter { it.isFile() }
61-
into "${project.buildDir}/headers/rnworklets/react-native-worklets-core/"
62-
includeEmptyDirs = false
15+
def getExtOrDefault(name) {
16+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Worklets_" + name]
6317
}
6418

65-
task deleteCmakeCache() {
66-
doFirst {
67-
delete "${projectDir}/.cxx"
68-
}
19+
def getExtOrIntegerDefault(name) {
20+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Worklets_" + name]).toInteger()
6921
}
7022

7123
android {
24+
namespace "com.worklets"
25+
26+
ndkVersion getExtOrDefault("ndkVersion")
7227
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
7328

7429
defaultConfig {
@@ -77,39 +32,27 @@ android {
7732

7833
externalNativeBuild {
7934
cmake {
80-
arguments "-DANDROID_STL=c++_shared",
81-
"-DANDROID_TOOLCHAIN=clang",
82-
"-DREACT_NATIVE_DIR=${nodeModules}/react-native",
83-
"-DJS_RUNTIME=${JS_RUNTIME}"
84-
abiFilters (*reactNativeArchitectures())
35+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
8536
}
8637
}
8738
}
8839

89-
buildFeatures {
90-
prefab true
91-
}
92-
93-
compileOptions {
94-
sourceCompatibility JavaVersion.VERSION_1_8
95-
targetCompatibility JavaVersion.VERSION_1_8
96-
}
97-
9840
externalNativeBuild {
9941
cmake {
10042
path "CMakeLists.txt"
10143
}
10244
}
10345

46+
buildFeatures {
47+
buildConfig true
48+
prefab true
49+
}
50+
10451
buildTypes {
10552
debug {
10653
externalNativeBuild {
10754
cmake {
108-
if (JS_RUNTIME == "hermes") {
109-
arguments "-DHERMES_ENABLE_DEBUGGER=1"
110-
} else {
111-
arguments "-DHERMES_ENABLE_DEBUGGER=0"
112-
}
55+
arguments "-DHERMES_ENABLE_DEBUGGER=1"
11356
}
11457
}
11558
}
@@ -119,62 +62,33 @@ android {
11962
arguments "-DHERMES_ENABLE_DEBUGGER=0"
12063
}
12164
}
65+
minifyEnabled false
12266
}
12367
}
12468

125-
sourceSets {
126-
main {
127-
if (isNewArchitectureEnabled()) {
128-
java.srcDirs += ["src/newarch"]
129-
} else {
130-
java.srcDirs += ["src/oldarch"]
131-
}
132-
}
133-
}
134-
135-
packagingOptions {
136-
excludes = [
137-
"META-INF",
138-
"META-INF/**",
139-
"**/libc++_shared.so",
140-
"**/libfbjni.so",
141-
"**/libjsi.so",
142-
"**/libfolly_json.so",
143-
"**/libfolly_runtime.so",
144-
"**/libglog.so",
145-
"**/libhermes.so",
146-
"**/libhermes-executor-debug.so",
147-
"**/libhermes_executor.so",
148-
"**/libreactnativejni.so",
149-
"**/libturbomodulejsijni.so",
150-
"**/libreact_nativemodule_core.so",
151-
"**/libjscexecutor.so",
152-
]
153-
}
154-
155-
buildFeatures {
156-
prefabPublishing true
69+
lintOptions {
70+
disable "GradleCompatible"
15771
}
72+
}
15873

159-
prefab {
160-
rnworklets {
161-
headers "${project.buildDir}/headers/rnworklets/"
162-
}
163-
}
74+
repositories {
75+
mavenCentral()
76+
google()
16477
}
16578

79+
16680
dependencies {
167-
implementation "com.facebook.react:react-android"
168-
if (JS_RUNTIME == "hermes") {
169-
implementation "com.facebook.react:hermes-android"
170-
}
171-
}
81+
// For < 0.71, this will be from the local maven repo
82+
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
83+
//noinspection GradleDynamicVersion
84+
implementation "com.facebook.react:react-native:+"
17285

173-
preBuild.dependsOn(prepareHeaders)
86+
// Requires Hermes Runtime
87+
implementation "com.facebook.react:hermes-android"
88+
}
17489

175-
tasks.configureEach { task ->
176-
// C++ clean
177-
if (task.name.contains("clean")) {
178-
task.dependsOn(deleteCmakeCache)
179-
}
90+
react {
91+
jsRootDir = file("../src/")
92+
libraryName = "Worklets"
93+
codegenJavaPackageName = "com.worklets"
18094
}

package/android/cpp-adapter.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

package/android/gradle.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Worklets_kotlinVersion=1.7.0
2-
Worklets_minSdkVersion=21
1+
Worklets_minSdkVersion=23
32
Worklets_targetSdkVersion=31
43
Worklets_compileSdkVersion=31
54
Worklets_ndkversion=21.4.7075529

0 commit comments

Comments
 (0)