mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
36 lines
14 KiB
Markdown
36 lines
14 KiB
Markdown
---
|
|
id: version-0.49-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 class="banner-crna-ejected"><h3>Project with Native Code Required</h3><p>This page only applies to projects made with <code>react-native init</code> or to those made with Create React Native App which have since ejected. For more information about ejecting, please see the <a href="https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md" target="_blank">guide</a> on the Create React Native App repository.</p></div><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>Android Support Repository >= 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"><span class="token keyword">export</span> 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
|
|
<span class="token keyword">export</span> 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="https://developer.android.com/ndk/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-sh">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-gradle"><span class="token operator">...</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:3.1.2'</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 operator">...</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-gradle"><span class="token operator">...</span>
|
|
include <span class="token string">':ReactAndroid'</span>
|
|
|
|
<span class="token function">project</span><span class="token punctuation">(</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 operator">...</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-gradle"><span class="token operator">...</span>
|
|
dependencies <span class="token punctuation">{</span>
|
|
compile <span class="token function">fileTree</span><span class="token punctuation">(</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><span class="token punctuation">(</span><span class="token string">':ReactAndroid'</span><span class="token punctuation">)</span>
|
|
|
|
<span class="token operator">...</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token operator">...</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-gradle">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-gradle">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="building-for-maven-nexus-deployment"></a>Building for Maven/Nexus deployment <a class="hash-link" href="docs/android-building-from-source.html#building-for-maven-nexus-deployment">#</a></h2><p>If you find that you need to push up a locally compiled React Native .aar and related files to a remote Nexus repository, you can.</p><p>Start by following the <code>Point Gradle to your Android SDK</code> section of this page. Once you do this, assuming you have Gradle configured properly, you can then run the following command from the root of your React Native checkout to build and package all required files:</p><div class="prism language-javascript"><span class="token punctuation">.</span><span class="token operator">/</span>gradlew ReactAndroid<span class="token punctuation">:</span>installArchives</div><p>This will package everything that would typically be included in the <code>android</code> directory of your <code>node_modules/react-native/</code> installation in the root directory of your React Native checkout.</p><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="docs/testing.html" target="_blank">Running Tests</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 btn" href="docs/android-ui-performance.html#content">← Previous</a><a class="docs-next btn" href="docs/contributing.html#content">Continue Reading →</a></div><p class="edit-page-block"><a target="_blank" href="https://github.com/facebook/react-native/blob/master/docs/AndroidBuildingFromSource.md">Improve this page</a> by sending a pull request!</p> |