How can I generate an apk that can run without server with react-native?
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.
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
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
ENOENT: no such file or directory, open 'android/app/src/main/assets/index.android.bundle'
run mkdir android/app/src/main/assets
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.
Run this command:
react-native run-android --variant=release
Note that
--variant=release
is only available if you've set up signing withcd android && ./gradlew assembleRelease
.