From a912331844081199fc32b805908548af98cb2179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 2 Jul 2019 19:44:35 +0200 Subject: [PATCH 1/2] feat: simplify passing root to gradle autolinking --- .../platform-android/native_modules.gradle | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index 0b09f47d9..e0b34dd0e 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -51,7 +51,7 @@ class ReactNativeModules { private Project project private String packageName private DefaultSettings defaultSettings - private ExtraPropertiesExtension extension + private String root private ArrayList> reactNativeModules private static String LOG_PREFIX = ":ReactNative:" @@ -60,17 +60,17 @@ class ReactNativeModules { this.logger = logger } - void applySettingsGradle(DefaultSettings defaultSettings, ExtraPropertiesExtension extraPropertiesExtension) { + void applySettingsGradle(DefaultSettings defaultSettings, String root) { this.defaultSettings = defaultSettings - this.extension = extraPropertiesExtension + this.root = root this.reactNativeModules = this.getReactNativeConfig() addReactNativeModuleProjects() } - void applyBuildGradle(Project project, ExtraPropertiesExtension extraPropertiesExtension) { + void applyBuildGradle(Project project, String root) { this.project = project - this.extension = extraPropertiesExtension + this.root = root this.reactNativeModules = this.getReactNativeConfig() addReactNativeModuleDependencies() @@ -110,13 +110,6 @@ class ReactNativeModules { * @return */ File getReactNativeProjectRoot() { - if (this.extension.has("reactNativeProjectRoot")) { - File rnRoot = File(this.extension.get("reactNativeProjectRoot")) - // allow custom React Native project roots for non-standard directory structures - this.logger.debug("${LOG_PREFIX}Using custom React Native project root path '${rnRoot.toString()}'") - return rnRoot - } - File androidRoot if (this.project) { @@ -125,8 +118,11 @@ class ReactNativeModules { androidRoot = this.defaultSettings.rootProject.projectDir } - this.logger.debug("${LOG_PREFIX}Using default React Native project root path '${androidRoot.parentFile.toString()}'") - return androidRoot.parentFile + + File rnRoot = new File(androidRoot, this.root) + // allow custom React Native project roots for non-standard directory structures + this.logger.debug("${LOG_PREFIX}Using React Native project root path '${rnRoot.toString()}'") + return rnRoot } /** @@ -220,12 +216,12 @@ class ReactNativeModules { def autoModules = new ReactNativeModules(logger) -ext.applyNativeModulesSettingsGradle = { DefaultSettings defaultSettings -> - autoModules.applySettingsGradle(defaultSettings, ext) +ext.applyNativeModulesSettingsGradle = { DefaultSettings defaultSettings, String root = ".." -> + autoModules.applySettingsGradle(defaultSettings, root) } -ext.applyNativeModulesAppBuildGradle = { Project project -> - autoModules.applyBuildGradle(project, ext) +ext.applyNativeModulesAppBuildGradle = { Project project, String root = ".." -> + autoModules.applyBuildGradle(project, root) def generatedSrcDir = new File(buildDir, "generated/rncli/src/main/java/com/facebook/react") From 928441055822dc0030b60b6a3e7622d23043f674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 2 Jul 2019 20:10:11 +0200 Subject: [PATCH 2/2] improve docs --- docs/autolinking.md | 45 +++++++++++++++++-- .../platform-android/native_modules.gradle | 2 - 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/docs/autolinking.md b/docs/autolinking.md index b62c6957e..f01ab2474 100644 --- a/docs/autolinking.md +++ b/docs/autolinking.md @@ -34,17 +34,56 @@ The implementation ensures that a library is imported only once. If you need to See the implementation of [native_modules.rb](https://github.com/react-native-community/cli/blob/master/packages/platform-ios/native_modules.rb). -> Autolinking assumes your Podfile is in a sub-folder from your `package.json`. If this is not the case, use the first parameter to tell the linker where to find the `package.json` e.g. `use_native_modules!("../../")`. +### Custom root (monorepos) + +The project root is where `node_modules` with `react-native` is. Autolinking script assume your project root to be `".."`, relative to `ios` directory. If you're in a project with custom structure, like this: + +``` +root/ + node_modules + example/ + ios/ +``` + +you'll need to set a custom root. Pass it as an argument to `use_native_modules!`: + +```rb +# example/ios/Podfile +use_native_modules!("../..") +``` ## Platform Android 1. At build time, before the build script is run: - 1. A first Gradle plugin (in `settings.gradle`) runs. It uses the package metadata from `react-native config` to add Android projects. - 1. A second Gradle plugin (in `build.gradle`) runs. It creates a list of React Native packages to include in the generated `/android/build/generated/rn/src/main/java/com/facebook/react/PackageList.java` file. + 1. A first Gradle plugin (in `settings.gradle`) runs `applyNativeModulesSettingsGradle` method. It uses the package metadata from `react-native config` to add Android projects. + 1. A second Gradle plugin (in `app/build.gradle`) runs `applyNativeModulesAppBuildGradle` method. It creates a list of React Native packages to include in the generated `/android/build/generated/rn/src/main/java/com/facebook/react/PackageList.java` file. 1. At runtime, the list of React Native packages, generated in step 1.2, is passed to React Native host. See the implementation of [native_modules.gradle](https://github.com/react-native-community/cli/blob/master/packages/platform-android/native_modules.gradle). +### Custom root (monorepos) + +The project root is where `node_modules` with `react-native` is. Autolinking scripts assume your project root to be `".."`, relative to `android` directory. If you're in a project with custom structure, like this: + +``` +root/ + node_modules + example/ + android/ +``` + +you'll need to set a custom root. Pass it as a second argument to `applyNativeModulesSettingsGradle` and `applyNativeModulesAppBuildGradle` methods: + +```groovy +// example/android/settings.gradle +applyNativeModulesSettingsGradle(settings, "../..") +``` + +```groovy +// example/android/app/build.gradle +applyNativeModulesAppBuildGradle(project, "../..") +``` + ## What do I need to have in my package to make it work? You’re already using Gradle, so Android support will work by default. diff --git a/packages/platform-android/native_modules.gradle b/packages/platform-android/native_modules.gradle index e0b34dd0e..c70a57b14 100644 --- a/packages/platform-android/native_modules.gradle +++ b/packages/platform-android/native_modules.gradle @@ -118,9 +118,7 @@ class ReactNativeModules { androidRoot = this.defaultSettings.rootProject.projectDir } - File rnRoot = new File(androidRoot, this.root) - // allow custom React Native project roots for non-standard directory structures this.logger.debug("${LOG_PREFIX}Using React Native project root path '${rnRoot.toString()}'") return rnRoot }