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

时间:2013-11-07 07:38:08

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

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

6 个答案:

答案 0 :(得分:31)

更新

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

  

您可以通过调用System.getProperty(“java.vm.version”)来验证正在使用的运行时。如果正在使用ART,则该属性的值为“2.0.0”或更高。

有了这个,现在没有必要经过反思,只需检查相应的系统属性:

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

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

样品:

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";
        }
    }
}

希望这有帮助。

答案 1 :(得分:4)

对于需要JNI版本的任何人:

#include <sys/system_properties.h>

static bool isArtEnabled() {
    char buf[PROP_VALUE_MAX] = {};
    __system_property_get("persist.sys.dalvik.vm.lib.2", buf);
    // This allows libartd.so to be detected as well.
    return strncmp("libart", buf, 6) == 0;
}

或者,如果您想要遵循更接近鞋鼠标的代码路径,

static bool isArtEnabled(JNIEnv *env)
{
    // Per https://developer.android.com/guide/practices/verifying-apps-art.html
    // if the result of System.getProperty("java.vm.version") starts with 2,
    // ART is enabled.

    jclass systemClass = env->FindClass("java/lang/System");

    if (systemClass == NULL) {
        LOGD("Could not find java.lang.System.");
        return false;
    }

    jmethodID getProperty = env->GetStaticMethodID(systemClass,
        "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");

    if (getProperty == NULL) {
        LOGD("Could not find java.lang.System.getProperty(String).");
        return false;
    }

    jstring propertyName = env->NewStringUTF("java.vm.version");

    jstring jversion = (jstring)env->CallStaticObjectMethod(
        systemClass, getProperty, propertyName);

    if (jversion == NULL) {
        LOGD("java.lang.System.getProperty('java.vm.version') did not return a value.");
        return false;
    }

    const char *version = env->GetStringUTFChars(jversion, JNI_FALSE);

    // Lets flip that check around to better bullet proof us.
    // Consider any version which starts with "1." to be Dalvik,
    // and all others to be ART.
    bool isArtEnabled = !(strlen(version) < 2 ||
        strncmp("1.", version, 2) == 0);

    LOGD("Is ART enabled? %d (%s)", isArtEnabled, version);

    env->ReleaseStringUTFChars(jversion, version);

    return isArtEnabled;
}

答案 2 :(得分:2)

Android docs实际上提出了以下建议:

  

您可以通过调用System.getProperty(&#34; java.vm.version&#34;)来验证正在使用的运行时。如果正在使用ART,则该属性的值为&#34; 2.0.0&#34;或更高。

这对我的Nexus 4 w / ART启用(运行Android 4.4.4)似乎很准确。 Dalvik的Nexus 5返回1.6.0

答案 3 :(得分:1)

我认为您应该能够System.getProperty使用java.vm.name作为密钥。 在JavaDoc中,它的值为Dalvik,在使用运行时时,希望它是ArtART。值得一试...

答案 4 :(得分:0)

final String vm = VMRuntime.getRuntime().vmLibrary();

然后将vm与“libdvm.so”或“libart.so”进行比较,以检查它是否为Dalvik或ART。

参考:https://gitorious.org/cyandreamproject/android_frameworks_base/commit/4c3f1e9e30948113b47068152027676172743eb1

答案 5 :(得分:0)

一个简单的解决方案:

String vm = System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version");

在我的Android 8.0(API 26)手机上,它返回Dalvik 2.1.0。