Skip to content Skip to sidebar Skip to footer

How Can I Detect The Android Runtime (dalvik Or Art) In Xamarin

Similar question to this one : How can I detect the Android runtime (Dalvik or ART)? How to do it in Xamarin.Android (MonoDroid), since I dont have java reflection here? Since ART

Solution 1:

Seems to work on the emulator, don't know what happens with actual device until it gets some charge:

[Activity (Label = "RuntimeActivity", MainLauncher = true)]
publicclassRuntimeActivity : Activity 
{
    privatestaticreadonlystringSELECT_RUNTIME_PROPERTY = "persist.sys.dalvik.vm.lib";
    privatestaticreadonlystringLIB_DALVIK = "libdvm.so";
    privatestaticreadonlystringLIB_ART = "libart.so";
    privatestaticreadonlystringLIB_ART_D = "libartd.so";

    override protectedvoidOnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.runtime);

        var tv = FindViewById<TextView>(Resource.Id.textView1);
        tv.Text = getCurrentRuntimeValue ();
    }

    privatestringgetCurrentRuntimeValue() 
    {
        try 
        {
            var systemProperties = Java.Lang.Class.ForName("android.os.SystemProperties");

            try 
            {
                var str = newJava.Lang.String();
                var getMethod = systemProperties.GetMethod("get", str.Class, str.Class);

                if (getMethod == null) 
                {
                    return"WTF?!";
                }

                try 
                {
                    var value = getMethod.Invoke(systemProperties, SELECT_RUNTIME_PROPERTY,
                        /* Assuming default is */"Dalvik").ToString();
                    if (LIB_DALVIK.Equals(value)) {
                        return"Dalvik";
                    } elseif (LIB_ART.Equals(value)) {
                        return"ART";
                    } elseif (LIB_ART_D.Equals(value)) {
                        return"ART debug build";
                    }

                    return value;
                } 
                catch (IllegalAccessException e) 
                {
                    Log.Error(this.ToString(), e.Message);
                    return"IllegalAccessException";
                } 
                catch (IllegalArgumentException e) 
                {
                    Log.Error(this.ToString(), e.Message);
                    return"IllegalArgumentException";
                } 
                catch (InvocationTargetException e) 
                {
                    Log.Error(this.ToString(), e.Message);
                    return"InvocationTargetException";
                }
            } 
            catch (NoSuchMethodException e) 
            {
                Log.Error(this.ToString(), e.Message);
                return"SystemProperties.get(String key, String def) method is not found";
            }
        } 
        catch (ClassNotFoundException e) 
        {
            Log.Error(this.ToString(), e.Message);
            return"SystemProperties class is not found";
        }
    }
}

Solution 2:

Xamarin.Android itself uses __system_property_get, which is a stable, documented, API, so you can use P/Invoke to do the same:

// <sys/system_properties.h>
[DllImport ("/system/lib/libc.so")]
staticexternint __system_property_get (string name, StringBuilder value);

constint MaxPropertyNameLength   = 32; // <sys/system_properties.h>constint MaxPropertyValueLength  = 92; // <sys/system_properties.h>staticbool OnArt {
    get {
        var buf = new StringBuilder (MaxPropertyValueLength + 1);
        int n   = __system_property_get ("persist.sys.dalvik.vm.lib", buf);
        if (n > 0)
            return buf.ToString () == "libart.so";
        returnfalse;
    }
}

Post a Comment for "How Can I Detect The Android Runtime (dalvik Or Art) In Xamarin"