{"data":{"markdownRemark":{"frontmatter":{"title":"Guide for React Native development on Ubuntu/Elementary","date":"January 2019"},"timeToRead":6,"html":"<p>Originally posted on <a href=\"https://gist.github.com/ajitid/540bd1dcb30290c8d779c34fb898a47a\">GitHub Gist</a></p>\n<h3>Assumptions</h3>\n<ul>\n<li>This guide mainly covers steps (including and) after ejection from <code class=\"language-text\">create-react-native-app</code> (CRNA).</li>\n<li>You should know how to run your app using CRNA and Expo app from Play Store. Two issue I've faced and fixed - (1) watchers fixed permanently by <code class=\"language-text\">echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf &amp;&amp; sudo sysctl -p</code> and (2) by using yarn over npmv5 to avoid buggy installation of CRNA projects as well as of other packages that are installed in it later.</li>\n<li>This guide does not cover setting up any Android emulator or using Android Studio (we will go full cli instead).</li>\n</ul>\n<h3>What do you need?</h3>\n<ul>\n<li>~600MB internet data</li>\n<li>Android phone.</li>\n<li>Node and npm installed with <code class=\"language-text\">create-react-native-app</code> globally</li>\n<li>Time</li>\n</ul>\n<p>If something doesn't works at step x, look at step x+1 for possible solution. And if still doesn't works, ask then.</p>\n<h2>Setting up</h2>\n<ol>\n<li>Install Java with <code class=\"language-text\">sudo apt install openjdk-8-jdk</code> (or download zip from Oracle Java website and set its PATH manually).</li>\n<li>Go to <a href=\"https://developer.android.com/studio/index.html#downloads\">Android Studio's \"Download options\"</a>. Scroll to near bottom where it says \"Get just the command line tools\". \"Download for Linux\" (>=130MB) from there.</li>\n<li>You can extract this zip anywhere but do make sure to extract it inside an empty folder (if after extracting directory looks like <code class=\"language-text\">~/android/tools/</code> (<code class=\"language-text\">tools</code> being the extracted folder) then <code class=\"language-text\">~/android/</code> must be initially empty (You'll know later why). From now we'll consider <code class=\"language-text\">~/android/</code> is where you extracted).</li>\n<li>Navigate to <code class=\"language-text\">~/android/tools/bin/</code> and open a terminal there. Now use command <code class=\"language-text\">./sdkmanager --list</code>. This returns a list of what is/are downloaded and what can be downloaded.</li>\n<li>We'll need <code class=\"language-text\">adb</code> to install any compiled apk to phone. For that <code class=\"language-text\">platform-tools</code> is needed. A SDK is needed too. For it we will use Marshmallow 6.x (API level 23).</li>\n<li>Install these by <code class=\"language-text\">./sdkmanager --list  &quot;platform-tools&quot; &quot;platforms;android-23&quot;</code>. Accept any license agreement you encounter. You can even install Google APIs here by <code class=\"language-text\">./sdkmanager &quot;add-ons;addon-google_apis-google-23&quot;</code> but that is optional (this Google APIs gets downloaded quickly &#x3C;1MB).</li>\n<li>Make a project from <code class=\"language-text\">create-react-native-app</code> and use command <code class=\"language-text\">yarn eject</code> in it. Make sure to install yarn and not use npmv5 before making project.</li>\n<li>Choose <code class=\"language-text\">React Native: I&#39;d like a regular React Native project.</code> for first question. Next two subsequent questions ask about name of app you want to give and project name of that app used by Android Studio. Give whatever name you like. From here-on we'll consider your project lives in <code class=\"language-text\">~/a-crna-app/</code>. <code class=\"language-text\">~/a-crna-app/</code> should now be filled with two folders <code class=\"language-text\">android/</code> and <code class=\"language-text\">ios/</code>.</li>\n<li>Navigate to <code class=\"language-text\">~/a-crna-app/android/</code>. You'll see a <code class=\"language-text\">gradlew</code> here. Use <code class=\"language-text\">./gradlew build</code> here. You'll get an error of \"SDK location not found. Define location with sdk.dir in the local.properties or...\". We'll do what's asked here. Now being inside <code class=\"language-text\">~/a-crna-app/android/</code>, use terminal and run <code class=\"language-text\">touch local.properties</code>. Open this <code class=\"language-text\">local.properties</code> with editor and write <code class=\"language-text\">sdk.dir=~/android/</code>. Do remember, you've extracted <code class=\"language-text\">tools</code> folder inside this <code class=\"language-text\">~/android/</code>. Now save <code class=\"language-text\">local.properties</code>.</li>\n<li>Rerun <code class=\"language-text\">./gradlew build</code>. You may encounter another error saying that you haven't accepted license agreement. Open a new terminal, <code class=\"language-text\">cd</code> into <code class=\"language-text\">~/android/tools/bin/</code> and use <code class=\"language-text\">./sdkmanager --licenses</code>. You'll need to do <em>y-enter</em> a couple of times here (maybe 4 or 5 times) to accept all remaining license agreements.</li>\n<li>Android SDK tools have an issue of being 32-bits but running in x64 OS. This is fixed by <code class=\"language-text\">sudo apt-get install libstdc++6:i386 libgcc1:i386 zlib1g:i386 libncurses5:i386</code>. If you don't do this, gradle build step hangs at one point.</li>\n<li>Now go back and and run <code class=\"language-text\">./gradlew build</code>. This runs with 2 'Configuring (x/2)' steps. First step downloads Gradle (~80MB) and next step installs <code class=\"language-text\">extras;android;m2repository</code> from <code class=\"language-text\">./sdkmanager</code> of Android. I have no idea how much data this takes (maybe 100-200MB). I'm telling about about internet data of each, so that you can wait accordingly, plus wait for its extraction from a zip.</li>\n<li>If you license isn't accepted do what is said in step 10 again. If this doesn't fixes license question during gradle build, chances are you didn't had empty folder as told in step 3.</li>\n<li>Everything that you have downloaded till now, you won't need to download them again in the future for any new project you create.</li>\n<li>\n<p>Now you may have seen instructors doing <code class=\"language-text\">yarn run android</code> in <code class=\"language-text\">~/a-crna-app/</code> if they have Mac. What this does internally is to start Metro Bundler to build and watch for any changes in the project and installing app using <code class=\"language-text\">adb</code> command. Now there are various things to do here.</p>\n<ol>\n<li>\n<p>Adding <code class=\"language-text\">adb</code> to path</p>\n<ul>\n<li>Open up <code class=\"language-text\">~/.bashrc</code> or <code class=\"language-text\">~/.zshrc</code> using any editor. Go to bottom and type <code class=\"language-text\">export PATH=&quot;$PATH:/home/ajitid/android/platform-tools/&quot;</code>.</li>\n<li>If you're using Fish shell you'll need to write <code class=\"language-text\">set PATH $PATH &quot;/home/ajitid/codes/android/platform-tools/&quot;</code> in <code class=\"language-text\">~/.config/fish/config.fish</code> file.</li>\n<li>Ubuntu and Elementary comes with Bash as default. And <code class=\"language-text\">ajitid</code> is my user name in the lines above. Use yours there.</li>\n<li>\n<p>To make terminal know about the changes you made, you'll need to either:-</p>\n<ul>\n<li>close terminal and open it back again.</li>\n<li><code class=\"language-text\">source</code> that file. If changed <code class=\"language-text\">~/.bashrc</code> type in terminal <code class=\"language-text\">source ~/.bashrc</code> and hit enter. That terminal session now has reloaded <code class=\"language-text\">.bashrc</code>. You'll need to this for every terminal that you've opened and you want to use them later.</li>\n</ul>\n</li>\n</ul>\n</li>\n<li>\n<p>ADB device recognition</p>\n<ol>\n<li>From your phone, go to Settings > About phone. Now scroll to bottom and find 'Build number'.</li>\n<li>Make continous taps on it (8 times) till you see \"You are now a developer!\". This doesn't voids warranty so don't freak out.</li>\n<li>Press back button. Third last option in Settings on stock Android will be \"Developer options\". If you can't find it, then there is search bar/button in Settings in most of devices now, search from it. Tap on Developer options.</li>\n<li>Scroll a bit to get to Debugging section. Enable 'USB debugging' and go back.</li>\n<li>Use USB cable to connect your phone to PC/laptop.</li>\n<li>Type <code class=\"language-text\">adb devices</code> in terminal. You'll see a device is \"unauthorized\". This is your device. If you can't see it, probably reconnecting will list your device.</li>\n<li>Look at your phone. It'll ask for \"Allow USB debugging?\". Check on \"Always allow from this computer\" and tap OK.</li>\n<li>Rerun <code class=\"language-text\">adb devices</code>. \"unauthorized\" will be replaced by \"device\" now.</li>\n</ol>\n</li>\n<li>\n<p>As told before, <code class=\"language-text\">yarn run android</code> runs \"Metro Bundler\". Problem is that this sometimes stops unexpectedly or runs but doesn't works. In Mac you can see it opens a new terminal for Metro Bundler but in Ubuntu it doesn't. So instead, we'll run two different commands:-</p>\n<ol>\n<li><code class=\"language-text\">yarn start</code> to run Metro Bundler.</li>\n<li>From now on your Android device needs to be connected using USB to install and update any changes made to app.</li>\n<li>Once <code class=\"language-text\">yarn start</code> completes its loading of dependency graph, use <code class=\"language-text\">yarn run android</code> in a new terminal to install app on device. (Don't stop <code class=\"language-text\">yarn start</code> as it watches for changes).</li>\n<li>Do make sure to have <code class=\"language-text\">yarn start</code> running before issuing <code class=\"language-text\">yarn run android</code>.</li>\n<li>Once <code class=\"language-text\">yarn run android</code> completes, you'll see a green bar on top in phone telling it is loading from localhost:8081. If you want to see its loading progress you can switch to the terminal where <code class=\"language-text\">yarn start</code> is running.</li>\n</ol>\n</li>\n</ol>\n</li>\n<li>You'll have successfully running Android app by now.</li>\n<li>For every next CRNA you make and eject, only steps that you need to do is to make a <code class=\"language-text\">local.properties</code> and add sdk path to it, and issuing <code class=\"language-text\">./gradlew build</code>.</li>\n</ol>"}},"pageContext":{"id":"a67038dd-9be6-58ac-b569-13e23459ed36"}}