Get Android Version Information

Viewed 4222

I want to get Android version information from Unity. I know that SystemInfo.operatingSystem can be used to do this but it does not have all the information such as release number, code-name and other info.

I decided to make a tiny plugin with Unity's AndroidJavaClass class using Android's Build.VERSION class but ran into a problem I can't explain.

When I do:

AndroidJavaClass("android.os.Build.VERSION");

I get class not found exception.

It works when use:

AndroidJavaClass("android.os.Build$VERSION");

Notice that I replaced "." with "$" and the class can now be found.

I have written many plugins in the past and have never ran into this problem before. For example, when I accessed the Android's Uri class, I used AndroidJavaClass("android.net.Uri"); and it worked. I didn't have to put "$" before "Uri".

What makes accessing android.net.Uri different from accessing android.os.Build.VERSION?

Why do you have to put "$" between Build and VERSION in order for AndroidJavaClass to find this class?

By the way, here is the working plugin of Build.VERSION in Unity:

public class AndroidVersion
{
    static AndroidJavaClass versionInfo;

    static AndroidVersion()
    {
        versionInfo = new AndroidJavaClass("android.os.Build$VERSION");
    }

    public static string BASE_OS
    {
        get
        {
            return versionInfo.GetStatic<string>("BASE_OS");
        }
    }

    public static string CODENAME
    {
        get
        {
            return versionInfo.GetStatic<string>("CODENAME");
        }
    }

    public static string INCREMENTAL
    {
        get
        {
            return versionInfo.GetStatic<string>("INCREMENTAL");
        }
    }

    public static int PREVIEW_SDK_INT
    {
        get
        {
            return versionInfo.GetStatic<int>("PREVIEW_SDK_INT");
        }
    }

    public static string RELEASE
    {
        get
        {
            return versionInfo.GetStatic<string>("RELEASE");
        }
    }

    public static string SDK
    {
        get
        {
            return versionInfo.GetStatic<string>("SDK");
        }
    }

    public static int SDK_INT
    {
        get
        {
            return versionInfo.GetStatic<int>("SDK_INT");
        }
    }

    public static string SECURITY_PATCH
    {
        get
        {
            return versionInfo.GetStatic<string>("SECURITY_PATCH");
        }
    }

    public static string ALL_VERSION
    {
        get
        {
            string version = "BASE_OS: " + BASE_OS + "\n";
            version += "CODENAME: " + CODENAME + "\n";
            version += "INCREMENTAL: " + INCREMENTAL + "\n";
            version += "PREVIEW_SDK_INT: " + PREVIEW_SDK_INT + "\n";
            version += "RELEASE: " + RELEASE + "\n";
            version += "SDK: " + SDK + "\n";
            version += "SDK_INT: " + SDK_INT + "\n";
            version += "SECURITY_PATCH: " + SECURITY_PATCH;

            return version;
        }
    }
}
1 Answers
Related