How to get the version of the Linux kernel using Android?
Asked Answered
B

5

6

How can I get the version of the Linux kernel in an Android application?

Brough answered 8/3, 2011 at 15:32 Comment(0)
P
20

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

Peba answered 25/10, 2011 at 15:21 Comment(2)
uname doesn't require root, just tested.Furculum
That link is dead.Briggs
F
3

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";
    }
}
Furculum answered 15/3, 2013 at 11:9 Comment(0)
C
2

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

Call answered 8/3, 2011 at 15:37 Comment(2)
Not sure why the down-vote as it works fine, even thought it's not the best option to use.Furculum
This 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.Bilestone
J
1

If you are doing this from an app, this will give you the kernel version string:

public static String readKernelVersion() {
    try {
        Process p = Runtime.getRuntime().exec("uname -a");
        InputStream is = null;
        if (p.waitFor() == 0) {
            is = p.getInputStream();
        } else {
            is = p.getErrorStream();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is),
                BUFFER_SIZE);
        String line = br.readLine();
        br.close();
        return line;
    } catch (Exception ex) {
        return "ERROR: " + ex.getMessage();
    }
}
Jaramillo answered 3/3, 2014 at 4:1 Comment(0)
M
1

If you are targeting API 21+ (Android 5+), use android.system.Os.uname. For example: Os.uname().release

Maestoso answered 22/2 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.