Create APK for react‐native project in windows - arnabutilities/rbac-frontend GitHub Wiki

To create an APK from your React Native project on Windows, you need to follow these steps:

1. Prepare Your Project for Release

  1. Open your project in VSCode or any text editor.

  2. Edit android/app/build.gradle:

    • Set minifyEnabled and proguardFiles under the release block to optimize the APK size:
      android {
          ...
          buildTypes {
              release {
                  // Caution! Minifying and obfuscating your code can make it harder to debug.
                  minifyEnabled enableProguardInReleaseBuilds
                  proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
              }
          }
      }
      
  3. Create a key to sign the APK:

    • Open a Command Prompt or PowerShell and navigate to your project's android/app directory:
      cd android/app
      
    • Run the following command to generate a keystore:
      keytool -genkey -v -keystore my-release-key.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias my-key-alias
      
    • Follow the prompts to set up the keystore. Remember the passwords and alias as you will need them later.
  4. Configure the keystore in gradle:

    • Create a file named key.properties in the android directory and add the following lines, replacing the placeholders with your actual values:
      storePassword=<your-keystore-password>
      keyPassword=<your-key-password>
      keyAlias=my-key-alias
      storeFile=my-release-key.keystore
      
    • Edit android/app/build.gradle to load the keystore configuration:
      def keystorePropertiesFile = rootProject.file("key.properties")
      def keystoreProperties = new Properties()
      keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
      
      android {
          ...
          signingConfigs {
              release {
                  keyAlias keystoreProperties["keyAlias"]
                  keyPassword keystoreProperties["keyPassword"]
                  storeFile file(keystoreProperties["storeFile"])
                  storePassword keystoreProperties["storePassword"]
              }
          }
          buildTypes {
              release {
                  ...
                  signingConfig signingConfigs.release
              }
          }
      }
      

2. Build the Release APK

  1. Open a terminal and navigate to your React Native project directory:
    cd MyFirstProject
    
  2. Run the following command to assemble the release APK:
    npx react-native run-android --variant=release
    

3. Locate the APK

After the build is complete, you can find your release APK at:

MyFirstProject/android/app/build/outputs/apk/release/app-release.apk

4. Testing the APK

Transfer the app-release.apk file to your Android device and install it to test the application.

By following these steps, you should be able to generate a signed APK for your React Native project on Windows.