我怎样才能检测到Android运行(Dalvik的或艺术)? [英] How can I detect the Android runtime (Dalvik or ART)?

查看:228
本文介绍了我怎样才能检测到Android运行(Dalvik的或艺术)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌增加了一个新的艺术运行了Android 4.4。如何确定技术或Dalvik的是否为当前运行?

Google added a new ART runtime with Android 4.4. How can I determine whether ART or Dalvik is the current runtime?

推荐答案

至少,早在2014年6月谷歌已经在如何正确地验证当前运​​行在使用:

Update

At least, as early as June 2014 Google has released an official documentation on how to correctly verify the current runtime in use:

您可以验证其运行时正在使用调用System.getProperty(java.vm.version)。如果艺术是在使用中,该属性的值是2.0.0或更高。

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.

使用的,现在也没有必要去通过反射和简单地检查相应的系统属性:

With that, now there is no need to go through reflection and simply check the corresponding system property:

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


一种可能的方式是读取各个<一href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/SystemProperties.java"><$c$c>SystemProperty通过反射。


One possible way is to read the respective SystemProperty through 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";
        }
    }
}

希望这有助于。

Hope this helps.

这篇关于我怎样才能检测到Android运行(Dalvik的或艺术)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆