Android系统判断CPU是32位还是64位

一、方法一,读取"/proc/cpuinfo"文件的第一行。
1、32bit


2、64bit



二、方法二,读取Android 的system/build.prop文件("ro.product.cpu.abilist64")

1、32bit


2、64bit


三、方法三,通过读取libc.so文件的ELF头部e_indent[]数组,根据数组第e_indent[4]的取值来判断,参考代码如下,没有亲自测试。
    public static final String CPU_ARCHITECTURE_TYPE_32 = "32";public static final String CPU_ARCHITECTURE_TYPE_64 = "64";/** ELF文件头 e_indent[]数组文件类标识索引 */private static final int EI_CLASS = 4;/** ELF文件头 e_indent[EI_CLASS]的取值:ELFCLASS32表示32位目标 */private static final int ELFCLASS32 = 1;/** ELF文件头 e_indent[EI_CLASS]的取值:ELFCLASS64表示64位目标 */private static final int ELFCLASS64 = 2;/** The system property key of CPU arch type */private static final String CPU_ARCHITECTURE_KEY_64 = "ro.product.cpu.abilist64";/** The system libc.so file path */private static final String SYSTEM_LIB_C_PATH = "/system/lib/libc.so";private static final String SYSTEM_LIB_C_PATH_64 = "/system/lib64/libc.so";private static final String PROC_CPU_INFO_PATH = "/proc/cpuinfo";private static boolean LOGENABLE = false;/*** Check if the CPU architecture is x86*/public static boolean checkIfCPUx86() {//1. Check CPU architecture: arm or x86if (getSystemProperty("ro.product.cpu.abi", "arm").contains("x86")) {//The CPU is x86return true;} else {return false;}}/*** Get the CPU arch type: x32 or x64*/public static String getArchType(Context context) {if (getSystemProperty(CPU_ARCHITECTURE_KEY_64, "").length() > 0) {if (LOGENABLE) {Log.d("###############getSystemProperty","CPU arch is 64bit");}return CPU_ARCHITECTURE_TYPE_64;} else if (isCPUInfo64()) {return CPU_ARCHITECTURE_TYPE_64;} else if (isLibc64()) {return CPU_ARCHITECTURE_TYPE_64;} else {if (LOGENABLE) {Log.d("###############getArchType()","return cpu DEFAULT 32bit!");}return CPU_ARCHITECTURE_TYPE_32;}}private static String getSystemProperty(String key, String defaultValue) {String value = defaultValue;try {Class<?> clazz= Class.forName("android.os.SystemProperties");Method get = clazz.getMethod("get", String.class, String.class);value = (String)(get.invoke(clazz, key, ""));} catch (Exception e) {if (LOGENABLE) {Log.d("getSystemProperty", "key = " + key + ", error = " + e.getMessage());}}if (LOGENABLE) {Log.d("getSystemProperty",  key + " = " + value);}return value;}/*** Read the first line of "/proc/cpuinfo" file, and check if it is 64 bit.*/private static boolean isCPUInfo64() {File cpuInfo = new File(PROC_CPU_INFO_PATH);if (cpuInfo != null && cpuInfo.exists()) {InputStream inputStream = null;BufferedReader bufferedReader = null;try {inputStream = new FileInputStream(cpuInfo);bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 512);String line = bufferedReader.readLine();if (line != null && line.length() > 0 && line.toLowerCase(Locale.US).contains("arch64")) {if (LOGENABLE) {Log.d("###############isCPUInfo64()", PROC_CPU_INFO_PATH + " contains is arch64");}return true;} else {if (LOGENABLE) {Log.d("###############isCPUInfo64()", PROC_CPU_INFO_PATH + " is not arch64");}}} catch (Throwable t) {if (LOGENABLE) {Log.d("###############isCPUInfo64()","read " + PROC_CPU_INFO_PATH + " error = " + t.toString());}} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (Exception e) {e.printStackTrace();}try {if (inputStream != null) {inputStream.close();}} catch (Exception e) {e.printStackTrace();}}}return false;}/*** Check if system libc.so is 32 bit or 64 bit*/private static boolean isLibc64() {File libcFile = new File(SYSTEM_LIB_C_PATH);if (libcFile != null && libcFile.exists()) {byte[] header = readELFHeadrIndentArray(libcFile);if (header != null && header[EI_CLASS] == ELFCLASS64) {if (LOGENABLE) {Log.d("###############isLibc64()", SYSTEM_LIB_C_PATH + " is 64bit");}return true;}} File libcFile64 = new File(SYSTEM_LIB_C_PATH_64);if (libcFile64 != null && libcFile64.exists()) {byte[] header = readELFHeadrIndentArray(libcFile64);if (header != null && header[EI_CLASS] == ELFCLASS64) {if (LOGENABLE) {Log.d("###############isLibc64()", SYSTEM_LIB_C_PATH_64 + " is 64bit");}return true;}}return false;}/*** ELF文件头格式是固定的:文件开始是一个16字节的byte数组e_indent[16]* e_indent[4]的值可以判断ELF是32位还是64位*/private static byte[] readELFHeadrIndentArray(File libFile) {if (libFile != null && libFile.exists()) {FileInputStream inputStream = null;try {inputStream = new FileInputStream(libFile);if (inputStream != null) {byte[] tempBuffer = new byte[16];int count = inputStream.read(tempBuffer, 0, 16);if (count == 16) {return tempBuffer;} else {if (LOGENABLE) {Log.e("readELFHeadrIndentArray", "Error: e_indent lenght should be 16, but actual is " +  count);}}}} catch (Throwable t) {if (LOGENABLE) {Log.e("readELFHeadrIndentArray", "Error:" + t.toString());}} finally {if (inputStream != null) {try {inputStream.close();} catch (Exception e) {e.printStackTrace();}}}}return null;}

Android系统判断CPU是32位还是64位相关推荐

  1. Android 如何判断CPU是32位还是64位

    转自:http://blog.csdn.net/wangbaochu/article/details/47723265 1. 读取Android 的system property ("ro. ...

  2. Android判断CPU是32位还是64位

    Android系统判断CPU是32位还是64位 方法一: 命令行中输入以下命令 adb shell getprop ro.product.cpu.abi 这样可以直接获取cpu处理器位数: armea ...

  3. c语言程序判断32位还是64位,c++ 判断是64位还是32位系统的实例

    1.IsWow64Process 确定指定进程是否运行在64位操作系统的32环境(Wow64)下. 语法 BOOL WINAPI IsWow64Process( __in HANDLE hProces ...

  4. 如何判断Unix系统的一个库文件是32位还是64位的

    如何判断Unix系统的一个库文件是32位还是64位的 某些时候,我们需要知道操作系统的位数,或者配置插件的时候需要知道主程序的位数(例如配置apache插件的时候需要知道apache的位数以便配置相应 ...

  5. 判断当前Windows XP操作系统是32位还是64位的方法

    昨天在调查怎样区分Windows XP32位和64位的问题,在网上找了一下,大家常用的有两种方法. 方法一.通过指针来判断. 在32位操作系统上指针长是4个字节,而在64位系统上指针为8个字节. 方法 ...

  6. 台式计算机32位和64位的区别,电脑系统32位和64位有哪些区别 32位和64位是什么意思 【详解】...

    我们在安装系统的时候,需要了解的东西太多了,比如选择系统的话你首先要了解是选择32位还是64位系统呢?然后还需要判断电脑适合32位还是64位系统?这些问题要折腾清楚之后才能更好的进行安装,今天小编带大 ...

  7. 电脑系统32位和64位有哪些区别?32位和64位是什么意思 ?

    我们在安装系统的时候,需要了解的东西太多了,比如选择系统的话你首先要了解是选择32位还是64位系统呢?然后还需要判断电脑适合32位还是64位系统?这些问题要折腾清楚之后才能更好的进行安装,今天带大家了 ...

  8. 如何检测Android应用是32位还是64位

    目录 1.前言 2.检测App 3.应用是否包含 64 位库? 1.前言 从Android 4.4宣布支持64位系统以来,各终端方案厂商逐步推出了各自的64位soc解决方案.Google为了兼容之前3 ...

  9. 台式计算机怎么查是32位还是64位,如何判断电脑是32位还是64位

    生活中有很多的人不太清楚自己的电脑是多少位的,有时装软件就是装不上,自己也着急,后来才知道软件也是区分32跟64位的. 首先为大家讲解一下,32位与64位的不同,"32 位"和&q ...

最新文章

  1. 太TM难看了,我自己都看不下去了
  2. 如何在 Linux 上用 Markdown 编写电影剧本
  3. c# 监视目录下的文件变化
  4. 一夜间,中英同时发布新冠疫苗临床试验结果:均可引起免疫反应,同时登上《柳叶刀》...
  5. 2、ESXI安装出错
  6. android studio github 项目导入问题
  7. mysql 嵌入式_MySql移植到嵌入式Linux平台
  8. C++ —— C++程序编译的四个过程
  9. LeetCode 1696. 跳跃游戏 VI(优先队列 / 单调队列)
  10. 查询工资最低的3名员工的职工工号、姓名和收入_工资条6个常识必须掌握,事关你的权益!...
  11. (04)Verilog HDL模块仿真激励
  12. Android Studio(6)---编写APP
  13. 别人家只会编段子,谷歌带大家找乐子 | 愚人节の真 · 大型线下踏春游戏
  14. c语言二维数组每行最小值,编写一个函数,用于计算具有n行和m列的二维数组中指定列的平均值以及数组各行的和的最小值。...
  15. Mybatis中利用PageHelp分页功能的实现
  16. 计算机与网络安全系列书籍推荐
  17. ios-mfi_蓝牙部分翻译
  18. 解决html中表格线条粗细不一的问题
  19. 携创教育:2022学历改革解读系列|提升学历、迫在眉睫
  20. 关于chrome、360浏览器自动填充的黄色背景处理方案

热门文章

  1. HEIC文件怎么打开,如何将HEIC格式转换为JPG格式
  2. 常用电机驱动芯片的对比分析
  3. python3代码编程规范(命名、空格、注释、代码布局、编程建议等)
  4. 计算机中网络协议三要素,网络协议的三要素是什么?各有什么含义?
  5. 服务器配置jdk环境
  6. c#创建word表格 将表格所有内容居中
  7. 【windows版本】 db2数据库安装与使用
  8. Real-Time Loop Closure in 2D LIDAR SLAM 翻译和总结(一)
  9. 已解决FileNotFoundError: [WinError 2] 系统找不到指定的文件。
  10. Sql Server系统数据库的作用