Google在Android 4.4中增加了一个新的ART运行时。如何确定当前运行时是ART还是Dalvik?
Google在Android 4.4中增加了一个新的ART运行时。如何确定当前运行时是ART还是Dalvik?
至少在2014年6月,谷歌发布了一份关于如何正确验证当前运行时的官方文档(点击此处查看):
您可以通过调用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";
}
}
}
希望这能帮到您。
如果需要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;
}
如果您想要更接近Shoe Rat发布的代码路径,可以选择以下方法:
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;
}
Android文档 实际上提供了以下建议:
您可以通过调用System.getProperty(“java.vm.version”)来验证正在使用哪个运行时。如果正在使用ART,则属性的值为“2.0.0”或更高。
在我的启用ART的Nexus 4上(运行Android 4.4.4),此方法似乎很准确。开启Dalvik的Nexus 5返回了1.6.0
。
最初的回答:
String vm = System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version");
java.vm.name
作为键。在 JavaDoc 中,它的值为 Dalvik
,希望在使用该运行时时,其值为 Art
或 ART
。 这值得一试...System.getProperty("java.vm.name")
返回 Dalvik
。
System.getProperty("java.specification.name")
返回 Dalvik Core Library
。 - Chris Lacyfinal String vm = VMRuntime.getRuntime().vmLibrary();
然后将vm与"libdvm.so"或"libart.so"进行比较,以检查它是Dalvik还是ART。