Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the Android runtime (Dalvik or ART)?

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

like image 831
Chris Lacy Avatar asked Nov 07 '13 07:11

Chris Lacy


People also ask

What is Dalvik and art 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.

Which runtime is better Dalvik or 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.

How do I switch from Dalvik to ART?

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.

What is the Android runtime explain Android Virtual Device?

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.


1 Answers

Update

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.

like image 74
ozbek Avatar answered Oct 03 '22 04:10

ozbek