Google added a new ART runtime with Android 4.4. How can I determine whether ART or Dalvik is the current runtime?
Android runtime (ART) is the managed runtime used by applications and some system services on Android. ART and its predecessor Dalvik were originally created specifically for the Android project. ART and Dalvik are compatible runtimes running Dex bytecode, so apps developed for Dalvik should work when running with ART.
Android Runtime, or ART, offers a faster alternative; Dalvik is optimized to run on older hardware with a limited processor and memory, something that isn't required of modern Android hardware. With Dalvik, apps are compiled using the Just-In-Time (JIT) compiler, making use of free system resources.
To give an example, it's the Java runtime that makes the java applications we write run on a Linux system as well as a windows system. ART was introduced as part of Android KitKat as an optional Run time. One can go to settings at Settings > Developer Options > Select Runtime and choose between Dalvik and ART.
Android Runtime (ART) is an application runtime environment used by the Android operating system. Replacing Dalvik, the process virtual machine originally used by Android, ART performs the translation of the application's bytecode into native instructions that are later executed by the device's runtime environment.
At least, as early as June 2014 Google has released an official documentation on how to correctly verify the current runtime in use:
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"); }
One possible way is to read the respective SystemProperty
through reflection.
Sample:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With