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

时间:2024-10-18 07:03:30

可以利用三种方式来判断CPU是32位还是64位:

1. 读取Android 的system property (".abilist64")来判断

2. 读取"/proc/cpuinfo"文件的第一行来判断

3. 读取文件的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 = ".abilist64";
    
    /** The system  file path */
    private static final String SYSTEM_LIB_C_PATH = "/system/lib/";
    private static final String SYSTEM_LIB_C_PATH_64 = "/system/lib64/";
    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 x86
        if (getSystemProperty("", "arm").contains("x86")) {
            //The CPU is x86
            return 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) {
                ("###############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) {
                ("###############getArchType()","return cpu DEFAULT 32bit!");
            }
            return CPU_ARCHITECTURE_TYPE_32;
        }
    }
    
    private static String getSystemProperty(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> clazz= ("");
            Method get = ("get", , );
            value = (String)((clazz, key, ""));
        } catch (Exception e) {
            if (LOGENABLE) {
                ("getSystemProperty", "key = " + key + ", error = " + ());
            }
        }

        if (LOGENABLE) {
            ("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 && ()) {
            InputStream inputStream = null;
            BufferedReader bufferedReader = null;
            try {
                inputStream = new FileInputStream(cpuInfo);
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 512);
                String line = ();
                if (line != null && () > 0 && ().contains("arch64")) {
                    if (LOGENABLE) {
                        ("###############isCPUInfo64()", PROC_CPU_INFO_PATH + " contains is arch64");
                    }
                    return true;
                } else {
                    if (LOGENABLE) {
                        ("###############isCPUInfo64()", PROC_CPU_INFO_PATH + " is not arch64");
                    }
                }
            } catch (Throwable t) {
                if (LOGENABLE) {
                    ("###############isCPUInfo64()","read " + PROC_CPU_INFO_PATH + " error = " + ());
                }
            } finally {
                try {
                    if (bufferedReader != null) {
                        ();
                    }
                } catch (Exception e) {
                    ();
                }
                
                try {
                    if (inputStream != null) {
                        ();
                    }
                } catch (Exception e) {
                    ();
                }
            }
        }
        return false;
    }
    
    /**
     * Check if system  is 32 bit or 64 bit
     */
    private static boolean isLibc64() {
        File libcFile = new File(SYSTEM_LIB_C_PATH);
        if (libcFile != null && ()) {
            byte[] header = readELFHeadrIndentArray(libcFile);
            if (header != null && header[EI_CLASS] == ELFCLASS64) {
                if (LOGENABLE) {
                    ("###############isLibc64()", SYSTEM_LIB_C_PATH + " is 64bit");
                }
                return true;
            }
        } 
        
        File libcFile64 = new File(SYSTEM_LIB_C_PATH_64);
        if (libcFile64 != null && ()) {
            byte[] header = readELFHeadrIndentArray(libcFile64);
            if (header != null && header[EI_CLASS] == ELFCLASS64) {
                if (LOGENABLE) {
                    ("###############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 && ()) {
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(libFile);
                if (inputStream != null) {
                    byte[] tempBuffer = new byte[16];
                    int count = (tempBuffer, 0, 16);
                    if (count == 16) {
                        return tempBuffer;
                    } else {
                        if (LOGENABLE) {
                            ("readELFHeadrIndentArray", "Error: e_indent lenght should be 16, but actual is " +  count);
                        }
                    }
                }
            } catch (Throwable t) {
                if (LOGENABLE) {
                    ("readELFHeadrIndentArray", "Error:" + ());
                }
            } finally {
                if (inputStream != null) {
                    try {
                        ();
                    } catch (Exception e) {
                        ();
                    }
                }
            }
        }
        
        return null;
    }