<?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>DEV Community: Kelsius</title>
    <description>The latest articles on DEV Community by Kelsius (@kelsiusdeg).</description>
    <link>https://dev.to/kelsiusdeg</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%2F233611%2F2ba21faf-afb6-48e6-9021-d8d079267284.jpg</url>
      <title>DEV Community: Kelsius</title>
      <link>https://dev.to/kelsiusdeg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kelsiusdeg"/>
    <language>en</language>
    <item>
      <title>React Native: Android debug APK without development server</title>
      <dc:creator>Kelsius</dc:creator>
      <pubDate>Fri, 20 Sep 2019 23:08:23 +0000</pubDate>
      <link>https://dev.to/kelsiusdeg/react-native-android-debug-apk-without-development-server-2l80</link>
      <guid>https://dev.to/kelsiusdeg/react-native-android-debug-apk-without-development-server-2l80</guid>
      <description>&lt;p&gt;By default React Native debug builds require the development server to run, as the server is responsible for providing the JS bundle and the assets in debug mode.&lt;br&gt;
If however, you find yourself in need of a debug APK which can run without the development server, you can manually package the debug bundle along with the generated APK.&lt;/p&gt;

&lt;p&gt;There are two ways of accomplishing this: &lt;/p&gt;
&lt;h2&gt;
  
  
  bundleInDebug
&lt;/h2&gt;

&lt;p&gt;In your &lt;strong&gt;app/build.gradle&lt;/strong&gt; file you should see a large commented out block that explains which settings you can set in the &lt;code&gt;project.ext.react&lt;/code&gt; map inside your gradle file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faogwf9wnt9b1ib4mynox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faogwf9wnt9b1ib4mynox.png" alt="Screenshot of app/build.gradle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The one that is interesting to us is &lt;code&gt;bundleInDebug: true&lt;/code&gt;. &lt;br&gt;
Look for the &lt;code&gt;project.ext.react&lt;/code&gt; map (should be right after the commented out block) in your gradle configuration file and add the &lt;code&gt;bundleInDebug: true&lt;/code&gt; entry to the map:&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;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;

    &lt;span class="c1"&gt;//ADD THIS LINE &lt;/span&gt;
    &lt;span class="nx"&gt;bundleInDebug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;This tells gradle to actually bundle the JS code and assets and package those files into the APK instead of serving them up from the dev server.&lt;/p&gt;

&lt;p&gt;If you try this out, you will notice that while your app now runs without the dev server it will still display yellowbox warning messages. This is because it is still a development build. If you want to make it a production build, which will have no warnings and will have a minified JS bundle, add the &lt;code&gt;devDisabledInDebug: true&lt;/code&gt; entry to the &lt;code&gt;project.ext.react&lt;/code&gt; map.&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;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;

    &lt;span class="nx"&gt;bundleInDebug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

    &lt;span class="c1"&gt;//ADD THIS LINE&lt;/span&gt;
    &lt;span class="nx"&gt;devDisabledInDebug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;debug&lt;/em&gt; vs &lt;em&gt;release&lt;/em&gt; refers to the Android build types, as specified in gradle, whereas &lt;em&gt;development&lt;/em&gt; vs &lt;em&gt;production&lt;/em&gt; affects the behaviour of the JS bundle.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;When you now create an APK with &lt;code&gt;./gradlew assembleDebug&lt;/code&gt; or by running &lt;code&gt;react-native&lt;br&gt;
run-android&lt;/code&gt; it will be a debug APK, bundled with the minified JS bundle and all assets,capable of running without the dev server.&lt;/p&gt;

&lt;h2&gt;
  
  
  react-native bundle
&lt;/h2&gt;

&lt;p&gt;If, for some reason, you want or need to manually invoke the &lt;code&gt;react-native bundle&lt;/code&gt; command to bundle your JS and your assets, the way to build a development-server-independent APK is as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/generated/assets/react/debug/index.android.bundle --assets-dest ./android/app/build/res/react/debug&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's unpack what is happening there:&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bundle&lt;/code&gt; command is simply the command used to bundle the JS and the assets. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;--dev false&lt;/code&gt; tells it to not bundle it using development mode, i.e. disable warnings and minify the bundle. This corresponds to the &lt;code&gt;devDisabledInDebug: true&lt;/code&gt; setting above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--platform android&lt;/code&gt; is self explanatory. Needs to be set, as the default is "ios".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--entry-file index.js&lt;/code&gt; is also quite self explanatory. If your entry file is named differently, then you should of course write that name there.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--bundle-output&lt;/code&gt; here is where it gets interesting. How do we know the path for the bundle output? In order for gradle to be able to find the bundle it must be in a path where gradle expects it and also be named the way gradle expects it. &lt;br&gt;
This information can be found in the &lt;strong&gt;node_modules/react-native/react.gradle&lt;/strong&gt; file. &lt;/p&gt;

&lt;p&gt;Find the line where the variables &lt;code&gt;jsBundleDir&lt;/code&gt; and &lt;code&gt;resourcesDir&lt;/code&gt; are being defined and assigned values to:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fc8id51zwpontpmqx216p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fc8id51zwpontpmqx216p.png" alt="Screenshot of the lines in the react.gradle file where jsBundleDir and resourcesDir are being defined"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;buildDir&lt;/code&gt; is the &lt;strong&gt;android/app/build directory&lt;/strong&gt; in your project and &lt;code&gt;targetPath&lt;/code&gt; is either &lt;em&gt;debug&lt;/em&gt; or &lt;em&gt;release&lt;/em&gt;, depending on the build type of your build.&lt;/p&gt;

&lt;p&gt;These are the paths where gradle expects to find the JS bundle and the assets to be packaged with the APK.&lt;/p&gt;

&lt;p&gt;If, for some reason, you want to override those locations (or any other build settings), do not change them in the &lt;strong&gt;react.gradle&lt;/strong&gt; file directly, but rather in your app level build.gradle file inside the project.ext.react map.&lt;/p&gt;

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