code: public static int getAppVersion(@NonNull Context context) {
          try {
              return context.getPackageManager()
                      .getPackageInfo(context.getPackageName(), 0).versionCode;
          } catch (PackageManager.NameNotFoundException e) {
              return -1;
          }
      }
docs: Get Application version registered by the OS.
  
  @return Application version registered by the OS.