DEV Community

loading...

Generating Android APK file with React Native

Alen
Developing software since 2017
・2 min read

Let's be honest, even if you really like React Native, you were frustrated with it at least once. Especially if you have just started working with it.

So I hope this post will help at least some of you :D.

This are 7 steps how to generate your Android APK file so you can test your app on a real phone and share it with your friends or testers.

Currently tested and working with latest version today 0.64!

  1. Generate private signing key using keytool. How to do that you can find it here

  2. Edit gradle.properties file in your android folder by adding following (make sure to replace ***** with a password that you want to use, my-upload-key and my-key-alias with the names you want):

     MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore<br>
     MYAPP_UPLOAD_KEY_ALIAS=my-key-alias<br>
     MYAPP_UPLOAD_STORE_PASSWORD=*****<br>
     MYAPP_UPLOAD_KEY_PASSWORD=*****<br>
    
  3. Add signing config in Gradle config file. Edit the build.gradle file in your android/app/ folder:

    ...
    android {
     ...
     defaultConfig { ... }
     signingConfigs {
         release {
             if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                 storeFile file(MYAPP_UPLOAD_STORE_FILE)
                 storePassword MYAPP_UPLOAD_STORE_PASSWORD
                 keyAlias MYAPP_UPLOAD_KEY_ALIAS
                 keyPassword MYAPP_UPLOAD_KEY_PASSWORD
             }
         }
     }
     buildTypes {
         release {
             ...
             signingConfig signingConfigs.release
         }
     }
    }
    ...
    
  4. Check if the file my-release-key.keystore ("my-release-key" is the key you entered earlier) is located in path /android/app/ (if it is not there, locate it in root or /android/ folder and place it in /android/app/ folder)

  5. Create assets folder in /android/app/src/main/

  6. Open your terminal and position yourself to root folder. Run command:

    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
    
  7. Open one more terminal and position yourself into android folder. Run command:
    gradlew clean
    or
    ./gradlew clean

  8. After previous command is finished run:
    gradlew assembleRelease -x bundleReleaseJsAndAssets
    or
    ./gradlew assembleRelease -x bundleReleaseJsAndAssets

  9. If everything goes well, you can find your signed apk file in android/app/build/outputs/apk/
    or
    android/app/build/outputs/apk/release/

Discussion (0)