328

I've built my app, I can run it on my local emulator (and also on my android device within the same network by changing debug server).

However, I want to build an APK that I can send to someone without access to the development server and I want them to be able to test application.

I see there is a section Using offline bundle on iOS section of the documentation. But I couldn't figure out how to accomplish the same for android. Is this possible? If so, how?

UPDATE: On the answer to this question (Android failed to load JS bundle) it is said that offline bundle can be downloaded from development server. But when I obtain the bundle from development server the image files can't be loaded.

0

22 Answers 22

353

Following Aditya Singh's answer the generated (unsigned) apk would not install on my phone. I had to generate a signed apk using the instructions here.

The following worked for me:

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

Place the my-release-key.keystore file under the android/app directory in your project folder. Then edit the file ~/.gradle/gradle.properties and add the following (replace **** with the correct keystore password, alias and key password)

MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=****
MYAPP_RELEASE_KEY_PASSWORD=****

If you're using MacOS, you can store your password in the keychain using the instructions here instead of storing it in plaintext.

Then edit app/build.gradle and ensure the following are there (the sections with signingConfigs signingConfig may need to be added) :

...
android {
    ...
    defaultConfig { ... }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }
}
...

Then run the command cd android && ./gradlew assembleRelease ,

For Windows 'cd android' and then run gradlew assembleRelease command , and find your signed apk under android/app/build/outputs/apk/app-release.apk, or android/app/build/outputs/apk/release/app-release.apk

15
  • 3
    app-release.apk is not installing. what would be the issue? Commented Oct 6, 2017 at 17:10
  • 2
    I create the apk, upload to google play, but it not working... some hint? Commented Jan 4, 2018 at 1:01
  • 3
    I can't find the android/app folder, where is this located?
    – DHLopez
    Commented Feb 15, 2018 at 5:44
  • 10
    Thanks, it wasn't, but I found it was because I am using expo for testing, and the create native app npm has a different structure, so for future reference, if anyone gets this problem, just eject your project and then you get those folders, funny how the tutorial tells you to use npm to create the app, and then talks about a structure that doesn't exist when you use it
    – DHLopez
    Commented Feb 27, 2018 at 13:13
  • 1
    @DHLopez Thanks for answering my question :-) softwareengineering.stackexchange.com/questions/367683/… Commented Mar 16, 2018 at 6:15
243

You will have to create a key to sign the apk. Use below to create your key:

 keytool -genkey -v -keystore my-app-key.keystore -alias my-app-alias -keyalg RSA -keysize 2048 -validity 10000

Use a password when prompted

Once the key is generated, use it to generate the installable build:

 react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

Generate the build using gradle

 cd android && ./gradlew assembleRelease

Upload the APK to your phone. The -r flag will replace the existing app (if it exists)

adb install -r ./app/build/outputs/apk/app-release-unsigned.apk

A more detailed description is mentioned here: https://facebook.github.io/react-native/docs/signed-apk-android.html

UPDATE: Based on comments by @shashuec and @Fallen

if you get error

error ENOENT: no such file or directory, open 'android/app/src/main/assets/index.android.bundle' 

run

mkdir android/app/src/main/assets
22
  • 5
    Thanks a lot for this, it worked for me. The only issue I got was this message: "Missing required arguments: bundle-output" en the react-native-bundle part, but is solved by deleting the 2 "\" symbols
    – guinunez
    Commented Jun 16, 2016 at 13:47
  • 70
    I am amazed by the poor documentation of Facebook where they totally omitted the react-native bundle part. Thanks for your answer! Commented Aug 3, 2016 at 20:57
  • 3
    I am working with windows, works with this gradlew.bat assembleRelease as well.
    – Tabares
    Commented Sep 9, 2016 at 20:40
  • 14
    I get this error Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES] . when I try to install the apk. Commented Nov 16, 2016 at 15:21
  • 21
    if you get error ENOENT: no such file or directory, open 'android/app/src/main/assets/index.android.bundle' run mkdir android/app/src/main/assets
    – shashuec
    Commented Mar 22, 2017 at 15:04
55

You should just use android studio for this process. It is just simpler. But first run this command in your react native app directory:

For Newer version of react-native(e.g. react native 0.49.0 & so on...)

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

For Older Version of react-native (0.49.0 & below)

react-native bundle --platform android --dev false --entry-file index.android.js   --bundle-output android/app/src/main/assets/index.android.bundle   --assets-dest android/app/src/main/res/

Then Use android studio to open the 'android' folder in you react native app directory, it will ask to upgrade gradle and some other stuff. go to build-> Generate signed APK and follow the instructions from there. It's really straight forward.

2
  • I also use Android Studio and Xcode to build release versions for my React Native apps... but the android generation could be done with a script as described by @Aditya Singh
    – WiRa
    Commented May 12, 2017 at 8:57
  • This is no longer needed proandroiddev.com/…
    – onmyway133
    Commented Sep 1, 2018 at 22:03
52

If any one want to build without playstore keys then this command would be very helpful.

# react-native run-android --variant=release

After build complete you can find the apk in release build file.

2
  • 8
    the APK you're looking for will be located inside /android/app/build/outputs/apk/release Commented Jul 22, 2020 at 20:47
  • Can someone use the apk generated by this method to restore the react-native code ?
    – Kanhaiya
    Commented Oct 13, 2022 at 18:24
49

Run this command:

react-native run-android --variant=release

Note that --variant=release is only available if you've set up signing with cd android && ./gradlew assembleRelease.

3
27

for React Native 0.49 and over

you should go to project directory on terminal and run that command

1 - mkdir android/app/src/main/assets
2 - react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

if under 0.49

  1 - mkdir android/app/src/main/assets
  2 - react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

Then Use android studio to open the 'android' folder in you react native app directory, it will ask to upgrade gradle and some other stuff. go to build-> Generate signed APK and follow the instructions from there. That okey.

3
  • This is an easy way to do it if you have Android Studio installed (which will take care of using or creating the Keystore). Thanks.
    – leosok
    Commented Oct 31, 2018 at 0:05
  • Hey.. have a quick question.. are the above steps required ? The react native doc does not mention it anywhere..
    – pravin
    Commented Feb 2, 2019 at 18:48
  • 1
    @pravin it is not required, it is one of the method of the generate apk. Commented Feb 3, 2019 at 21:07
17

Try below, it will generate an app-debug.apk on your root/android/app/build/outputs/apk/debug folder

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

then go to android folder and run

./gradlew assembleDebug

1
  • 3
    The APK generated here needs Metro to run...so I think that's still using the dev server
    – nerdlinger
    Commented May 29, 2020 at 17:56
14

If you get Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES].

I tried using @Aditya's method of generating an unsigned APK file and installing that, but when I went to install it on my Android tablet, it gave me an error of Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES].

I imagine this is because the APK file wasn't signed and the tablet was not happy with this. So, after generating the React Native bundle file I was able to generate a signed APK file, as normal, via Android Studio.

By the way, our app is a fully functioning Android app that already exists in the Play Store and we are just adding React Native to it in bite-sized pieces, because it's awesome.

So to summarize, here were my steps:

1) Generate React Native bundle:

react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/<your-package-name>/src/main/assets/index.android.bu‌​ndle --assets-dest android/<your-package-name>/src/main/res/ 

2) Generate a Signed APK file from Android Studio.

3) Install Signed APK File to USB device:

adb -d install -r <path_to_signed_apk> 

The -d flag just tells adb to install on the usb device attached, in the event that you have an emulator running as well. If you want to install on whatever an emulator, drop the -d flag.

4) Profit!

12

Go to project directory and run the command:

cd android && ./gradlew assembleRelease
2
  • 1
    This will generate an unsigned apk if you didn't do required configuration (like Pedram told). Commented Oct 23, 2017 at 14:06
  • 1
    It will create debug, release and unsigned apk in generated folder. My intention was to share release apk. Commented Oct 25, 2017 at 12:56
10

Hopefully it will help new beginners

Official doc here

If you dont have keystore than use before command else skip

Generating a signing key / Keystore file You can generate a private signing key using keytool. On Windows keytool must be run from C:\Program Files\Java\jdkx.x.x_x\bin.

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

you will get a file like my-release-key.keystore

Setting up gradle variables Place the my-release-key.keystore file under the android/app directory in your project folder. Edit the file android/gradle.properties and add the following (replace ***** with the correct keystore password, alias and key password), enableAapt2 set false is workaround , as android gradle version 3.0 problem

MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=*****
MYAPP_RELEASE_KEY_PASSWORD=*****

android.enableAapt2=false

then add these app/buid.gradle (app)

below default config

signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }

and Inside Build type release { }

 signingConfig signingConfigs.release

then simply run this command in android studio terminal Below commands will automate above all answers

if windows

cd android
gradlew assembleRelease

if linux / mac

$ cd android
$ ./gradlew assembleRelease

if you got any error delete all build folder and run command

gradlew clean

than again

gradlew assembleRelease
3
  • 2
    omg gradle clean...I have been struggling with this for a while getting those build errors, thank you so much for this answer!!
    – nerdlinger
    Commented May 29, 2020 at 17:59
  • how to upload the Keystore file on the play store console? Commented Sep 1, 2021 at 11:43
  • @AhsanAbrar first thing its optional!! then you dont upload, play console will generate sha keys for you and automatically sign your apps. generally people develop keystore file using android studio and then sign it at their end and upload signed apk to playstore. but in that case you have to safely store keystore app to do future uploads. Commented Sep 3, 2021 at 6:20
8

I have another solution:- Generating a signing key You can generate a private signing key using keytool. On Windows keytool must be run from C:\Program Files\Java\jdkx.x.x_x\bin.

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

Place the my-release-key.keystore file under the android/app directory in your project folder.

Edit the file android/app/build.gradle in your project folder and add the signing config,

android {
....
signingConfigs { 
  release { 

      storeFile file('my-release-key.keystore') 
      storePassword 'yourpassword' 
      keyAlias 'my-key-alias' 
      keyPassword 'yourpassword' 

  } 
}}}


buildTypes {release {signingConfig signingConfigs.release}}

and run

gradlew assembleRelease

This will give you two files at android\app\build\outputs\apk app-release.apk and app-release-unsigned.apk now install the app-release.apk in your android device.

2
  • Where is the android\app folder? I don't see it under my project (I only see node_modules, and inside of it, there's no android/app)
    – DHLopez
    Commented Feb 15, 2018 at 5:46
  • 1
    @DHLopez you have to use the pure React Native project to have access to the android folder. If you're using Expo, then you have to detach the Expo project using expo eject. Commented Jun 10, 2019 at 21:32
8

If you get an error involving index.android.js. This is because you are using the new React-native version (I am using 0.55.4) Just replace the "index.android.js" to "index.js"

react-native bundle --platform android --dev false --entry-file index.js   --bundle-output android/app/src/main/assets/index.android.bundle   --assets-dest android/app/src/main/res/
0
6

First

cd android

then Run

./gradlew installRelease

Finally You will able to find the apk here.

Project\android\app\build\outputs\apk\release\app-release.apk
1
  • Does it make sense to use installRelease here?
    – dipakbari4
    Commented Sep 29, 2021 at 6:50
6

For me this worked (MACOS)

Open terminal

cd project_directory

run this command

yarn start

Open another terminal

run these commands

cd android
./gradlew assembleRelease
5

The GUI way in Android Studio

  • Have the Android app part of the React Native app open in Android Studio (this just means opening the android folder of your react-native project with Android Studio)
  • Go to the "Build" tab at the top
  • Go down to "Build Bundle(s) / APK(s)"
  • Then select "Build APK(s)"
  • This will walk you through a process of locating your keys to sign the APK or creating your keys if you don't have them already which is all very straightforward. If you're just practicing, you can generate these keys and save them to your desktop.
  • Once that process is complete and if the build was successful, there will be a popup down below in Android Studio. Click the link within it that says "locate" (the APK)
  • That will take you to the apk file, move that over to your android device. Then navigate to that file on the Android device and install it.
5

enter image description here

enter image description here

  • Some useful script for WINDOW Users

Copy-paste these code in package.json file under "script":{}

 * VS Code
   --Enable NPM SCRIPT

   --Now you can directly run scripts from here


    "android": "react-native run-android",
    "clean-run-android": "cd android && ./gradlew clean && cd.. && react-native run-android  ",
    "start": "node node_modules/react-native/local-cli/cli.js start --reset-cache",
    "reset": "rm -rf node_modules/ && npm cache clear && watchman watch-del-all && npm i",
    "debug-build": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ && cd android && ./gradlew assembleDebug && cd app/src/main/res/ && rmdir /S /Q raw && rmdir /S /Q drawable-mdpi",
    "release-build": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/build/intermediates/res/merged/release/ && cd android && ./gradlew assembleRelease && cd..",
    "install-apk": "cd android && ./gradlew installRelease",
    "release-aab": "cd android && ./gradlew bundleRelease"
4

For latest react native versions to bundle the project

Android

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

iOS

react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios

3

For me ./gradlew assembleRelease does not work anymore but the following does.

./gradlew :app:assembleRelease

Thanks to this comment.

3

Only this worked for me - yarn android --variant=release

2

Generate debug APK without dev-server

If you really want to generate a debug APK (for testing purpose) that can run without the development server, Here is the link to my answer to another similar post which may help you.
https://stackoverflow.com/a/65762142/7755579

All you've to do is:

  1. edit android/app/build.gradle
project.ext.react = [
    ...
    bundleInDebug: true, // add this line
]
  1. run this command at the root directory of your project
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
  1. Then, run ./gradlew assembleDebug (Mac or Linux) or gradlew assembleDebug (Windows) inside /android folder
    If the build failed, I suggest you to build the application from Android studio
0

Using android studio to generate signed apk for react's android project is also a good and easy option , open react's android project in android studio and generate keystroke and signed apk.

-3

Refer the react-native official documentation on Generating Signed APK

React-Native Generating Signed APK

1
  • This is not a good answer, even if it did address the user's question which it does not. They want to build a standalone debug APK, not a release one.
    – user37309
    Commented Apr 20, 2020 at 5:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.