diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index dd4e4f4..f934106 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -6,9 +6,9 @@ set (CMAKE_CXX_STANDARD 11) # add directories to "include" search paths include_directories( ../cpp - ../../../node_modules/react-native/React - ../../../node_modules/react-native/React/Base - ../../../node_modules/react-native/ReactCommon/jsi + "${NODE_MODULES_DIR}/node_modules/react-native/React" + "${NODE_MODULES_DIR}/node_modules/react-native/React/Base" + "${NODE_MODULES_DIR}/node_modules/react-native/ReactCommon/jsi" ) # set the base libsodium build directory @@ -33,7 +33,7 @@ set_target_properties( sodium PROPERTIES IMPORTED_LOCATION ${LIBSODIUM_BUILD_DIR # which will be built from the listed source files add_library(libsodium SHARED - ../../../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp + "${NODE_MODULES_DIR}/node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp" ../cpp/react-native-libsodium.cpp ../cpp/react-native-libsodium.h cpp-adapter.cpp diff --git a/android/build.gradle b/android/build.gradle index 3dbc737..aa56a61 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -30,6 +30,21 @@ def getExtOrIntegerDefault(name) { return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Libsodium_" + name]).toInteger() } +static def findNodeModules(baseDir) { + def basePath = baseDir.toPath().normalize() + while (basePath) { + def nodeModulesPath = Paths.get(basePath.toString(), "node_modules") + def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native") + if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) { + return nodeModulesPath.toString() + } + basePath = basePath.getParent() + } + throw new GradleException("react-native-libsodium: Failed to find node_modules path") +} + +def nodeModules = findNodeModules(projectDir) + android { ndkVersion getExtOrDefault("ndkVersion") compileSdkVersion getExtOrIntegerDefault("compileSdkVersion") @@ -48,7 +63,8 @@ android { // exceptions (and other STL types) across shared object boundaries work // correctly. // see https://developer.android.com/ndk/guides/cpp-support#selecting_a_c_runtime - arguments "-DANDROID_STL=c++_shared" + arguments "-DANDROID_STL=c++_shared", + "-DNODE_MODULES_DIR=${nodeModules}" } } }