cli icon indicating copy to clipboard operation
cli copied to clipboard

Autolinking on iOS in custom project roots

Open diaozheng999 opened this issue 5 years ago • 9 comments

Ask your Question

We're working on a hybrid native/RN app being managed separately, thus, we're using a non-standard folder structure. Other than using an older version of platform-ios, is there another way for us to perform autolinking with the RN project in a separate directory?

Details

Prior to #768, we were able to pass a custom root to be able to use able to use autolinking. However, the pull request changed the way autolinking is being handled, and the custom root is no longer being respected, resulting in the following message:

$ pod install
Command `config` unrecognized. Make sure that you have run `npm install` and that you are inside a react-native project.

[!] Invalid `Podfile` file: A JSON text must at least contain two octets!.

 #  from /Users/xx/Desktop/Projects/my-ios-app/Podfile:44
 #  -------------------------------------------
 #    
 >    use_native_modules!($rn_path)
 #  end
 #  -------------------------------------------

[!] Passing custom root to use_native_modules! is deprecated.
    - CLI detects root of the project automatically. The "../my-react-native"
    argument was ignored.

Our podfile on the native side looks like this:

# We need to use_frameworks! because Swift pods (esp. Alamofire)                                                                                                                                                                                                              
use_frameworks!
# ignore all warnings from all pods                                                                                                                                                                                                                                           
inhibit_all_warnings!

# Define react native path and node_modules                                                                                                                                                                                                                                   
$rn_path = ENV['REACT_NATIVE_PATH'] || '../my-react-native'
$node_modules = "#{$rn_path}/node_modules"

# get the new autolinking feature                                                                                                                                                                                                                                             
require_relative "#{$node_modules}/@react-native-community/cli-platform-ios/native_modules"


def react_native_pods
  pod 'FBLazyVector', :path => "#{$node_modules}/react-native/Libraries/FBLazyVector"
  pod 'FBReactNativeSpec', :path => "#{$node_modules}/react-native/Libraries/FBReactNativeSpec"
  # all of React's base pods...
  use_native_modules!($rn_path)
end

def native_pods
  pod 'Siren', '~>3.4.0'
  pod 'Alamofire', '~> 4.5'
  # other non-React pods...
end

target '...' do
  react_native_pods
  native_pods
end

diaozheng999 avatar Dec 11 '19 02:12 diaozheng999

+1

mengpeilee avatar Dec 12 '19 06:12 mengpeilee

@diaozheng999 Wanna ask you a question. Did you success on Android yet? we're also using a non-standard folder structure. I wanna know how you do auto-linking & react-native link on Android.

mengpeilee avatar Dec 12 '19 06:12 mengpeilee

Sorry for the late reply,

In the android app, we do:

In /build.gradle, we define our variables:

buildscript {
  react = [
    root: "../react-native"
  ]
}

In app/build.gradle, we have:


apply from: file("$project.react.root/node_modules/@react-native-community/cli-platform-android/native_modules.gradle")
applyNativeModulesAppBuildGradle(project, rootProject.react.root)

In our RN project, we have a react-native.config.js that contains the following:

const path = require("path");

// get the relative path of the android project from the react native project
const target = path.resolve(process.env.REACT_NATIVE_ANDROID_SOURCE || "../android-app");
const sourceDir = path.relative(path.resolve("."), target);

module.exports = {
  project: {
    android: {
      sourceDir,
    },
  },
};

diaozheng999 avatar Jan 07 '20 06:01 diaozheng999

@diaozheng999 Not sure if it helps in your case, but I did the following to get it working for me: I first removed the parameter for use_native_modules!($rn_path) to use_native_modules!. Then I hard coded the path to my custom project and it was able to resolve the correct modules.

module.exports = {
    project: {
      ios: {
        project: "./path/ios/MyStartupProject.xcodeproj"
      },
      android: {
        sourceDir: "./path/android/"
      }
    }
};

Just check with react-native configuntil you get it right.

bondehagen avatar Jan 26 '20 14:01 bondehagen

@bondehagen where are you hard coding this?

thomasMinshull avatar Nov 02 '20 19:11 thomasMinshull

I have the same problem when seperate native ios and anroid out of the react native project folder.

kezhongsun avatar Nov 27 '20 09:11 kezhongsun

I am having trouble with this issue. Is this still an unresolved issue or is there a workaround?

dallas-lbg avatar Feb 03 '21 18:02 dallas-lbg

+1

ziggear avatar Apr 08 '22 10:04 ziggear

@diaozheng999 I have the same problem Can you tell me how you finally solved it?

stickor avatar May 13 '22 08:05 stickor

There hasn't been any activity on this issue in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

github-actions[bot] avatar Nov 29 '22 03:11 github-actions[bot]