Skip to content

Commit 8c18a18

Browse files
feat: v1
1 parent 4dea0c9 commit 8c18a18

File tree

21 files changed

+19851
-88
lines changed

21 files changed

+19851
-88
lines changed

android/CMakeLists.txt

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
cmake_minimum_required(VERSION 3.4.1)
1+
project(ReactNativeMsgPack)
2+
cmake_minimum_required(VERSION 3.9.0)
23

4+
set (PACKAGE_NAME "react-native-msgpack")
5+
set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
36
set (CMAKE_VERBOSE_MAKEFILE ON)
4-
set (CMAKE_CXX_STANDARD 11)
7+
set (CMAKE_CXX_STANDARD 17)
8+
9+
include_directories(
10+
../cpp
11+
)
512

613
add_library(cpp
714
SHARED
8-
../cpp/react-native-msgpack.cpp
915
cpp-adapter.cpp
16+
../cpp/react-native-msgpack.hpp
17+
../cpp/cmp.c
18+
../cpp/cmp.h
19+
../cpp/msgpack.hpp
1020
)
1121

12-
# Specifies a path to native header files.
13-
include_directories(
14-
../cpp
22+
set_target_properties(
23+
cpp PROPERTIES
24+
CXX_STANDARD 17
25+
CXX_EXTENSIONS OFF
26+
POSITION_INDEPENDENT_CODE ON
27+
)
28+
29+
30+
find_package(ReactAndroid REQUIRED CONFIG)
31+
32+
target_link_libraries(
33+
cpp
34+
ReactAndroid::jsi
35+
android
1536
)

android/build.gradle

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,51 @@ def getExtOrIntegerDefault(name) {
2727
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Msgpack_" + name]).toInteger()
2828
}
2929

30+
def resolveBuildType() {
31+
Gradle gradle = getGradle()
32+
String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()
33+
34+
return tskReqStr.contains('Release') ? 'release' : 'debug'
35+
}
36+
3037
android {
3138
ndkVersion getExtOrDefault("ndkVersion")
39+
ndkPath getExtOrDefault("ndkPath")
3240
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
3341

42+
buildFeatures {
43+
prefab true
44+
}
45+
3446
defaultConfig {
3547
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
3648
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
3749
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
3850
externalNativeBuild {
3951
cmake {
40-
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
52+
cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
53+
arguments "-DANDROID_STL=c++_shared"
4154
abiFilters "x86", "x86_64", "armeabi-v7a", "arm64-v8a"
4255
}
4356
}
4457
}
58+
4559
externalNativeBuild {
4660
cmake {
4761
path "CMakeLists.txt"
4862
}
4963
}
50-
buildTypes {
51-
release {
52-
minifyEnabled false
53-
}
64+
65+
packagingOptions {
66+
doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
67+
excludes = [
68+
"META-INF",
69+
"META-INF/**",
70+
"**/libjsi.so",
71+
]
5472
}
5573

74+
5675
lintOptions {
5776
disable "GradleCompatible"
5877
}
@@ -84,3 +103,18 @@ if (isNewArchitectureEnabled()) {
84103
codegenJavaPackageName = "com.msgpack"
85104
}
86105
}
106+
107+
108+
tasks.whenTaskAdded { task ->
109+
if (task.name.contains("configureCMakeDebug")) {
110+
rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
111+
task.dependsOn(it)
112+
}
113+
}
114+
// We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
115+
if (task.name.contains("configureCMakeRel")) {
116+
rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
117+
task.dependsOn(it)
118+
}
119+
}
120+
}

android/cpp-adapter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include <jni.h>
2-
#include "react-native-msgpack.h"
2+
#include "react-native-msgpack.hpp"
33

4-
extern "C"
5-
JNIEXPORT jint JNICALL
6-
Java_com_msgpack_MsgpackModule_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
7-
return msgpack::multiply(a, b);
4+
extern "C" JNIEXPORT void JNICALL
5+
Java_com_msgpack_MsgpackModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi)
6+
{
7+
auto runtime = reinterpret_cast<jsi::Runtime *>(jsi);
8+
install(*runtime);
89
}

android/src/main/java/com/msgpack/MsgpackModule.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.msgpack;
22

3+
import android.util.Log;
4+
35
import androidx.annotation.NonNull;
46

57
import com.facebook.react.bridge.Promise;
68
import com.facebook.react.bridge.ReactApplicationContext;
79
import com.facebook.react.bridge.ReactContextBaseJavaModule;
810
import com.facebook.react.bridge.ReactMethod;
911
import com.facebook.react.module.annotations.ReactModule;
12+
import com.facebook.react.bridge.JavaScriptContextHolder;
1013

1114
@ReactModule(name = MsgpackModule.NAME)
1215
public class MsgpackModule extends ReactContextBaseJavaModule {
@@ -23,15 +26,24 @@ public String getName() {
2326
}
2427

2528
static {
29+
Log.i(NAME, "Loading C++ library...");
2630
System.loadLibrary("cpp");
2731
}
2832

29-
private static native double nativeMultiply(double a, double b);
30-
31-
// Example method
32-
// See https://reactnative.dev/docs/native-modules-android
33-
@ReactMethod
34-
public void multiply(double a, double b, Promise promise) {
35-
promise.resolve(nativeMultiply(a, b));
33+
private native void nativeInstall(long jsi);
34+
35+
36+
@ReactMethod(isBlockingSynchronousMethod = true)
37+
public boolean install() {
38+
try {
39+
ReactApplicationContext reactContext = getReactApplicationContext();
40+
JavaScriptContextHolder contextHolder = reactContext.getJavaScriptContextHolder();
41+
this.nativeInstall(contextHolder.get());
42+
Log.i(NAME, "Successfully installed!");
43+
return true;
44+
} catch (Exception exception) {
45+
Log.e(NAME, "Failed to install JSI Bindings!", exception);
46+
return false;
47+
}
3648
}
3749
}

0 commit comments

Comments
 (0)