Skip to content

Commit 7ec17a4

Browse files
author
pioner921227
committed
init android
1 parent eac42d2 commit 7ec17a4

File tree

9 files changed

+510
-18
lines changed

9 files changed

+510
-18
lines changed

android/CMakeLists.txt

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
11
cmake_minimum_required(VERSION 3.4.1)
2-
project(Xxhash)
2+
project(react-native-xxhash)
33

44
set (CMAKE_VERBOSE_MAKEFILE ON)
5-
set (CMAKE_CXX_STANDARD 14)
5+
set (CMAKE_CXX_STANDARD 17)
66

77
add_library(react-native-xxhash SHARED
88
../cpp/react-native-xxhash.cpp
9+
../cpp/xxhash.c
910
cpp-adapter.cpp
1011
)
1112

13+
find_package(ReactAndroid REQUIRED CONFIG)
14+
15+
find_package(fbjni REQUIRED CONFIG)
16+
17+
target_include_directories(
18+
react-native-xxhash PRIVATE
19+
"${NODE_MODULES_DIR}/react-native/React"
20+
"${NODE_MODULES_DIR}/react-native/React/Base"
21+
"${NODE_MODULES_DIR}/react-native/ReactCommon"
22+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
23+
# "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
24+
# "${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
25+
)
26+
1227
# Specifies a path to native header files.
1328
include_directories(
1429
../cpp
1530
)
31+
32+
if (ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
33+
target_link_libraries(
34+
react-native-xxhash
35+
ReactAndroid::jsi
36+
log
37+
ReactAndroid::reactnative
38+
fbjni::fbjni
39+
)
40+
else()
41+
target_link_libraries(
42+
react-native-xxhash
43+
fbjni::fbjni
44+
android
45+
ReactAndroid::jsi
46+
ReactAndroid::turbomodulejsijni
47+
)
48+
49+
50+
# target_link_libraries(react-native-xxhash
51+
# ReactAndroid::jsi
52+
# log
53+
# ReactAndroid::reactnative
54+
# fbjni::fbjni
55+
# )

android/build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import groovy.json.JsonSlurper
2+
import org.apache.tools.ant.filters.ReplaceTokens
3+
import java.nio.file.Paths
4+
15
buildscript {
26
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
37
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Xxhash_kotlinVersion"]
@@ -14,6 +18,24 @@ buildscript {
1418
}
1519
}
1620

21+
static def findNodeModules(baseDir) {
22+
def basePath = baseDir.toPath().normalize()
23+
// Node's module resolution algorithm searches up to the root directory,
24+
// after which the base path will be null
25+
while (basePath) {
26+
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
27+
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
28+
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
29+
return nodeModulesPath.toString()
30+
}
31+
basePath = basePath.getParent()
32+
}
33+
throw new GradleException("react-native-xxhash: Failed to find node_modules/ path!")
34+
}
35+
36+
def nodeModules = findNodeModules(projectDir)
37+
logger.warn("react-native-xxhash: node_modules/ found at: ${nodeModules}")
38+
1739
def reactNativeArchitectures() {
1840
def value = rootProject.getProperties().get("reactNativeArchitectures")
1941
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
@@ -69,6 +91,8 @@ android {
6991
cmake {
7092
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
7193
abiFilters (*reactNativeArchitectures())
94+
arguments "-DANDROID_STL=c++_shared",
95+
"-DNODE_MODULES_DIR=${nodeModules}"
7296
}
7397
}
7498
}
@@ -85,6 +109,23 @@ android {
85109
}
86110
}
87111

112+
buildFeatures {
113+
buildConfig true
114+
prefab true
115+
}
116+
117+
packagingOptions {
118+
excludes = [
119+
"**/libjsi.so",
120+
"**/libreactnativejni.so",
121+
"**/libreact_nativemodule_core.so",
122+
"**/libturbomodulejsijni.so",
123+
"**/libc++_shared.so",
124+
"**/libfbjni.so",
125+
"**/libreactnative.so",
126+
]
127+
}
128+
88129
lintOptions {
89130
disable "GradleCompatible"
90131
}

android/cpp-adapter.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
#include <jni.h>
22
#include "react-native-xxhash.h"
3+
#include "jsi/jsi.h"
4+
#include <android/log.h>
5+
#include <fbjni/fbjni.h>
6+
#include <fbjni/detail/Registration.h>
7+
#include <typeinfo>
38

4-
extern "C"
5-
JNIEXPORT jdouble JNICALL
6-
Java_com_xxhash_XxhashModule_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
7-
return xxhash::multiply(a, b);
9+
//XXHashBridge
10+
11+
12+
using namespace facebook;
13+
14+
struct XXHashBridge : jni::JavaClass<XXHashBridge> {
15+
public:
16+
static constexpr auto kJavaDescriptor = "Lcom/xxhash/XxhashModule;";
17+
18+
static void registerNatives() {
19+
javaClassStatic()->registerNatives({
20+
makeNativeMethod("nativeInstall", XXHashBridge::nativeInstall)
21+
});
22+
}
23+
private:
24+
static void nativeInstall(
25+
jni::alias_ref<jni::JObject> thiz,
26+
jlong jsiRuntimePointer
27+
) {
28+
29+
// Реализация
30+
auto jsiRuntime = reinterpret_cast<jsi::Runtime*>(jsiRuntimePointer);
31+
// Установите JSI
32+
xxhash::install(jsiRuntime);
33+
}
34+
};
35+
36+
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
37+
return jni::initialize(vm, [] { XXHashBridge::registerNatives(); });
838
}
42.6 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)