Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Obtaining Information from the Manifest File

Author: Colin Wilcox -- Published? true -- FormatLanguage: W

Problem:

You want to read project settings (e.b, app version) data from the AndroidManifest file during program execution.

Solution:

The key is to use the PackageManager. Rather than hard-coding values into the application which need to be changed each time the application is modified it is easier to read the version number from the manifest file as shown below.

Other settings can be read in a similar manner.

Discussion:

The two imports below need to be added to the Activity

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

The guts of the code is shown below:

public String readVersionNameFromManifest ()
{
       PackageInfo packageInfo = null;
		
       // Read package name and version number from manifest
       try {
           // load the package manager for the current context
  	  PackageManager packageManager = this.getPackageManager();

           // get the package info structure and pick out the fields you want
   	  packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
       } catch (Exception e) {
  	   Log.e(TAG, "Exception reading manifest version " + e);
       }
       return (packageInfo.versionName);
}
No records found.