38

Google は Android 4.4 で新しいARTランタイムを追加しました。ART または Dalvik が現在のランタイムであるかどうかを判断するにはどうすればよいですか?

4

6 に答える 6

33

アップデート

少なくとも 2014 年 6 月には、Google は現在使用中のランタイムを正しく検証する方法に関する公式ドキュメントをリリースしました。

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考えられる 1 つの方法は、リフレクションを通じてそれぞれを読み取ることです。

サンプル:

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

お役に立てれば。

于 2013-11-07T08:37:44.027 に答える
1

キーとしてSystem.getPropertyを使用できるはずだと思いますjava.vm.nameDalvikJavaDocでは、その値はArtです。それは試みに値します...ART

于 2013-11-07T08:29:18.960 に答える