How to get the installed App Version in React Native?

By Prajwal Haniya

Techletter #40 | July 23, 2023

Why do you need an app version? It’s because, you can compare the user-installed version and the latest version, through this you can keep the data related to the user-installed versions, and change the UIs accordingly so that the user can proceed with app updates.

Of course, there is an in-app updates feature in Android. But, when the app is privately published you may not get the app version from the play store, because the user may have logged in using his/her personal email, instead of work/organization email. So, the solution would be to compare the app version to the dynamically fetched app version from the source. (Maybe from an organization web app where you keep track of the app versions).

So, here I will show how you can write a simple Java module that does the same. It includes comparing the Play Store version as well. (You can modify the code, only to return your app version).

android/app/src/main/java/com/your_package/AppVersionModule.java

package com.YOUR_PACKAGE_NAME;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;

import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.google.android.play.core.tasks.Task;

public class AppVersionModule extends ReactContextBaseJavaModule {
    private final ReactApplicationContext reactContext;
    private AppUpdateManager appUpdateManager;

    public AppVersionModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        this.appUpdateManager = AppUpdateManagerFactory.create(reactContext);
    }

    @Override
    public String getName() {
        return "AppVersionModule";
    }

    @ReactMethod
    public void getAppVersion(Promise promise) {
        try {
            PackageInfo packageInfo = reactContext.getPackageManager().getPackageInfo(reactContext.getPackageName(), 0);
            // You can get app version name as well
            String versionName = packageInfo.versionName;
            int versionCode = packageInfo.versionCode;
            String versionCodeString = String.valueOf(versionCode);
            // here I am using only the version code details
            promise.resolve(versionCodeString);
        } catch (PackageManager.NameNotFoundException e) {
            promise.reject("VERSION_NOT_AVAILABLE", "Unable to get the app version.");
        }
    }

    @ReactMethod
    public void getPlayStoreAppVersion(Promise promise) {
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                // Get the available Play Store version code
                int playStoreVersionCode = appUpdateInfo.availableVersionCode();
                promise.resolve(String.valueOf(playStoreVersionCode));
            } else {
                // you can reject, but it goes to the catch block in react native, so would prefer with resolve
                promise.resolve("false");
            }
        }).addOnFailureListener(e -> {
            promise.resolve("false");
        });
    }
}
package com.YOUR_PACKAGE_NAME;

import androidx.annotation.NonNull;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppVersionPackage implements ReactPackage {

    @NonNull
    @Override
    public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();

        modules.add(new AppVersionModule(reactContext));

        return modules;
    }

    @NonNull
    @Override
    public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}

Now, this can be imported into the react native code that you have written to get the app version/version code accordingly and perform the required operation.

If you are not familiar with how to connect the Java module with react native, you can check out the below article

How to set up communication between Java & JavaScript in react native?