1

我正在尝试在我的设备中获取运行时,我已经在 2 台设备(Xperia Z1,Note 4)中测试了以下方法。

我尝试过的以下代码:

private String getIsArtInUse() {
return System.getProperty("java.vm.version");
}

此代码返回 2.1,正如一些 stackoverflow 回答所说,版本大于 2.0 意味着它是 ART,但我的运行时是 Dalvik。

private CharSequence getCurrentRuntimeValue() {
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";
    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";
    }
}

此代码始终返回 Dalvik。

public String getRuntime()
{ 
return System.getProperty("java.vm.name");
}

以下也返回 Dalvik,但我认为它不会返回确切的应用运行时。

有什么建议么 ?

4

0 回答 0