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

Posted

技术标签:

【中文标题】React-Native 更改 iOS 和 Android 文件夹的默认位置【英文标题】:React-Native change default location of iOS and Android folder 【发布时间】:2020-07-11 05:48:30 【问题描述】:

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

module.exports = 
  project: 
    ios: 
      project: './example/ios/example.xcworkspace',
    ,
    android: 
      sourceDir: './example/android/',
    ,
  ,
;

如果我在根文件夹中运行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"
    
  

问题是每当我运行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.

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

react-native-cli: 2.0.1
react-native: 0.62.0

【问题讨论】:

非常感谢,它困扰了我 2 天 @Hamid 你是怎么解决这个问题的?即使我也在为同样的问题而苦苦挣扎。 【参考方案1】:

"react-native": "0.63.4"

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

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

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

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

您可以查看更多here

【讨论】:

以上是关于React-Native 更改 iOS 和 Android 文件夹的默认位置的主要内容,如果未能解决你的问题,请参考以下文章

如何在 react-native 中更改应用程序的显示名称

更改项目名称后 react-native run-ios 将不起作用

React-Native iOS 状态栏

如何更改 react-native 中的默认 fontFamily

React-Native 更改 selectTextOnFocus 关键字(选择、全选、粘贴)

react-native ios“无法连接到开发服务器”和“__fbBatchedBridge 未定义”错误