From 062b83ed2312627853815630c53adfeef418cff4 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 24 Jun 2020 20:01:17 +0200 Subject: [PATCH] fix: Allow building React Native from source (#131) --- android/app/build.gradle | 16 ++++++++++------ android/react-native-build.gradle | 24 ++++++++++++++++++++++++ android/test-app-util.gradle | 5 +++++ example/package.json | 2 +- package.json | 2 +- test-app.gradle | 11 +++++++++++ 6 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 android/react-native-build.gradle diff --git a/android/app/build.gradle b/android/app/build.gradle index e52ab034a..bcf1114ee 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -5,13 +5,13 @@ buildscript { apply from: "$buildscriptDir/../test-app-util.gradle" repositories { - jcenter() google() + jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" - classpath "com.android.tools.build:gradle:3.6.1" + classpath "com.android.tools.build:gradle:3.6.2" } } @@ -20,8 +20,8 @@ repositories { url("${findNodeModulesPath(rootDir, "react-native")}/android") } - jcenter() google() + jcenter() } apply plugin: "com.android.application" @@ -35,8 +35,8 @@ apply from: file("${testAppDir}/test-app.gradle") applyTestAppModule(project, "com.sample") project.ext.react = [ - enableFlipper: getFlipperVersion(rootDir), - enableHermes: true, + enableFlipper: getFlipperVersion(rootDir), + enableHermes: true, ] android { @@ -87,7 +87,11 @@ dependencies { releaseImplementation files("$hermesPath/hermes-release.aar") debugImplementation files("$hermesPath/hermes-debug.aar") - implementation "com.facebook.react:react-native:+" + if (buildReactNativeFromSource(rootDir)) { + implementation project(':ReactAndroid') + } else { + implementation "com.facebook.react:react-native:+" + } implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion" implementation "androidx.appcompat:appcompat:1.1.0" diff --git a/android/react-native-build.gradle b/android/react-native-build.gradle new file mode 100644 index 000000000..2c6bb795a --- /dev/null +++ b/android/react-native-build.gradle @@ -0,0 +1,24 @@ +buildscript { + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.6.2' + classpath 'de.undercouch:gradle-download-task:4.0.4' + } +} + +allprojects { + repositories { + google() + jcenter() + } + configurations.all { + resolutionStrategy { + dependencySubstitution { + substitute module("com.facebook.react:react-native:+") with project(":ReactAndroid") + } + } + } +} diff --git a/android/test-app-util.gradle b/android/test-app-util.gradle index 4bbe237a0..ba8f0cd16 100644 --- a/android/test-app-util.gradle +++ b/android/test-app-util.gradle @@ -1,5 +1,10 @@ import java.nio.file.Paths +ext.buildReactNativeFromSource = { baseDir -> + def reactNativePath = findNodeModulesPath(baseDir, 'react-native') + return !file("${reactNativePath}/android").exists() +} + ext.findFile = { fileName -> def currentDir = file(rootDir) while (currentDir != null) { diff --git a/example/package.json b/example/package.json index e16f72993..29f2c788c 100644 --- a/example/package.json +++ b/example/package.json @@ -11,7 +11,7 @@ }, "peerDependencies": { "react": "~16.8.6 || ~16.9.0 || ~16.11.0 || ~16.13.1", - "react-native": "^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63", + "react-native": "^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63 || 1000.0.0", "react-native-macos": "^0.60 || ^0.61.39" }, "devDependencies": { diff --git a/package.json b/package.json index 3dd0d5bb8..cdd1ed300 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ }, "peerDependencies": { "react": "~16.8.6 || ~16.9.0 || ~16.11.0 || ~16.13.1", - "react-native": "^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63", + "react-native": "^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63 || 1000.0.0", "react-native-macos": "^0.60 || ^0.61.39" }, "devDependencies": { diff --git a/test-app.gradle b/test-app.gradle index 1de67848d..47fb28d22 100644 --- a/test-app.gradle +++ b/test-app.gradle @@ -1,9 +1,20 @@ import groovy.json.JsonSlurper +import java.nio.file.Paths import org.gradle.initialization.DefaultSettings private static void apply(Settings settings) { def projectDir = settings.findNodeModulesPath(settings.rootDir, "react-native-test-app") + if (settings.buildReactNativeFromSource(settings.rootDir)) { + def buildFile = Paths.get("${projectDir}/android/react-native-build.gradle") + settings.rootProject.buildFileName = settings.rootDir.toPath().relativize(buildFile) + + def reactNativeDir = settings.findNodeModulesPath(settings.rootDir, "react-native") + settings.include(":ReactAndroid") + settings.project(":ReactAndroid") + .projectDir = new File("${reactNativeDir}/ReactAndroid") + } + settings.include(":app") settings.project(":app") .projectDir = new File("${projectDir}/android/app")