Skip to content

Commit 20420a1

Browse files
Merge pull request #15 from software-mansion-labs/feat/android/jsi-config
feat: added basic jsi example
2 parents 8a0ed2f + b22a3d3 commit 20420a1

22 files changed

+324
-309
lines changed

CODE_OF_CONDUCT.md

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

android/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
cmake_minimum_required(VERSION 3.4.1)
2-
project(AudioContext)
1+
cmake_minimum_required(VERSION 3.9.0)
2+
project(react-native-audio-context)
33

44
set (CMAKE_VERBOSE_MAKEFILE ON)
55
set (CMAKE_CXX_STANDARD 14)
66

7-
add_library(react-native-audio-context SHARED
8-
../cpp/react-native-audio-context.cpp
7+
add_library(react-native-audio-context
8+
SHARED
9+
../cpp/JSIExampleHostObject.cpp
910
cpp-adapter.cpp
1011
)
1112

1213
# Specifies a path to native header files.
1314
include_directories(
1415
../cpp
16+
../node_modules/react-native/ReactCommon/jsi
17+
)
18+
19+
find_package(ReactAndroid REQUIRED CONFIG)
20+
21+
target_link_libraries(
22+
react-native-audio-context
23+
ReactAndroid::jsi
24+
android
1525
)

android/build.gradle

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import com.android.Version
2+
13
buildscript {
24
repositories {
35
google()
46
mavenCentral()
57
}
68

79
dependencies {
8-
classpath "com.android.tools.build:gradle:7.2.1"
10+
classpath "com.android.tools.build:gradle:7.2.2"
911
}
1012
}
1113

@@ -19,6 +21,7 @@ def isNewArchitectureEnabled() {
1921
}
2022

2123
apply plugin: "com.android.library"
24+
apply plugin: 'org.jetbrains.kotlin.android'
2225

2326
if (isNewArchitectureEnabled()) {
2427
apply plugin: "com.facebook.react"
@@ -32,8 +35,8 @@ def getExtOrIntegerDefault(name) {
3235
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AudioContext_" + name]).toInteger()
3336
}
3437

35-
def supportsNamespace() {
36-
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
38+
static def supportsNamespace() {
39+
def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
3740
def major = parsed[0].toInteger()
3841
def minor = parsed[1].toInteger()
3942

@@ -64,6 +67,7 @@ android {
6467
cmake {
6568
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
6669
abiFilters (*reactNativeArchitectures())
70+
arguments "-DANDROID_STL=c++_shared"
6771
}
6872
}
6973
}
@@ -76,6 +80,7 @@ android {
7680

7781
buildFeatures {
7882
buildConfig true
83+
prefab true
7984
}
8085

8186
buildTypes {
@@ -103,6 +108,9 @@ android {
103108
}
104109
}
105110
}
111+
kotlinOptions {
112+
jvmTarget = '17'
113+
}
106114
}
107115

108116
repositories {
@@ -116,12 +124,13 @@ dependencies {
116124
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
117125
//noinspection GradleDynamicVersion
118126
implementation "com.facebook.react:react-native:+"
127+
implementation 'androidx.core:core-ktx:1.13.1'
119128
}
120129

121130
if (isNewArchitectureEnabled()) {
122131
react {
123132
jsRootDir = file("../src/")
124-
libraryName = "AudioContext"
133+
libraryName = "react-native-audio-context"
125134
codegenJavaPackageName = "com.audiocontext"
126135
}
127136
}

android/cpp-adapter.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
#include <jni.h>
2-
#include "react-native-audio-context.h"
2+
#include <jsi/jsi.h>
3+
#include "JSIExampleHostObject.h"
4+
5+
using namespace facebook;
6+
7+
void install(jsi::Runtime& runtime) {
8+
auto hostObject = std::make_shared<example::JSIExampleHostObject>();
9+
auto object = jsi::Object::createFromHostObject(runtime, hostObject);
10+
runtime.global().setProperty(runtime, "__JSIExampleProxy", std::move(object));
11+
}
312

413
extern "C"
5-
JNIEXPORT jdouble JNICALL
6-
Java_com_audiocontext_AudioContextModule_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
7-
return audiocontext::multiply(a, b);
14+
JNIEXPORT void JNICALL
15+
Java_com_audiocontext_jsi_JSIExampleModule_00024Companion_nativeInstall(JNIEnv *env, jobject clazz, jlong jsiPtr) {
16+
auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
17+
if (runtime) {
18+
install(*runtime);
19+
}
820
}

android/src/main/java/com/audiocontext/AudioContextModule.java

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

android/src/main/java/com/audiocontext/AudioContextPackage.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.audiocontext
2+
3+
import com.audiocontext.jsi.JSIExampleModule
4+
import com.facebook.react.ReactPackage
5+
import com.facebook.react.bridge.NativeModule
6+
import com.facebook.react.bridge.ReactApplicationContext
7+
import com.facebook.react.uimanager.ViewManager
8+
9+
class JSIExamplePackage : ReactPackage {
10+
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
11+
return listOf<NativeModule>(JSIExampleModule(reactContext))
12+
}
13+
14+
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
15+
return emptyList()
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.audiocontext.jsi
2+
3+
import android.util.Log
4+
import com.facebook.react.bridge.ReactApplicationContext
5+
import com.facebook.react.bridge.ReactContextBaseJavaModule
6+
import com.facebook.react.bridge.ReactMethod
7+
import com.facebook.react.module.annotations.ReactModule
8+
9+
@ReactModule(name = JSIExampleModule.NAME)
10+
class JSIExampleModule(reactContext: ReactApplicationContext?) :
11+
ReactContextBaseJavaModule(reactContext) {
12+
override fun getName(): String {
13+
return NAME
14+
}
15+
16+
@ReactMethod(isBlockingSynchronousMethod = true)
17+
fun install(): Boolean {
18+
try {
19+
System.loadLibrary("react-native-audio-context")
20+
21+
val jsContext = reactApplicationContext.javaScriptContextHolder
22+
23+
nativeInstall(jsContext!!.get())
24+
return true
25+
} catch (exception: Exception) {
26+
Log.e(NAME, "Failed to install JSI Bindings for react-native-audio-context", exception)
27+
return false
28+
}
29+
}
30+
31+
companion object {
32+
const val NAME: String = "JSIExample"
33+
34+
private external fun nativeInstall(jsiPtr: Long)
35+
}
36+
}

0 commit comments

Comments
 (0)