Files
react-native/website/versioned_docs/0.39/android-building-from-source.md
T
2017-10-27 11:51:58 -07:00

36 lines
13 KiB
Markdown

---
id: version-0.39-android-building-from-source
title: android-building-from-source
original_id: android-building-from-source
---
<a id="content"></a><h1><a class="anchor" name="building-react-native-from-source"></a>Building React Native from source <a class="hash-link" href="docs/android-building-from-source.html#building-react-native-from-source">#</a></h1><div><p>You will need to build React Native from source if you want to work on a new feature/bug fix, try out the latest features which are not released yet, or maintain your own fork with patches that cannot be merged to the core.</p><h2><a class="anchor" name="prerequisites"></a>Prerequisites <a class="hash-link" href="docs/android-building-from-source.html#prerequisites">#</a></h2><p>Assuming you have the Android SDK installed, run <code>android</code> to open the Android SDK Manager.</p><p>Make sure you have the following installed:</p><ol><li>Android SDK version 23 (compileSdkVersion in <a href="https://github.com/facebook/react-native/blob/master/ReactAndroid/build.gradle" target="_blank"><code>build.gradle</code></a>)</li><li>SDK build tools version 23.0.1 (buildToolsVersion in <a href="https://github.com/facebook/react-native/blob/master/ReactAndroid/build.gradle" target="_blank"><code>build.gradle</code></a>)</li><li>Local Maven repository for Support Libraries (formerly <code>Android Support Repository</code>) &gt;= 17 (for Android Support Library)</li><li>Android NDK (download links and installation instructions below)</li></ol><h3><a class="anchor" name="point-gradle-to-your-android-sdk"></a>Point Gradle to your Android SDK: <a class="hash-link" href="docs/android-building-from-source.html#point-gradle-to-your-android-sdk">#</a></h3><p><strong>Step 1:</strong> Set environment variables through your local shell.</p><p>Note: Files may vary based on shell flavor. See below for examples from common shells.</p><ul><li>bash: <code>.bash_profile</code> or <code>.bashrc</code></li><li>zsh: <code>.zprofile</code> or <code>.zshrc</code></li><li>ksh: <code>.profile</code> or <code>$ENV</code></li></ul><p>Example:</p><div class="prism language-javascript">export ANDROID_SDK<span class="token operator">=</span><span class="token operator">/</span>Users<span class="token operator">/</span>your_unix_name<span class="token operator">/</span>android<span class="token operator">-</span>sdk<span class="token operator">-</span>macosx
export ANDROID_NDK<span class="token operator">=</span><span class="token operator">/</span>Users<span class="token operator">/</span>your_unix_name<span class="token operator">/</span>android<span class="token operator">-</span>ndk<span class="token operator">/</span>android<span class="token operator">-</span>ndk<span class="token operator">-</span>r10e</div><p><strong>Step 2:</strong> Create a <code>local.properties</code> file in the <code>android</code> directory of your react-native app with the following contents:</p><p>Example:</p><div class="prism language-javascript">sdk<span class="token punctuation">.</span>dir<span class="token operator">=</span><span class="token operator">/</span>Users<span class="token operator">/</span>your_unix_name<span class="token operator">/</span>android<span class="token operator">-</span>sdk<span class="token operator">-</span>macosx
ndk<span class="token punctuation">.</span>dir<span class="token operator">=</span><span class="token operator">/</span>Users<span class="token operator">/</span>your_unix_name<span class="token operator">/</span>android<span class="token operator">-</span>ndk<span class="token operator">/</span>android<span class="token operator">-</span>ndk<span class="token operator">-</span>r10e</div><h3><a class="anchor" name="download-links-for-android-ndk"></a>Download links for Android NDK <a class="hash-link" href="docs/android-building-from-source.html#download-links-for-android-ndk">#</a></h3><ol><li>Mac OS (64-bit) - <a href="http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip">http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip</a></li><li>Linux (64-bit) - <a href="http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip">http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip</a></li><li>Windows (64-bit) - <a href="http://dl.google.com/android/repository/android-ndk-r10e-windows-x86_64.zip">http://dl.google.com/android/repository/android-ndk-r10e-windows-x86_64.zip</a></li><li>Windows (32-bit) - <a href="http://dl.google.com/android/repository/android-ndk-r10e-windows-x86.zip">http://dl.google.com/android/repository/android-ndk-r10e-windows-x86.zip</a></li></ol><p>You can find further instructions on the <a href="http://developer.android.com/ndk/downloads/index.html" target="_blank">official page</a>.</p><h2><a class="anchor" name="building-the-source"></a>Building the source <a class="hash-link" href="docs/android-building-from-source.html#building-the-source">#</a></h2><h4><a class="anchor" name="1-installing-the-fork"></a>1. Installing the fork <a class="hash-link" href="docs/android-building-from-source.html#1-installing-the-fork">#</a></h4><p>First, you need to install <code>react-native</code> from your fork. For example, to install the master branch from the official repo, run the following:</p><div class="prism language-javascript">npm install <span class="token operator">--</span>save github<span class="token punctuation">:</span>facebook<span class="token operator">/</span>react<span class="token operator">-</span>native#master</div><p>Alternatively, you can clone the repo to your <code>node_modules</code> directory and run <code>npm install</code> inside the cloned repo.</p><h4><a class="anchor" name="2-adding-gradle-dependencies"></a>2. Adding gradle dependencies <a class="hash-link" href="docs/android-building-from-source.html#2-adding-gradle-dependencies">#</a></h4><p>Add <code>gradle-download-task</code> as dependency in <code>android/build.gradle</code>:</p><div class="prism language-javascript"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
dependencies <span class="token punctuation">{</span>
classpath <span class="token string">'com.android.tools.build:gradle:1.3.1'</span>
classpath <span class="token string">'de.undercouch:gradle-download-task:2.0.0'</span>
<span class="token comment" spellcheck="true"> // NOTE: Do not place your application dependencies here; they belong
</span> <span class="token comment" spellcheck="true"> // in the individual module build.gradle files
</span> <span class="token punctuation">}</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span></div><h4><a class="anchor" name="3-adding-the-reactandroid-project"></a>3. Adding the <code>:ReactAndroid</code> project <a class="hash-link" href="docs/android-building-from-source.html#3-adding-the-reactandroid-project">#</a></h4><p>Add the <code>:ReactAndroid</code> project in <code>android/settings.gradle</code>:</p><div class="prism language-javascript"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
include <span class="token string">':ReactAndroid'</span>
<span class="token function">project<span class="token punctuation">(</span></span><span class="token string">':ReactAndroid'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>projectDir <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>
rootProject<span class="token punctuation">.</span>projectDir<span class="token punctuation">,</span> <span class="token string">'../node_modules/react-native/ReactAndroid'</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span></div><p>Modify your <code>android/app/build.gradle</code> to use the <code>:ReactAndroid</code> project instead of the pre-compiled library, e.g. - replace <code>compile 'com.facebook.react:react-native:+'</code> with <code>compile project(':ReactAndroid')</code>:</p><div class="prism language-javascript"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
dependencies <span class="token punctuation">{</span>
compile <span class="token function">fileTree<span class="token punctuation">(</span></span>dir<span class="token punctuation">:</span> <span class="token string">'libs'</span><span class="token punctuation">,</span> include<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'*.jar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
compile <span class="token string">'com.android.support:appcompat-v7:23.0.1'</span>
compile <span class="token function">project<span class="token punctuation">(</span></span><span class="token string">':ReactAndroid'</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span></div><h4><a class="anchor" name="4-making-3rd-party-modules-use-your-fork"></a>4. Making 3rd-party modules use your fork <a class="hash-link" href="docs/android-building-from-source.html#4-making-3rd-party-modules-use-your-fork">#</a></h4><p>If you use 3rd-party React Native modules, you need to override their dependencies so that they don't bundle the pre-compiled library. Otherwise you'll get an error while compiling - <code>Error: more than one library with package name 'com.facebook.react'</code>.</p><p>Modify your <code>android/app/build.gradle</code>, and add:</p><div class="prism language-javascript">configurations<span class="token punctuation">.</span>all <span class="token punctuation">{</span>
exclude group<span class="token punctuation">:</span> <span class="token string">'com.facebook.react'</span><span class="token punctuation">,</span> module<span class="token punctuation">:</span> <span class="token string">'react-native'</span>
<span class="token punctuation">}</span></div><h2><a class="anchor" name="building-from-android-studio"></a>Building from Android Studio <a class="hash-link" href="docs/android-building-from-source.html#building-from-android-studio">#</a></h2><p>From the Welcome screen of Android Studio choose "Import project" and select the <code>android</code> folder of your app.</p><p>You should be able to use the <em>Run</em> button to run your app on a device. Android Studio won't start the packager automatically, you'll need to start it by running <code>npm start</code> on the command line.</p><h2><a class="anchor" name="additional-notes"></a>Additional notes <a class="hash-link" href="docs/android-building-from-source.html#additional-notes">#</a></h2><p>Building from source can take a long time, especially for the first build, as it needs to download ~200 MB of artifacts and compile the native code. Every time you update the <code>react-native</code> version from your repo, the build directory may get deleted, and all the files are re-downloaded. To avoid this, you might want to change your build directory path by editing the <code>~/.gradle/init.gradle</code> file:</p><div class="prism language-javascript">gradle<span class="token punctuation">.</span>projectsLoaded <span class="token punctuation">{</span>
rootProject<span class="token punctuation">.</span>allprojects <span class="token punctuation">{</span>
buildDir <span class="token operator">=</span> <span class="token string">"/path/to/build/directory/${rootProject.name}/${project.name}"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></div><h2><a class="anchor" name="testing"></a>Testing <a class="hash-link" href="docs/android-building-from-source.html#testing">#</a></h2><p>If you made changes to React Native and submit a pull request, all tests will run on your pull request automatically. To run the tests locally, see <a href="/react-native/docs/testing.html" target="">Testing</a>.</p><h2><a class="anchor" name="troubleshooting"></a>Troubleshooting <a class="hash-link" href="docs/android-building-from-source.html#troubleshooting">#</a></h2><p>Gradle build fails in <code>ndk-build</code>. See the section about <code>local.properties</code> file above.</p></div><div class="docs-prevnext"><a class="docs-prev" href="docs/android-ui-performance.html#content">← Prev</a><a class="docs-next" href="docs/activityindicator.html#content">Next →</a></div><p class="edit-page-block">You can <a target="_blank" href="https://github.com/facebook/react-native/blob/master/docs/AndroidBuildingFromSource.md">edit the content above on GitHub</a> and send us a pull request!</p>