Building React Native Expo APK Locally on Windows
Developing a React Native app is exciting, but constant testing can pose challenges.
Why am I Building an apk locally?
- I’m still in the development stage of my own app and lack a Google Play Store developer account for testing in a simulated production environment.
- While EAS offer convenience, it comes with costs, particularly with multiple builds.
- EAS doesn’t support local builds on Windows, requiring Linux or Mac. Since I lack access to these platforms, I’ve found a workaround
I’ll share the process I discovered for locally building my React Native APK on Windows using Expo and EAS. I hope it helps others in a similar situation.
Install wsl on windows
First, let’s install WSL on Windows by running the following command in PowerShell as an admin:
wsl --install
After that you will need to reboot your machine.
Install dependencies and configure eas
After rebooting your system, the Ubuntu terminal should appear on the screen. If not, search for the word terminal in Windows, and you should see the Ubuntu terminal in your options.
Then run:
sudo apt update
Installing node js and npm
To install the dependencies for your React Native Expo project, such as Node.js and npm run the following command:
sudo apt install nodejs npm
You can update Node.js to a specific version or the one you have in Windows using the following commands:
sudo npm cache clean --force
sudo npm install -g n
sudo n <node_version>
# Example: sudo n 18
Or, in case your project requires a specific Node.js version or you want to match the one you have in Windows, use these commands:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=18
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt install nodejs -y
Installing eas and expo cli
sudo npm install --global expo-cli eas-cli
Note: You must have an Expo Application Services account to build the APK. If you don’t have one, click here to create an account.
Once you have completed the signup or installed both CLIs, return to your Linux terminal and run these commands to log in to EAS CLI:
eas login
# enter your username/ email and passwornd
eas whoami
# to check if you are authenticated
Install Android studio
To build the APK, you need to install Android Studio and it has to be manually apparently. I followed this steps to install them, but I will provide the same commands below:
cd
# Install the default Java Runtime Environment (JRE) and Java Development Kit (JDK)
sudo apt install default-jre default-jdk
# Download Android Studio from URL
wget https://redirector.gvt1.com/edgedl/android/studio/ide-zips/2022.3.1.20/android-studio-2022.3.1.20-linux.tar.gz
tar -xvzf android-studio-2022.3.1.20-linux.tar.gz
cd android-studio
./bin/studio.sh
# Set the ANDROID_HOME environment variable to the path where the Android SDK is installed
export ANDROID_HOME=$HOME/Android/Sdk
# Add the tools directory of the Android SDK to the system PATH
export PATH=$PATH:$ANDROID_HOME/tools
Building Android Apk
Now finally we can start building the Android APK.
Your windows files are located in this path:
/mnt/c
#your project
/mnt/c/your_project
# I copied my project to my user home directory but I think you can do it in the windows path without problems
cp /mnt/c/my_project ~
Now let’s configure eas:
cd #your project path e.g /mnt/c/your_project
eas build:configure
#choose All
# > All
# iOS
# Android
This command will create a eas.json file.If not, you will need to create it and copy the code below:
{
"build": {
"preview": {
"android": {
"buildType": "apk"
}
},
"preview2": {
"android": {
"gradleCommand": ":app:assembleRelease"
}
},
"preview3": {
"developmentClient": true
},
"preview4": {
"distribution": "internal"
},
"production": {}
}
}
A brief explanation of the code above:You can set different build profiles (preview, preview2, preview3, preview4, and production), and each profile will have its different configuration depending on your requirements. In this case, we want to build an APK, so in our preview profile, we only need to set android buildType to apk, and that’s enough for us.
If you want to know more about building React Native Expo apps with EAS for production, APK, and so on, I recommend reading the documentation.
Now let’s build the apk
# Local flag to build the APK locally; if no local flag is set, it will use EAS cloud resources to build it
eas build -p android --profile preview --local
Your will need to wait for few minutes , and If everything is okay you should see at the end of the build process the path to your apk file which I think it is going to be
on your root project folder by default.
My file looked like this:
build-bunch_of_numbers.apk
Now you can try your apk on your windows android studio emulator or your android real deviceđź‘Ť.