techStackGuru

React Native - Generating Signed APK


Step 1: Running keytool

Windows

  • Open Command Prompt
  • gotto C:\Program Files\Java\jdkx.x.x_x\bin
  • and run as administrator.
  • keytool -genkeypair -v -storetype PKCS12 -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

    macOS

    To find your JDK bin folder on macOS, run the following command:

    /usr/libexec/java_home

    Upon completion, the directory of the JDK will look like this:

    /Library/Java/JavaVirtualMachines/jdkX.X.X_XXX.jdk/Contents/Home

    Use the cd command to navigate to that directory, and then run the keytool command with sudo permissions.

    sudo keytool -genkey -v -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

    Step 2: Setting up Gradle variables

  • 1. You will need to place the my-upload-key.keystore file under your project folder's Android/app directory.
  • 2. Edit the file ~/.gradle/gradle.properties or android/gradle.properties, and add the following (replace ***** with the correct keystore password, alias and key password),
  • MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
    MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
    MYAPP_UPLOAD_STORE_PASSWORD=*****
    MYAPP_UPLOAD_KEY_PASSWORD=*****

    Step 3: Adding signing configuration to your Gradle configuration

    ...
    android {
        ...
        defaultConfig { ... }
        signingConfigs {
            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
                }
            }
        }
        buildTypes {
            release {
                ...
                signingConfig signingConfigs.release
            }
        }
    }
    ...

    Step 4: Generating the release AAB

    npx react-native build-android --mode=release

    Step 5: Testing the release build

    npm run android -- --mode="release"

    ProGuard

  • Create proguard-rules.txt: Place this file in android/app/build/outputs/apk/release.
  • Add ProGuard rules: Adapt the rules to your specific app's needs by following the general guidance.
  • Configure Gradle: Update android/app/build.gradle with the appropriate ProGuard settings
  • buildTypes {
        release {
            minifyEnabled true
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
        }
    }

    Difference AAB vs apk

    There are a few key differences between AAB (Android App Bundle) and APK (Android application package) file formats for distributing Android apps:

    ABB (Android App Bundle)

  • The AAB is mainly used for publishing apps to app stores like Google Play. The app cannot be installed directly on a device.
  • The AAB consists of modular components that are separated according to the device configuration (code, resources, assets).
  • For each user, Google Play generates an optimized APK based on their device requirements from the AAB.
  • Reducing data usage and storage consumption by delivering only necessary components to users.
  • Enhances flexibility by allowing apps to add and remove features dynamically after installation.
  • Reduced update sizes: Only updated components are delivered, which reduces update sizes.
  • APK (Android application package)

  • An APK file allows apps to be installed directly onto devices or sideloaded.
  • The APK file includes all components of the app, regardless of the device. In contrast to an optimized AAB, it could be larger.
  • There is no need to process app store APKs or manage split APKs.
  • Compatibility: Will work on older devices or platforms that do not support AAB.