一、判断CPU是否支持64位
Android系统从5.0开始支持64bit CPU,于是系统就有了zygote和zygote64两个进程来分别创建32和64位的应用进程。所以理论上Android系统5.0以下的系统是不支持64未架构的,只需要对Android系统5.0以上的系统进行检测。
public static boolean is64bit() {
boolean is64bit = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
is64bit = Build.SUPPORTED_64_BIT_ABIS.length > 0;
} else {
try {
BufferedReader localBufferedReader =
new BufferedReader(new FileReader("/proc/cpuinfo"));
if (localBufferedReader.readLine().contains("aarch64")) {
is64bit = true;
}
localBufferedReader.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
return is64bit;
}
来源:https://stackoverflow.com/questions/32452924/how-to-detect-android-device-is-64-bit-or-32-bit-processor
上面针对5.0以上和以下均进行了检测,主要是因为国内Android系统并不是完全标准的,可能会有特殊情况。
二、判断App进程是32位还是64位
- 对于Android 6.0及其以上的手机上,系统提供了检测方法
android.os.Process.is64Bit()
- 对于Android 5.0及其以上,6.0以下的手机,下面提供两种方法检测
(1)通过查看libart.so的路径来判断zygote的类型
private static boolean isART64() {
final String tag = "is64ART";
final String fileName = "art";
try {
ClassLoader classLoader = getContext().getClassLoader();
Class<?> cls = ClassLoader.class;
Method method = cls.getDeclaredMethod("findLibrary", String.class);
Object object = method.invoke(classLoader, fileName);
if (object != null) {
return ((String)object).contains("lib64");
}
} catch (Exception e) {
}
return false;
}
来源: https://www.jianshu.com/p/eb51edb1ab81
(2)通过System.getProperty("os.arch")检测
public static boolean is64() {
String arch = System.getProperty("os.arch");
if (arch != null && arch.contains("64")) {
return true;
} else {
return false;
}
}
来源:https://stackoverflow.com/questions/27121199/how-to-know-a-process-of-an-app-is-32-bit-or-64-bit-programmatically-in-android/33752302
- 对于Android5.0以下的手机,只支持32位
三、检测系统使用的是art还是dalvik
Google公司在Android 4.4中带来的ART模式仅仅是ART的一个预览版,系统默认仍然使用的是Dalvik虚拟机,ART虚拟机在Android 5.0及后续Android版本中作为正式的运行时库取代了以往的Dalvik虚拟机。
private boolean getIsArtInUse() {
final String vmVersion = System.getProperty("java.vm.version");
return vmVersion != null && vmVersion.startsWith("2");
}
来源:https://stackoverflow.com/questions/19830342/how-can-i-detect-the-android-runtime-dalvik-or-art