React-Native 更改 iOS 和 Android 文件夹的默认位置

Ham*_*mid 5 android ios reactjs react-native react-native-cli

根据react-native cli 文档,我们可以根据每个平台为 RN cli 指定自定义配置。我想更改每个平台文件夹的默认位置,所以我将我的自定义添加react-native.config.js到项目的根目录,您可以在这里找到:

module.exports = {
  project: {
    ios: {
      project: './example/ios/example.xcworkspace',
    },
    android: {
      sourceDir: './example/android/',
    },
  },
};
Run Code Online (Sandbox Code Playgroud)

如果我react-native config在根文件夹中运行,可以看到正确的配置:

{
...
  "project": {
    "ios": {
      "sourceDir": "path-to-project-directory/example/ios",
      "folder": "path-to-project-directory",
      "pbxprojPath": "path-to-project-directory/example/ios/example.xcworkspace/project.pbxproj",
      "podfile": "path-to-project-directory/example/ios/Podfile",
      "podspecPath": null,
      "projectPath": "path-to-project-directory/example/ios/example.xcworkspace",
      "projectName": "example.xcworkspace",
      "libraryFolder": "Libraries",
      "sharedLibraries": [],
      "plist": [],
      "scriptPhases": []
    },
    "android": {
      "sourceDir": "path-to-project-directory/example/android/",
      "isFlat": true,
      "folder": "path-to-project-directory",
      "stringsPath": "path-to-project-directory/example/android/app/src/main/res/values/strings.xml",
      "manifestPath": "path-to-project-directory/example/android/app/src/main/AndroidManifest.xml",
      "buildGradlePath": "path-to-project-directory/example/android/build.gradle",
      "settingsGradlePath": "path-to-project-directory/example/android/settings.gradle",
      "assetsPath": "path-to-project-directory/example/android/app/src/main/assets",
      "mainFilePath": "path-to-project-directory/example/android/app/src/main/java/com/example/MainApplication.java",
      "packageName": "com.example",
      "packageFolder": "com/example",
      "appName": "app"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是每当我运行时都会react-native run-ios收到此错误:

error iOS project folder not found. Are you sure this is a React Native project?. Run CLI with --verbose flag for more details.
Run Code Online (Sandbox Code Playgroud)

注意:我正确安装了所有依赖项。这是react-native --v命令的结果:

react-native-cli: 2.0.1
react-native: 0.62.0
Run Code Online (Sandbox Code Playgroud)

小智 0

"react-native": "0.63.4"

对于iOS,您需要--project-path在运行脚本中指定prop,而不是在react-native.config.js中

您的运行脚本应如下所示:

"ios": "react-native run-ios --project-path './<new folder>/ios'"

但之后您需要更新../../node_modules/许多本机文件中的定位模块的路径,例如Podfileios/<your project name>.xcodeproj/project.pbxproj

您可以在这里查看更多信息