Creating the Android .apk file

Creating the Android .apk file

Using Expo's Turtle CLI

ยท

4 min read

As a first 'proof of principle', I took a slightly modified version of the default Expo starter App and created an .apk from it to install it on my phone. As mentioned at the beginning of this series, the apps I'd like to build need to be able to play sound files, and it was a great relief to see that surprisingly, I can do that using Expo AV (Audio/Video player). This also means that there's currently no need to eject from Expo, it works perfectly even in managed workflow.

The downside is that the final app isn't particularly streamlined, and its file size is way too large for such a small demo app.


๐Ÿ“ฑ Building the .apk

You can always use Expo's service to create a stand-alone app. Depending on how busy their servers are, it'll take about 15-20 minutes for the build to complete, but you can also use their Turtle CLI to build the app on your computer instead.

I followed this excellent guide: Build Standalone Expo .apk and .ipa with Turtle CLI. Below are the steps summed up for Android builds.


1) Java Development Kit version 8

It has to be exactly this version (I first had to apt purge a previously installed newer version):

sudo apt install openjdk-8-jdk

Checking installation:

java -version
openjdk version "1.8.0_292"

2) Turtle CLI

npm install -g turtle-cli

Checking installation:

turtle --version
0.22.3

3) Configuring app.json

If you open app.json, it should already have an object for "android" (find a full list of what can be configured in Expo's docs). The only thing that's absolutely necessary though is the package name, in reverse DNS notation:

    "android": {
      ...
      "package": "com.example.application_name",
      ...
    }

4) Export the App

This will create a dist folder. To build the app later, you'll need to serve it from a local server (running at http://127.0.0.1:8000 in this case):

expo export --dev --public-url http://127.0.0.1:8000

5) Serve the App from localhost

npx http-server -p 8000 dist

6) Turtle Setup

For Android:

turtle setup:android

This will install everything that's required for creating the build, so it might take a while if you run it for the first time.


7) Create a Keystore to sign the app

From the root folder of your project, run:

keytool -genkeypair -v -keystore keystore.jks -alias <your-keystore-alias> -keyalg RSA -keysize 2048 -validity 10000

You'll be promped to enter a few details about your app (it doesn't really matter how you fill out the fields for this demonstration), a keystore password and a key password. This will then create a file keystore.jks that you can use later to sign your app.


8) Set environment variables for key and keystore passwords

export EXPO_ANDROID_KEYSTORE_PASSWORD=<your-keystore-password>
export EXPO_ANDROID_KEY_PASSWORD=<your-key-password>

9) Start the build ๐Ÿš€

turtle build:android \
  --type apk \
  --keystore-path ./keystore.jks \
  --keystore-alias <your-keystore-alias> \
  --allow-non-https-public-url \
  --public-url http://127.0.0.1:8000/android-index.json

If everything works (fingers crossed...), you'll find your .apk in a folder expo-apps in your Home directory.


10) Install it on your Phone

Upload the file to a service like Google Drive and download it with your phone. You'll get some security warnings when you try to install it, because it's not coming from the official App store and its author is unknown, but if you trust the author (... ๐Ÿ˜‡), go for "install anyway" - and enjoy playing with your first Android App ๐Ÿ“ฑ


๐Ÿ“ฑ Additional Notes

These steps describe how to create your own Android .apk in your local environment. However, when it comes to deploying an app to production, you might want to use Expo's services again. The greatest advantage is that they allow OTA (over-the-air) updates, simply by pushing your updated code to a repo.

Doing the build yourself though is great for learning purposes. Once you've created and built a number of apps, you'll have enough experience to tell that your code works and that the build will succeed.

Having this "proof of concept" out of the way, it's time to go back to learn more about

  • React Native

  • different kinds of navigation

  • the App signing process


๐Ÿ“ฑ Resources

Build Standalone Expo .apk and .ipa with Turtle CLI


๐Ÿ“ฑ Thanks for reading!

If you find any errors or have additions, questions or just want to say hi, please leave a comment below, or get in touch via my website jsdisco.dev or Twitter.
Keep calm & code ๐Ÿ‘‹