DEV Community

Cover image for Generate apk for PURE React Native
SilvenLEAF
SilvenLEAF

Posted on • Edited on

Generate apk for PURE React Native

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

expo build:android -t apk
Enter fullscreen mode Exit fullscreen mode

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/


Step 1: generating .keystore file

Run this following command to create a ".keystore" file for your build

keytool -genkey -v -keystore your_key_name.keystore -alias your_key_alias -keyalg RSA -keysize 2048 -validity 10000
Enter fullscreen mode Exit fullscreen mode

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

mv my-release-key.keystore /android/app
Enter fullscreen mode Exit fullscreen mode

Now open your "android/app/build.gradle" file and add the following keystore configuration.


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
      }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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/
Enter fullscreen mode Exit fullscreen mode

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

cd android
Enter fullscreen mode Exit fullscreen mode

Then run the following command to generate the apk
(for Windows)

gradlew assembleRelease
Enter fullscreen mode Exit fullscreen mode

(for Linux or Mac)

./gradlew assembleRelease
Enter fullscreen mode Exit fullscreen mode

OR run this single command from root

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 ..
Enter fullscreen mode Exit fullscreen mode

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

Wanna know more about me? Come here!
SilvenLEAF.github.io

Top comments (1)

Collapse
 
abipravi profile image
Praveen Kumar

Image description

when i try to build the APK i am getting error like this

Some comments may only be visible to logged-in visitors. Sign in to view all comments.