From 4240aa615e42bba814ab02f7d82697e116012f29 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Sat, 31 Oct 2020 20:19:40 +0100 Subject: [PATCH] fix(Android): Find react-native using node module resolution --- android/build.gradle | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/android/build.gradle b/android/build.gradle index 4dc43dc8..ff8c7ad2 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,3 +1,22 @@ +import java.nio.file.Paths + +def resolveModulePath(packageName) { + def basePath = rootDir.toPath().normalize() + + // Node's module resolution algorithm searches up to the root directory, + // after which the base path will be null + while (basePath) { + def candidatePath = Paths.get(basePath.toString(), 'node_modules', packageName) + if (candidatePath.toFile().exists()) { + return candidatePath.toString() + } + + basePath = basePath.getParent() + } + + return null +} + def safeExtGet(prop, fallback) { rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback } @@ -53,7 +72,7 @@ repositories { mavenLocal() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm - url "$rootDir/../node_modules/react-native/android" + url "${resolveModulePath("react-native")}/android" } google() jcenter()