android - 如何检测 Android 运行时(Dalvik 或 ART)?

标签 android dalvik android-4.4-kitkat art-runtime

Google 添加了一个新的 ART Android 4.4 运行时。如何确定当前运行时是 ART 还是 Dalvik?

最佳答案

更新

至少,早在 2014 年 6 月,Google 就在 how to correctly verify the current runtime in use 上发布了官方文档。 :

You can verify which runtime is in use by calling System.getProperty("java.vm.version"). If ART is in use, the property's value is "2.0.0" or higher.

这样,现在就不需要再经过反射,只需检查相应的系统属性:

private boolean getIsArtInUse() {
    final String vmVersion = System.getProperty("java.vm.version");
    return vmVersion != null && vmVersion.startsWith("2");
}

一种可能的方法是阅读相应的 SystemProperty通过反射(reflection)。

示例:

package com.example.getcurrentruntimevalue;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MainActivity extends Activity {
    private static final String SELECT_RUNTIME_PROPERTY = "persist.sys.dalvik.vm.lib";
    private static final String LIB_DALVIK = "libdvm.so";
    private static final String LIB_ART = "libart.so";
    private static final String LIB_ART_D = "libartd.so";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView)findViewById(R.id.current_runtime_value);
        tv.setText(getCurrentRuntimeValue());
    }

    private CharSequence getCurrentRuntimeValue() {
        try {
            Class<?> systemProperties = Class.forName("android.os.SystemProperties");
            try {
                Method get = systemProperties.getMethod("get",
                   String.class, String.class);
                if (get == null) {
                    return "WTF?!";
                }
                try {
                    final String value = (String)get.invoke(
                        systemProperties, SELECT_RUNTIME_PROPERTY,
                        /* Assuming default is */"Dalvik");
                    if (LIB_DALVIK.equals(value)) {
                        return "Dalvik";
                    } else if (LIB_ART.equals(value)) {
                        return "ART";
                    } else if (LIB_ART_D.equals(value)) {
                        return "ART debug build";
                    }

                    return value;
                } catch (IllegalAccessException e) {
                    return "IllegalAccessException";
                } catch (IllegalArgumentException e) {
                    return "IllegalArgumentException";
                } catch (InvocationTargetException e) {
                    return "InvocationTargetException";
                }
            } catch (NoSuchMethodException e) {
                return "SystemProperties.get(String key, String def) method is not found";
            }
        } catch (ClassNotFoundException e) {
            return "SystemProperties class is not found";
        }
    }
}

希望这会有所帮助。

关于android - 如何检测 Android 运行时(Dalvik 或 ART)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19830342/

相关文章:

java - AChartEngine:帮助需要轴标签

java - lint 检查未使用的方法(命令行)

java - 是否可以从 BroadcastReceiver 启动警报对话框?

android - ART 沙盒应用程序与 Dalvik 类似吗?

android - 在 kitkat 和 Lollipop 上时 event.getCreator() 的不同结果

Android Studio - 如何让 .addTextChangedListener 与两个 EditText 一起工作

android - 在 Android 中转换为 Dalvik 格式失败并出现错误 1

android - 带输入的 DalvikVM

android - 显示dialog fragment时被状态栏覆盖,仅限Android4.4

android - 带有 KitKat 的 Android 设备上的 java.lang.NoClassDefFoundError