How to Generate APK Using React Native Expo



This content originally appeared on DEV Community and was authored by Sh Raj

How to Generate APK Using React Native Expo

Creating an APK (Android Package) using React Native Expo involves using Expo’s tools to streamline the process. Here’s a comprehensive guide, including different options and configurations.

Prerequisites

  1. Node.js and npm: Ensure you have Node.js and npm installed. You can download them from nodejs.org.
  2. Expo CLI: Install Expo CLI:
   npm install -g expo-cli
  1. EAS CLI: Install EAS CLI (Expo Application Services):
   npm install -g eas-cli

Step-by-Step Guide

1. Setting Up Your Project

Create a new Expo project or navigate to your existing project directory:

expo init MyNewProject
cd MyNewProject

2. Configuring eas.json

Create an eas.json file in the root of your project to define build profiles. Here’s a detailed example:

{
  "build": {
    "preview": {
      "android": {
        "buildType": "apk"
      }
    },
    "preview2": {
      "android": {
        "gradleCommand": ":app:assembleRelease"
      }
    },
    "preview3": {
      "developmentClient": true
    },
    "production": {}
  }
}

3. Logging In to Expo

Ensure you are logged into your Expo account:

expo login

4. Building the APK

Run the following command to start the build process:

eas build -p android --profile preview

This command tells EAS to build an APK using the “preview” profile defined in your eas.json file.

5. Downloading the APK

Once the build is complete, you can download the APK from the Expo dashboard. The URL for your build will be displayed in the terminal or can be accessed from the Expo Builds page.

Additional Options

Building an AAB (Android App Bundle)

If you need an AAB instead of an APK (which is often required for Google Play Store submissions), you can modify the eas.json:

{
  "build": {
    "production": {
      "android": {
        "buildType": "app-bundle"
      }
    }
  }
}

And run the same build command:

eas build -p android --profile production

Custom Gradle Commands

You can also specify custom Gradle commands in your eas.json if you need more control:

{
  "build": {
    "preview2": {
      "android": {
        "gradleCommand": ":app:assembleRelease"
      }
    }
  }
}

Run the build with:

eas build -p android --profile preview2

Internal Distribution

For internal distribution builds, you can add:

{
  "build": {
    "preview3": {
      "android": {
        "distribution": "internal"
      }
    }
  }
}

And build using:

eas build -p android --profile preview3

Debugging and Troubleshooting

If you encounter issues during the build process, running the following commands can help clean your project and resolve common issues:

cd android
./gradlew clean
cd ..
eas build -p android --profile preview

You can also use adb (Android Debug Bridge) to get more detailed error logs and debug your application:

adb logcat

Publishing

To publish your APK or AAB to the Google Play Store, ensure you have set up the appropriate credentials and signing configurations in your Gradle files. Follow the instructions provided in the React Native documentation【10†source】【11†source】.

Important Notes

  • Keystore Security: Always keep your keystore files and passwords secure. If compromised, follow Google’s instructions to replace your upload key.
  • Expo Classic Builds: The expo build:android command is deprecated and replaced by EAS Build. Ensure you are using the latest SDK and tools【9†source】.

By following these steps and configurations, you can generate APKs and AABs using React Native Expo, customize your build process, and prepare your application for distribution.

For more detailed information, refer to the Expo Documentation and the React Native Documentation【8†source】【10†source】.


This content originally appeared on DEV Community and was authored by Sh Raj