建议封装至进程工具类(ProcessUtils)
private static int is64 = -1; // -1 -> 未知, 1 -> 64位进程, 0 -> 32位进程
/**
* 判断当前进程是否是64位的
* @return 若当前进程是64位,返回true
*/
public static boolean is64Bit() {
if(is64 >= 0) {
return is64 == 1;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
is64 = android.os.Process.is64Bit() ? 1 : 0;
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
is64 = Build.CPU_ABI.contains("64") ? 1 : 0;
} else {
is64 = containLib64() ? 1 : 0;
}
return is64 == 1;
}
private static boolean containLib64() {
BufferedReader reader = null;
try {
final File maps = new File("/proc/" + android.os.Process.myPid() + "/maps");
reader = new BufferedReader(new FileReader(maps));
String mapsLine = null;
while ((mapsLine = reader.readLine()) != null) {
if (mapsLine.contains("/system/lib64/")) {
return true;
} else if(mapsLine.contains("/system/lib/")) {
return false;
}
}
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
return false;
}