Android NDK动态获取系统版本

让电脑连上安卓手机上,执行

1
2
adb shell
cat /system/build.prop

可看到

1
2
3
4
5
...
ro.build.version.sdk=18
ro.build.version.codename=REL
ro.build.version.release=4.3
...

于是jni中可写如下代码获取系统版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sys/system_properties.h>
 
static int test(){
    char *key = (char *)"ro.build.version.sdk";
    //char *key = (char *)"ro.build.version.release";
    char value[1024] = {0};
    int ret = __system_property_get(key, value);
    if (ret <= 0 ) {
        DBG(("get prop value failed.\n"));
        return 0;
    }
    DBG(("ro.build.id is [%s]\n", value));
 
    return 0;
}