A Tale of Reducing Expo/React Native Android Application
Tags: reactnative, expo, android, tutorial
Published: 2021-04-13 02:55:05

I'm going to take you to my journey on how to smoothly reduce an android application made with React Native/Expo and hopefully without getting any error. Let's begin the journey!

First things first, let's talk about my development environment. I'm using `Lubuntu 20.04 focal`, with Desktop Environment `LxQt 0.14.1` and the details as shown in the picture below. I've successfully reduce my app for about 49% (the universal version).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2zjp8tecy5xo9mu8n0k.png)

## Requirements
There are several package you need to install in order to have smooth build.

### Install JRE, JDK and check the installation

```
sudo apt-get install default-jre
sudo apt-get install defalut-jdk

java -version
javac -version
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dc7bnqznk1rlbt0r9xyk.png)

### Install Expo Command Line

```
sudo npm install --global expo-cli
```

### Install Android Command Line Tools

```
sudo export ANDROID_HOME=/usr/lib/android-sdk
sudo wget https://dl.google.com/android/repository/commandlinetools-linux-6609375_latest.zip
sudo unzip commandlinetools-linux-6609375_latest.zip -d cmdline-tools
sudo mv cmdline-tools $ANDROID_HOME/
sudo export PATH=$ANDROID_HOME/cmdline-tools/tools/bin:$PATH
```

### Set up licenses
Go to /usr/lib/android-sdk/ directory

```
cd /usr/lib/android-sdk/
```

We need to accept the license before using the sdk

```
sudo yes | sdkmanager --licenses
```

And also we need to change the permission on `/tmp` folder

```
sudo chmod -R 0777 /tmp
```

## Action
So, the first action you need to take is open up your expo react native folder in a terminal. Make sure you back up your project files first, for safety.

Run the following command

```
expo eject
```

You will be asked with several question, and just answer it based on your project information. After you finish, there will be a new folder inside your project called android and IOs.

Go to `android` folder and make a new file called `local.properties`. This file will be used for pointing the sdk that gradle need in order to build your application.

```
cd android
touch local.properties
sudo nano local.properties
```

add the `sdk.dir` variable like this

```
sdk.dir = /usr/lib/android-sdk/
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d340knl5mh2t79dyxejq.png)

Ctrl + O, Enter, Ctrl + x to save it.

Next, go to the `android/app` folder on your terminal and generate a keystore for your application so that you can publish it in Google Play Store.

```
cd android/app
sudo keytool -genkey -v -keystore yourkeyname.keystore -alias yourkeyaliasname -keyalg RSA -keysize 2048 -validity 10000
```

Replace `yourkeyname` and `yourkeyaliasname`. And you will be asked for password. Make sure you remember the password.

After finish, a new file will appear with the extension (.keystore). That's your key. Now head back one directory and edit the `gradle.properties` file.

```
cd ..
sudo nano gradle.properties
```

add this line below

```
MYAPP_UPLOAD_STORE_FILE=yourkeyname.keystore
MYAPP_UPLOAD_KEY_ALIAS=yourkeyaliasname
MYAPP_UPLOAD_STORE_PASSWORD=yourkeypassword
MYAPP_UPLOAD_KEY_PASSWORD=yourkeypassword
org.gradle.jvmargs=-Xmx4096m
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06asyktnaaokemi2h3wp.png)

Replace `yourkeyname`, `yourkeyaliasname` and `yourkeypassword` then save it.

Now, go to `app` folder and edit the `build.gradle` file

```
cd app
sudo nano build.gradle
```

Change these line from false to true
 
```
def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false
```
	
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1z9gu2z4ch6mad2nghz2.png)

Scroll down into `signingConfigs` and add this configuration below the `debug`

```
	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
        }
``` 

Then, in the `buildTypes` look in curly brackets for `release` change this line

```
signingConfig signingConfigs.debug
```
to
```
signingConfig signingConfigs.release
```	

Also add this line below the `minifyEnabled`

```
shrinkResources true
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iebk8h8btxroxaequlvr.png)

Then scroll down to the last line, add a closing curly bracket (`}`). I don't know I'm getting an error without this strange closing curly bracket.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v973f93o8pl18tcavi.png)

The last thing, if you also want to build an universal apk file then scroll up and search for splits, inside the `abi` change `universalApk` from `false` to `true`. Save it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n46qhlnd8xg31wvf7xm9.png)

Note: If your app doesn't contain any permission, make sure you don't have any `use permission` code inside AndroidManifest.xml. 

Because by default, if you're not set the permission in the package.json, expo will automatically add every permission to your app and I'm having this issue when I was trying to publish to Google play store, so make sure you check that part.


Now everything is ready, time to build. Head back to `android` folder where the `gradlew` file exist. Log in terminal as super user, to avoid permission issues.

```
cd ..
sudo su
```

If you want to build the .apk file, run this command

```
./gradlew assembleRelease
```

Or if you want to build as a bundle (.aab) file run this command

```
./gradlew buildRelease
```

In my case, this take about 5-18 minutes depending on your machine and your project. The output will be in `app/build/outputs/apk/release` for `.apk` and `app/build/outputs/bundle/release` for `.aab`.

Well, that's all I guess. Hope it help, thank you for reading and sorry for a bad English. Cheers~