Android – Displaying Kernel Version information in an Application

A friend, Numus, suggested I display kernel version information in my application, FlashImageGUI, a while back. Today, I had time to research and implement this feature.
Turns out, AOSP includes a great method for doing this! In most android devices, under Settings and About Phone, there is a simple method which grabs this information.
The method is available on the AOSP kernel git site in the Settings app package under the DeviceInfoSettings file starting at line 169: AOSP Git Settings DeviceInfoSettings.java at Line 169

For all you wanting to add kernel version information to your android applications, here is the java code function copy/pasted from AOSP (Sorry, spacing is lost):

private String getFormattedKernelVersion() {
String procVersionStr;

try {
BufferedReader reader = new BufferedReader(new FileReader(“/proc/version”), 256);
try {
procVersionStr = reader.readLine();
} finally {
reader.close();
}

final String PROC_VERSION_REGEX =
“\\w+\\s+” + /* ignore: Linux */
“\\w+\\s+” + /* ignore: version */
“([^\\s]+)\\s+” + /* group 1: 2.6.22-omap1 */
“\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+” + /* group 2: ([email protected]) */
“\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+” + /* ignore: (gcc ..) */
“([^\\s]+)\\s+” + /* group 3: #26 */
“(?:PREEMPT\\s+)?” + /* ignore: PREEMPT (optional) */
“(.+)”; /* group 4: date */

Pattern p = Pattern.compile(PROC_VERSION_REGEX);
Matcher m = p.matcher(procVersionStr);

if (!m.matches()) {
Log.e(TAG, “Regex did not match on /proc/version: ” + procVersionStr);
return “Unavailable”;
} else if (m.groupCount() < 4) { Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount() + " groups"); return "Unavailable"; } else { return (new StringBuilder(m.group(1)).append("\n").append( m.group(2)).append(" ").append(m.group(3)).append("\n") .append(m.group(4))).toString(); } } catch (IOException e) { Log.e(TAG, "IO Exception when getting kernel version for Device Info screen", e); return "Unavailable"; } }

2 Comments on “Android – Displaying Kernel Version information in an Application

Leave a Reply

Your email address will not be published. Required fields are marked *

*