Generate apk for PURE React Native
Tags: typescript, javascript, reactnative, silvenleaf
Published: 2022-04-23 14:22:28

When building apps with Expo, it takes most of the hardwork out for you. So to create an apk for your app it is just a one line command
```bash
expo build:android -t apk
```
But for PURE React Native App it's a little bit more fun than just that. How? Let's find it out together!!

(Refer to this link it is the BEST)
[https://instamobile.io/android-development/generate-react-native-release-build-android/](https://instamobile.io/android-development/generate-react-native-release-build-android/)

***
### Step 1: generating .keystore file
Run this following command to create a **".keystore"** file for your build
```bash
keytool -genkey -v -keystore your_key_name.keystore -alias your_key_alias -keyalg RSA -keysize 2048 -validity 10000
```
I normally prefer to have my key name same as my app name but you can name it anything whatever you prefer :)

After running the above command you will be prompted with lots of questions from your beloved terminal. Answer each of the carefully and REMEMBER the password, you'll need it later (esp the last one but we'll keep them all same so we'll consider it all one password)

As a result of the previous command, it generates a key-store file on your project directory named **"youre_key_name.keystore"** valid for 10000 days. Most importantly, **back up this keystore file and its credentials** (store password, alias, and alias password) which will be required later.

***
### Step 2: adding the .keystore file to your project
Firstly, you need to copy the file _your_key_name.keystore_ and paste it under the  **"android/app"** directory in your React Native project folder.

You can use the following command from your terminal
```bash
mv my-release-key.keystore /android/app
 ```
Now open your **"android/app/build.gradle"** file and add the following keystore configuration.

***
```gradle
android {
  ....
    signingConfigs {
      release {
        storeFile file('your_key_name.keystore')
        storePassword System.console().readLine("\nKeystore password:")
        keyAlias System.console().readLine("\nAlias: ")
        keyPassword System.console().readLine("\Alias password: ")
      }
    }
    buildTypes {
      release {
        ....
        signingConfig signingConfigs.release
      }
    }
}
```
*** 

With this you'll get prompted  for passwords when you run the apk build command. Make sure the **"signingConfigs"** block appears before **"buildTypes"** block to avoid unnecessary errors. Moreover, before going any further, make sure you have an assets folder under **"android/app/src/main/assets"**. If it’s not there, create one.  Now we are ready to generate the apk

***
### Step 3: generate apk
First run this following command to build the bundle
```bash
react-native  bundle  --platform  android  --dev  false  --entry-file  index.js  --bundle-output  android/app/src/main/assets/index.android.bundle  --assets-dest  android/app/src/main/res/
```

**Note**: If you have a different entry file name, like **"index.android.js"**, change it within the command.

Now go inside **"android"** folder with this command
```bash
cd android
```
Then run the following command to generate the apk
(for Windows)
```bash
gradlew assembleRelease
```
(for Linux or Mac)
```bash
./gradlew assembleRelease
```
### OR run this single command from root
```bash
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/build/intermediates/res/merged/release/ && rm -rf android/app/src/main/res/drawable-* && rm -rf android/app/src/main/res/raw/* && cd android && ./gradlew assembleRelease && cd ..
```
As a result, **the APK creation process is done**. You can find the generated APK at **"android/app/build/outputs/apk/app-release.apk"**. This is the actual app, which you can send to your phone or upload to the Google Play Store. Congratulations, you’ve just generated a React Native Release Build APK for Android.

# What's NEXT?
#### 1. Project with Pure React Native
#### 2. More on App Development
#### 3. How to generate apk with pure React Native
#### 4. How to deploy to playstore
#### 5. Insane stuff with JavaScript/TypeScript
#### 6. Writing Automated Tests for any Server
#### 7. How to create an Android APP with NO XP with Expo
(including apk generating)


# Got any doubt? 
Drop a comment or Feel free to reach out to me @SilveLEAF on Twitter or [Linkedin](https://www.linkedin.com/in/silvenleaf/)

Wanna know more about me? Come here! 
[SilvenLEAF.github.io](https://SilvenLEAF.github.io)