How to get Linux kernel version using Android?

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

+3
source share
4 answers

Not 100% sure, but I think that calling “uname -r” will require root access. In any case, there is a less dirty way to do this, namely:

System.getProperty("os.version");

I found this information here: http://cb1991.blogspot.com/2011/03/android-code-to-get-kernel-version.html

+11
source

If you want the full version of the kernel to be displayed in Android about the phone, this is a file for parsing: / Proc / version

Here is an excerpt from the Android source code that extracts 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: (xxxxxx@xxxxx.constant) */
            "\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\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";
    }
}
+2
source

Call uname -rand read its output from stdout. It should not be too complicated. The conclusion is only the version number.


Runtime.getRuntime().exec("uname -r");

Assuming you are writing Java code (as far as I know, Android applications are written in Java), this may help you: http://www.java-tips.org/java-se-tips/java.lang/how- to-execute-a-command-from-code.html

+1
source

If you do this from an application, this will give you a 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();
    }
}
0
source

All Articles