Skip to content

Commit

Permalink
Merge pull request #454 from rahulpatel/main
Browse files Browse the repository at this point in the history
fix: use nodejs resolution to find node_modules dir for android build
  • Loading branch information
dcangulo committed Apr 14, 2024
2 parents c135372 + 14e834f commit 159c9b1
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.nio.file.Paths
import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
Expand All @@ -11,14 +12,27 @@ buildscript {
}
}

String toPlatformFileString(File path) {
def result = path.toString()
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
result = result.replace(File.separatorChar, '/' as char)
static def findNodeModules(baseDir) {
def basePath = baseDir.toPath().normalize()
// Node"s module resolution algorithm searches up to the root directory,
// after which the base path will be null
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()) {
def result = nodeModulesPath.toString()
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
result = result.replace(File.separatorChar, '/' as char)
}
return result
}
basePath = basePath.getParent()
}
return result
throw new GradleException("react-native-pkce-challenge: Failed to find node_modules/ path!")
}

def nodeModules = findNodeModules(projectDir)

def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}
Expand Down Expand Up @@ -67,7 +81,7 @@ android {
externalNativeBuild {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
arguments "-DNODE_MODULES_DIR=${toPlatformFileString(rootDir)}/../node_modules"
arguments "-DNODE_MODULES_DIR=${nodeModules}"
}
}
}
Expand Down

0 comments on commit 159c9b1

Please sign in to comment.