<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Iacopo Pazzaglia</title>
    <description>The latest articles on Forem by Iacopo Pazzaglia (@iacopo87).</description>
    <link>https://forem.com/iacopo87</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F218792%2F9bd67ec4-8c34-4036-9aa5-b5bace80febe.jpeg</url>
      <title>Forem: Iacopo Pazzaglia</title>
      <link>https://forem.com/iacopo87</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/iacopo87"/>
    <language>en</language>
    <item>
      <title>React Native: How to change the default folder structure</title>
      <dc:creator>Iacopo Pazzaglia</dc:creator>
      <pubDate>Wed, 22 Jan 2020 18:40:42 +0000</pubDate>
      <link>https://forem.com/iacopo87/react-native-how-to-change-the-default-folder-structure-47lf</link>
      <guid>https://forem.com/iacopo87/react-native-how-to-change-the-default-folder-structure-47lf</guid>
      <description>&lt;h6&gt;
  
  
  Cover photo taken from &lt;a href="https://www.pexels.com/es-es/foto/codigo-datos-efecto-desenfocado-escribiendo-un-guion-2653362/?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels"&gt;&lt;em&gt;Pexels&lt;/em&gt;&lt;/a&gt;
&lt;/h6&gt;

&lt;p&gt;Hello, &lt;/p&gt;

&lt;p&gt;have you ever had the need to change the default React Native folder structure and you spent hours to figure out the correct setup? 🙌&lt;/p&gt;

&lt;p&gt;Well, if this is case in this post I'll highlight all the changes needed to move the folders &lt;code&gt;android&lt;/code&gt; and &lt;code&gt;ios&lt;/code&gt; inside a parent folder called &lt;code&gt;native&lt;/code&gt;.&lt;br&gt;
I assume that the project is created with the command &lt;code&gt;react-native init&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;From: 

├── ios
├── android

To:

└── native
    ├── ios
    ├── android

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Android
&lt;/h1&gt;

&lt;p&gt;Let's start modifying the &lt;code&gt;package.json&lt;/code&gt; to specify the new root of the android project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;   "scripts": {
&lt;span class="gd"&gt;-    "android": "react-native run-android",
&lt;/span&gt;&lt;span class="gi"&gt;+    "android": "react-native run-android --root './native'",
&lt;/span&gt;     "ios": "react-native run-ios",

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we need to create a file called &lt;code&gt;react-native-config.js&lt;/code&gt; that is a file used by the react native CLI where we can modify the project &lt;a href="https://github.com/react-native-community/cli/blob/master/docs/configuration.md"&gt;configuration&lt;/a&gt;.&lt;br&gt;
In this file we set the source directory of the Android project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;android&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;sourceDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./native/android&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point we configured the CLI to locate the android folder but we still need to update the references of the &lt;code&gt;node_modules&lt;/code&gt; folder in each gradle file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;settings.gradle&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
&lt;/span&gt;&lt;span class="gi"&gt;+apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;build.gradle&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;         mavenLocal()
         maven {
             // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
&lt;span class="gd"&gt;-            url("$rootDir/../node_modules/react-native/android")
&lt;/span&gt;&lt;span class="gi"&gt;+            url("$rootDir/../../node_modules/react-native/android")
&lt;/span&gt;         }
         maven {
             // Android JSC is installed from npm
&lt;span class="gd"&gt;-            url("$rootDir/../node_modules/jsc-android/dist")
&lt;/span&gt;&lt;span class="gi"&gt;+            url("$rootDir/../../node_modules/jsc-android/dist")
&lt;/span&gt;         }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app/build.gradle&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-apply from: "../../node_modules/react-native/react.gradle"
&lt;/span&gt;&lt;span class="gi"&gt;+apply from: "../../../node_modules/react-native/react.gradle"
&lt;/span&gt;
 /**
  * Set this to true to create two separate APKs instead of one:

     into 'libs'
 }

-apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
&lt;span class="gi"&gt;+apply from: file("../../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All these changes reflect the moving of the directory one level deeper.&lt;/p&gt;

&lt;h1&gt;
  
  
  iOS
&lt;/h1&gt;

&lt;p&gt;In iOS as well we need to modify the &lt;code&gt;package.json&lt;/code&gt; to specify the new root of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;   "scripts": {
&lt;span class="gd"&gt;-    "ios": "react-native run-ios",
&lt;/span&gt;&lt;span class="gi"&gt;+    "ios": "react-native run-ios --project-path './native/ios'",
&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to update the the references of the &lt;code&gt;node_modules&lt;/code&gt; folder in the native project. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Podfile&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here I put a partial &lt;code&gt;diff&lt;/code&gt; of the &lt;code&gt;Podfile&lt;/code&gt; to highlight the changes needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+    pod 'FBLazyVector', :path =&amp;gt; "../../node_modules/react-native/Libraries/FBLazyVector"
&lt;/span&gt;&lt;span class="gd"&gt;-    pod 'FBLazyVector', :path =&amp;gt; "../node_modules/react-native/Libraries/FBLazyVector"
&lt;/span&gt;&lt;span class="gi"&gt;+    pod 'FBReactNativeSpec', :path =&amp;gt; "../../node_modules/react-native/Libraries/FBReactNativeSpec"
&lt;/span&gt;&lt;span class="gd"&gt;-    pod 'FBReactNativeSpec', :path =&amp;gt; "../node_modules/react-native/Libraries/FBReactNativeSpec"
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after the file is update delete the &lt;code&gt;build&lt;/code&gt; folder inside the iOS project if exist and execute &lt;code&gt;pod install&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;native/ios/&amp;lt;your project name&amp;gt;.xcodeproj/project.pbxproj&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally we need to update the script under &lt;code&gt;Build Phases-&amp;gt;Start Packager&lt;/code&gt; in Xcode &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vSuq1Iif--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/58a6b2hclq827p6w9hyi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vSuq1Iif--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/58a6b2hclq827p6w9hyi.png" alt="Build Phases. Start Packager script"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Extra
&lt;/h1&gt;

&lt;p&gt;The last thing to do is to update the &lt;code&gt;.gitignore&lt;/code&gt; file to reflect the changes made.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; # Android/IntelliJ
 #
&lt;span class="gd"&gt;-build/
&lt;/span&gt;&lt;span class="gi"&gt;+native/build/
&lt;/span&gt;
 # CocoaPods
&lt;span class="gd"&gt;-/ios/Pods/
&lt;/span&gt;&lt;span class="gi"&gt;+native/ios/Pods/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Now the project is ready to be executed using the usual &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt; command 🎉&lt;/p&gt;

&lt;p&gt;Let me know in the comments if you know a more straightforward way to achieve the same result!&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>ios</category>
      <category>android</category>
    </item>
  </channel>
</rss>
