NewPlatano

Building an Android APK with React Native CLI

Beto, May 5, 2024 · 22,416 views

I walk you through building an Android APK from a React Native project using the React Native CLI. I cover the essential environment setup, how to run Gradle commands to generate the APK, and where to find the final APK file in your project folder.

If you want to share your app for testing or distribution outside the Play Store, this is a practical guide for you. It’s also a quick introduction to the React Native CLI build process for Android, showing how to create a standalone APK that can be installed on any compatible device.

What's inside

  • React Native CLI environment setup prerequisite
  • Running the app on Android to verify it works
  • Why building APKs is useful for testing and sharing
  • Using Gradle commands to clean and build the APK
  • Locating the generated APK file in the project folder
  • Installing and testing the APK on a device
  • Versioning considerations when publishing APKs
  • Links to environment setup and React Native courses

React Native CLI environment setup prerequisite

Before you can build an APK, you need to have your Android development environment properly set up for React Native CLI. This means installing Android Studio, the necessary SDKs, and configuring environment variables correctly.

If you haven’t done this yet, I recommend watching my previous video on React Native environment setup. I walk you through setting up Android and iOS environments on macOS step-by-step. Getting this right is crucial to avoid build errors and to ensure Gradle commands run smoothly when building your APK.

Running the app on Android to verify it works

I start by running the React Native app on an Android emulator or physical device to confirm everything works as expected before building the APK. This helps catch any runtime issues early on.

You can run the app using the React Native CLI command . Once the app runs fine on your device or emulator, you’re ready to proceed with building the APK.

Why building APKs is useful for testing and sharing

Building an APK produces a standalone Android application file that can be installed on any compatible device without needing development tools or a connection to a development server.

This is ideal for sharing your app with testers, quality engineers, or even family members. APKs load faster than debug builds because they are release builds optimized for distribution. Keep in mind, though, that APKs are mainly for testing and sharing purposes, not for Play Store publishing, which now prefers app bundles instead.

Using Gradle commands to clean and build the APK

Inside your React Native project’s folder, I run Gradle wrapper commands to build the APK:

  • clears previous build artifacts to avoid conflicts
  • generates the release APK

These commands invoke the native Android build system and produce a signed APK ready for installation on devices.

Locating the generated APK file in the project folder

After the build completes successfully, you can find the APK file at:

android/app/build/outputs/apk/release/app-release.apk

I demonstrate how to open this folder in Finder on macOS or File Explorer on Windows to access the APK easily. This is the file you can distribute or install on devices for testing or sharing.

Knowing exactly where the APK is located saves time and helps you avoid confusion, especially when you have multiple build variants or flavors in your project.

Installing and testing the APK on a device

To test the APK, I uninstall any existing version of the app from the device first. Then I install the new APK by dragging and dropping it onto the device or emulator.

The installed app runs as a standalone release build, which loads faster and does not rely on the development server. This confirms the APK works as expected and is ready for sharing or further testing.

Versioning considerations when publishing APKs

If you plan to publish your APK (though this is less common now), you must increment the version number and build number in your app’s configuration files.

Proper versioning ensures that updates install correctly and prevents conflicts on devices or app stores. Even if you’re only sharing APKs for testing, it’s good practice to update these numbers to keep track of different builds.

If you haven’t set up your environment yet, check out my React Native environment setup video for a detailed walkthrough.

I also recommend my React Native course on codewithbeto.dev, where you learn to build apps from zero to publishing on the App Store and Play Store. Plus, my React with TypeScript course is now fully completed, with early bird promotions available.

Resources

CourseReact Native course

CourseReact with TypeScript course

Let's connect!

Had a win? Get featured on Code with Beto.Share your story