How to get the version of the Linux kernel using Android?
Solution 1
Not 100% sure, but I think calling "uname -r" would require root access. There is anyways a less dirty way to do this, which is :
System.getProperty("os.version");
I found this information here : http://cb1991.blogspot.com/2011/03/android-code-to-get-kernel-version.html
Solution 2
If you want the full Kernel version has shown in Android about phone, this is the file to parse: /proc/version
Here is an extract of Android source code that retrieves the actual kernel version string:
private String getFormattedKernelVersion() {
String procVersionStr;
try {
procVersionStr = readLine(FILENAME_PROC_VERSION);
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(LOG_TAG, "Regex did not match on /proc/version: " + procVersionStr);
return "Unavailable";
} else if (m.groupCount() < 4) {
Log.e(LOG_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(LOG_TAG,
"IO Exception when getting kernel version for Device Info screen",
e);
return "Unavailable";
}
}
Solution 3
Invoke uname -r
and read its output from stdout
. It shouldn't be too complicated. The output is just the version number.
Runtime.getRuntime().exec("uname -r");
Assuming you write Java code (as far as I know Android Apps are written in Java), this might help you: http://www.java-tips.org/java-se-tips/java.lang/how-to-execute-a-command-from-code.html
chaitanya
Updated on June 06, 2022Comments
-
chaitanya almost 2 years
How can I get the version of the Linux kernel in an Android application?
-
3c71 about 11 yearsuname doesn't require root, just tested.
-
3c71 about 11 yearsNot sure why the down-vote as it works fine, even thought it's not the best option to use.
-
not2qubit over 7 yearsThat link is dead.
-
Ryan Bis over 6 yearsThis method doesn't work on all phones. I just tried it on a Sony Xperia Z Ultra, and it fails. havchr's post is the only one that seems to work across everything.