cesiztel
5/15/2017 - 9:37 AM

Get version information name + code Android app

Get version information name + code Android app

// Include in custom Application file. If
// wants to add to helper function, the method
// will need the context.

/**
 * Get version information of your app to display in
 * view.
 *
 * @return versionInformation String that represent the version
 *                            information composed by name and version code.
 *                            If the method fails in retrieve the info, an
 *                            empty string is returned
 */
public String getVersionInformation() {
  String versionInformation = "";

  try {
    PackageManager packageManager = this.getPackageManager();
    PackageInfo packageInformation = packageManager.getPackageInfo(getPackageName(), 0);

    versionInformation = packageInformation.versionName + "." + packageInformation.versionCode;
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }

  return versionInformation;
}