How to get the version of the Linux kernel using Android?

谁说我不能喝 提交于 2019-12-07 06:04:10

问题


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


回答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




回答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: (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";
    }
}



回答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




回答4:


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();
    }
}


来源:https://stackoverflow.com/questions/5234574/how-to-get-the-version-of-the-linux-kernel-using-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!