React-native: [Android] Building release APK with `react-native bundle`

Created on 16 Sep 2015  Â·  44Comments  Â·  Source: facebook/react-native

See #2703, looks like running react-native bundle and building with Gradle doesn't include the bundle in the APK.

Can you please document how to build release versions of Android apps? Should we release the Gradle plugin? Looks like react-native bundle is sufficient at this point. cc @kmagiera

Locked

Most helpful comment

Hi there @shawnXiao - I can run the following to compile the package now (from the project folder in terminal):

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/

All 44 comments

I think we need a separate guide on building release versions of apps. "Using offline bundle" is only documented inside "Running On Device (iOS)" right now.

Yes, that'd be great. I could only find a short block of information about this topic on the Debugging page in the documentation (disabling developer mode for the app. And it doesn't include any information about the bundle command)

+1 on the documentation. Also see https://github.com/facebook/react-native/issues/2712

This works for me:

  1. cd to the project directory
  2. Start the react-native packager if not started
  3. Download the bundle to the asset folder:
    curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"

Messed around with this for a while...seems like the only way (as of my writing this) to create a bundle for Android is to use the method that @krizpoon outlined above.

I tried:

react-native bundle --url src/index.android.js --out android/app/src/main/assets/index.android.bundle --root src

However it seems like the packager is excluding *.android.* files and throws the following error, despite /Users/hswolff/Sites/chartbeat/mobile_app/src/index.android.js existing:

➜  ~/Sites/chartbeat/mobile_app git:(phoenix) ✗ react-native bundle --url src/index.android.js --out android/app/src/main/assets/index.android.bundle --root src
Building package...

/Users/hswolff/Sites/chartbeat/mobile_app/node_modules/react-native/node_modules/promise/lib/done.js:10
      throw err;
      ^
NotFoundError: Cannot find entry file src/index.android.js in any of the roots: ["/Users/hswolff/Sites/chartbeat/mobile_app","/Users/hswolff/Sites/chartbeat/mobile_app/src"]
    at DependencyGraph._getAbsolutePath (/Users/hswolff/Sites/chartbeat/mobile_app/node_modules/react-native/packager/react-packager/src/DependencyResolver/DependencyGraph/index.js:186:11)
    at /Users/hswolff/Sites/chartbeat/mobile_app/node_modules/react-native/packager/react-packager/src/DependencyResolver/DependencyGraph/index.js:141:28
    at tryCallOne (/Users/hswolff/Sites/chartbeat/mobile_app/node_modules/react-native/node_modules/promise/lib/core.js:37:12)
    at /Users/hswolff/Sites/chartbeat/mobile_app/node_modules/react-native/node_modules/promise/lib/core.js:103:15
    at flush (/Users/hswolff/Sites/chartbeat/mobile_app/node_modules/react-native/node_modules/promise/node_modules/asap/raw.js:50:29)
    at doNTCallback0 (node.js:407:9)
    at process._tickCallback (node.js:336:13)

Even trying to do something clever like this:

react-native bundle --root src --url index.android.bundle?platform\=android\&dev=

Also throws the same error.

I imagine this will be looked at once the packager is in its own repo. For now I'm using the above outlined method.

@hswolff This is due to the hardcoded blacklist param that results in the index.android.js getting ignored. Looks like this has been fixed on 0.12-stable.

can someone document the complete steps, from bundling all the way to installing the release version apk to device?

I put together my steps here. Hopefully it will save people some time.

https://gist.github.com/marty-wang/5a71e9d0a6a2c6d6263c

+1

+1

I have a pull request open here - https://github.com/facebook/react-native/pull/3061

@marty-wang +1 for gist

@kkganesan It's already on the documentation page - http://facebook.github.io/react-native/docs/signed-apk-android.html#content

Closing this issue, looks like it's been solved by @satya164's PR

Hi all, whenever I run the $ cd android && ./gradlew assembleRelease step in the page linked by @satya164 I get the error: org.gradle.api.GradleException: Could not list contents of '/Users/me/React/myapp/node_modules/.bin/defs'.

Has anyone else experienced this? I can't seem to get around it. I've posted on SO with no luck so far: http://stackoverflow.com/questions/33785803/error-generating-signed-apk

cc @mikearmstrong001 ^

Just as an update, the only way so far that I can create a signed APK is by running the curl "http://localhost:8081/index.android.bundle?platform=android" -dev=false -o "android/app/src/main/assets/index.android.bundle" command and then using Android Studio to create the actual APK. Whenever I try to create the APK fully via Terminal I get the error outlined here: http://stackoverflow.com/questions/33785803/error-generating-signed-apk

@scgough I have same question. Have you solved it?

Hi there @shawnXiao - I can run the following to compile the package now (from the project folder in terminal):

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/

You don't need to run any commands. The gradle script automatically bundles the script for you.

I have tried all the guides and still can't generate a working apk, it always keeps crushing, seems like the bundle not deliveres to the apk

Same here ?
cd android && ./gradlew assembleRelease
cd android && ./gradlew installRelease
I can run apk-appdebug on my device but not the app-release. And I dont know what is happening since I cant get to the first screen of the app.

Same here, It works for the app debug on my device, but when I build a release, install on device and open it throws a "App stopped working".

This started to happen from react-native 0.24 (It was working with 0.22)

Should I create a new issue?, because this one is closed

Same here, Gradle is not outputting the JS bundle file

@philohelp There's no "./gradlew installRelease" task... (RN 0.26)

@pjnovas same issue here, after upgrading to 0.22.2 from 18 - not sure if that's the same as your 0.22. Thought I'd add it here in case you did open another issue, as I'm at a loss of what to do.

I'm using version 0.28.0-rc.0 and this is my working flow of releasing signed Android APK:

  1. Bump version in android/app/build.gradle.
  2. As @scgough said: 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/
  3. In Android Studio: _Build_ -> _Generate Signed APK_

@neciu do you use the build.gradle supplied by react-native or your own?

@fab1an the one given to me by react-native init.

but shouldn't that one build the bundle by itself without you manually having to do it?

I have encountered a similar problem as @scgough described. In my case, it is Could not list contents of '/Users/me/React/myapp/node_modules/.bin/window-size'. The windows-size is a symbolic link point to '../window-size/cli.js', and this file disappeared, so I reinstalled the window-size package and the problem solved.

@levinqdl o wow! I gonna test that this evening!

Hi all - sorry I've not been active on this. Been on other projects. To summarise though, I can only create an Android APK using the method @neciu described. Not the end of the world but slightly more painful than iOS. It just feels a bit of a 'hacky' process.

In my case

./gradlew assembleRelease

...

FAILURE: Build failed with an exception.

* What went wrong:
Could not list contents of '/Users/devpc/myproject/node_modules/.bin/window-size'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

I simply had to remove that empty file rm ~/myproject/node_modules/.bin/window-sizeand now it works!

@marty-wang : Your guide was awesome. Really helped a lot.
I wish there was a step by step like that for apple from unassigned app to deploying online.

A solution which worked for me was deleting node_modules, run 'npm install' on the project folder
than cd android
en run ./gradlew assembleRelease

I tried the following:

Generate APK (UNSIGNED) (Android)
  1. Create the folder assets in android/app/src/main folder.
  2. Run curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
  3. Run 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/
  4. Run cd android && ./gradlew assembleRelease

BUILD SUCCESSFUL

But no .apk file created. I'm trying to generate an unsigned app, to install it on my own mobile. What did I miss?

@Vadorequest the APK you seek should reside in app/build/outputs/apk

@fab1an Thanks, I must have missed that in the doc. I tried to adb install the app-release-unsigned.apk but I couldn't (permission denied). So I tried the debug.apk, but I got a bundle error at startup, which I fixed by running the react-native start and react-native run-android commands, but it requires a wired connection between the phone and the computer. I wanted to have a standalone apk on the phone.

Finally, I just tried with the app-debug-unaligned.apk and it works on the phone without wired connection.
Thanks for the help!

@Vadorequest You did not miss it, it's simply not in the docs.

You can install the signed apk using adb pm install myapk.apk

@neciu do we have to run 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 everytime we wish to build apk?

(in project directory run following commands)

  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
  3. react-native run-android

I am creating release apk in react native using below commands. It works for me.

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/

Mac
watchman watch-del-all && rm -rf node_modules/ && npm cache clean && npm install && npm start -- --reset-cache

Windows
Permanent delete node_modules
npm cache clean –force
npm install
npm start -- --reset-cache

Open SDK -> Click on rebuild -> Fixed gradle build issue -> click on rebuild again -> generate signed apk.

I was able to build for offline use without having to sign using @krishan-kumar-mourya instructions! The only problem is now I don't have my assets that were loaded as so require('./assets/portal_ship/portal_ship_specular.png') from one of my js files.

Update
Found the Solution to ^ following step 5 of the answer here https://stackoverflow.com/questions/34175416/how-to-use-offline-bundle-on-android-for-react-native-project/

Was this page helpful?
0 / 5 - 0 ratings