Class AndroidVersion

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<AndroidVersion>

    public final class AndroidVersion
    extends java.lang.Object
    implements java.lang.Comparable<AndroidVersion>, java.io.Serializable

    Represents the version of a target or device.

    A version is defined by an API level, an optional code name, and an optional extension level.
    • Release versions of the Android platform are identified by their API level (integer), and extension level if present. (technically the code name for release version is "REL" but this class will return null instead.)
    • Preview versions of the platform are identified by a code name. Their API level is usually set to the value of the previous platform.

    While this class contains all values, its goal is to abstract them, so that code comparing 2+ versions doesn't have to deal with the logic of handling all values.

    There are some cases where ones may want to access the values directly. This can be done with getApiLevel(), getCodename(), getExtensionLevel(), and isBaseExtension().

    For generic UI display of the API version, getApiString() is to be used.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      AndroidVersion​(int apiLevel)
      Creates an AndroidVersion with the given api level of a release version (the codename is null).
      AndroidVersion​(int apiLevel, java.lang.String codename)
      Creates an AndroidVersion with the given api level and codename.
      AndroidVersion​(int apiLevel, java.lang.String codename, java.lang.Integer extensionLevel, boolean isBaseExtension)
      Creates an AndroidVersion with the given api level, codename, and extension level.
      AndroidVersion​(java.lang.String apiOrCodename)
      Creates an AndroidVersion from a string that may be an integer API level or a string codename.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canRun​(AndroidVersion appVersion)
      Checks whether a device running a version similar to the receiver can run a project compiled for the given version.
      int compareTo​(int apiLevel, java.lang.String codename)  
      int compareTo​(AndroidVersion o)
      Compares this object with the specified object for order.
      boolean equals​(int apiLevel)
      Returns true if the AndroidVersion is an API level equals to apiLevel.
      boolean equals​(java.lang.Object obj)  
      int getApiLevel()
      Returns the api level as an integer.
      java.lang.String getApiString()
      Returns a string representing the API level and/or the code name.
      java.lang.String getCodename()
      Returns the version code name if applicable, null otherwise.
      java.lang.Integer getExtensionLevel()
      Returns the extension level if known.
      int getFeatureLevel()
      Returns the API level as an integer.
      int hashCode()  
      boolean isBaseExtension()
      Returns whether this AndroidVersion is the base extension for the API level.
      boolean isGreaterOrEqualThan​(int api)
      Compares this version with the specified API and returns true if this version is greater or equal than the requested API -- that is the current version is a suitable min-api-level for the argument API.
      boolean isGreaterOrEqualThan​(int api, int extensionLevel)
      Compares this version with the specified API and extension level, and returns true if this version is greater or equal than the requested API -- that is the current version is a suitable min-api-level for the argument API.
      boolean isLegacyMultidex()
      Checks if the version is having legacy multidex support.
      boolean isPreview()
      Returns whether or not the version is a preview version.
      java.lang.String toString()
      Returns a string with the API Level and optional codename.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT

        public static final AndroidVersion DEFAULT
        The default AndroidVersion for minSdkVersion and targetSdkVersion if not specified.
      • ART_RUNTIME

        public static final AndroidVersion ART_RUNTIME
        First version to use ART by default.
      • SUPPORTS_64_BIT

        public static final AndroidVersion SUPPORTS_64_BIT
        First version to support 64-bit ABIs.
      • BINDER_CMD_AVAILABLE

        public static final AndroidVersion BINDER_CMD_AVAILABLE
        First version to feature binder's common interface "cmd" for sending shell commands to services.
      • ALLOW_SPLIT_APK_INSTALLATION

        public static final AndroidVersion ALLOW_SPLIT_APK_INSTALLATION
        First version to allow split apks
      • SUPPORTS_MULTI_USER

        public static final AndroidVersion SUPPORTS_MULTI_USER
        First version to have multi-user support (JB-MR2, API 17)
      • MIN_RECOMMENDED_API

        public static final int MIN_RECOMMENDED_API
        Minimum API versions that are recommended for use in testing apps
        See Also:
        Constant Field Values
      • MIN_RECOMMENDED_WEAR_API

        public static final int MIN_RECOMMENDED_WEAR_API
        See Also:
        Constant Field Values
      • MIN_FOLDABLE_DEVICE_API

        public static final int MIN_FOLDABLE_DEVICE_API
        Frist version to support Foldable device
        See Also:
        Constant Field Values
      • MIN_FREEFORM_DEVICE_API

        public static final int MIN_FREEFORM_DEVICE_API
        First version to support freeform display
        See Also:
        Constant Field Values
      • MIN_HINGE_FOLDABLE_DEVICE_API

        public static final int MIN_HINGE_FOLDABLE_DEVICE_API
        First version to support hinge foldable settings
        See Also:
        Constant Field Values
      • MIN_PIXEL_4A_DEVICE_API

        public static final int MIN_PIXEL_4A_DEVICE_API
        First version to support pixel 4a
        See Also:
        Constant Field Values
      • MIN_4K_TV_API

        public static final int MIN_4K_TV_API
        First version to support TV 4K display
        See Also:
        Constant Field Values
      • MAX_32_BIT_API

        public static final int MAX_32_BIT_API
        Last version of Android with supported 32-bit system images.
        See Also:
        Constant Field Values
    • Constructor Detail

      • AndroidVersion

        public AndroidVersion​(int apiLevel,
                              @Nullable
                              java.lang.String codename)
        Creates an AndroidVersion with the given api level and codename. Codename should be null for a release version, otherwise it's a preview codename.
      • AndroidVersion

        public AndroidVersion​(int apiLevel)
        Creates an AndroidVersion with the given api level of a release version (the codename is null).
      • AndroidVersion

        public AndroidVersion​(@NonNull
                              java.lang.String apiOrCodename)
                       throws AndroidVersion.AndroidVersionException
        Creates an AndroidVersion from a string that may be an integer API level or a string codename. Important: An important limitation of this method is that cannot possible recreate the API level integer from a pure string codename. This is only OK to use if the caller can guarantee that only getApiString() will be used later. Wrong things will happen if the caller then tries to resolve the numeric getApiLevel().
        Parameters:
        apiOrCodename - A non-null API integer or a codename in its "ALL_CAPS" format. "REL" is notable not a valid codename.
        Throws:
        AndroidVersion.AndroidVersionException - if the input isn't a pure integer or doesn't look like a valid string codename.
      • AndroidVersion

        public AndroidVersion​(int apiLevel,
                              @Nullable
                              java.lang.String codename,
                              @Nullable
                              java.lang.Integer extensionLevel,
                              boolean isBaseExtension)
        Creates an AndroidVersion with the given api level, codename, and extension level. Codename should be null for a release version, otherwise it's a preview codename.
    • Method Detail

      • getApiLevel

        public int getApiLevel()
        Returns the api level as an integer.

        For target that are in preview mode, this can be superseded by getCodename().

        To display the API level in the UI, use getApiString(), which will use the codename if applicable.

        See Also:
        getCodename(), getApiString()
      • getFeatureLevel

        public int getFeatureLevel()
        Returns the API level as an integer. If this is a preview platform, it will return the expected final version of the API rather than the current API level. This is the "feature level" as opposed to the "release level" returned by getApiLevel() in the sense that it is useful when you want to check the presence of a given feature from an API, and we consider the feature present in preview platforms as well.
        Returns:
        the API level of this version, +1 for preview platforms
      • getCodename

        @Nullable
        public java.lang.String getCodename()
        Returns the version code name if applicable, null otherwise.

        If the codename is non null, then the API level should be ignored, and this should be used as a unique identifier of the target instead.

      • getApiString

        @NonNull
        public java.lang.String getApiString()
        Returns a string representing the API level and/or the code name.
      • getExtensionLevel

        @Nullable
        public java.lang.Integer getExtensionLevel()
        Returns the extension level if known.
      • isBaseExtension

        public boolean isBaseExtension()
        Returns whether this AndroidVersion is the base extension for the API level.
      • isPreview

        public boolean isPreview()
        Returns whether or not the version is a preview version.
      • isLegacyMultidex

        public boolean isLegacyMultidex()
        Checks if the version is having legacy multidex support.
      • canRun

        public boolean canRun​(@NonNull
                              AndroidVersion appVersion)
        Checks whether a device running a version similar to the receiver can run a project compiled for the given version.

        Be aware that this is not a perfect test, as other properties could break compatibility despite this method returning true.

        Nevertheless, when testing if an application can run on a device (where there is no access to the list of optional libraries), this method can give a good indication of whether there is a chance the application could run, or if there's a direct incompatibility.

      • equals

        public boolean equals​(int apiLevel)
        Returns true if the AndroidVersion is an API level equals to apiLevel.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Returns a string with the API Level and optional codename. Useful for debugging. For display purpose, please use getApiString() instead.
        Overrides:
        toString in class java.lang.Object
      • compareTo

        public int compareTo​(@NonNull
                             AndroidVersion o)
        Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
        Specified by:
        compareTo in interface java.lang.Comparable<AndroidVersion>
        Parameters:
        o - the Object to be compared.
        Returns:
        a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
      • compareTo

        public int compareTo​(int apiLevel,
                             @Nullable
                             java.lang.String codename)
      • isGreaterOrEqualThan

        public boolean isGreaterOrEqualThan​(int api)
        Compares this version with the specified API and returns true if this version is greater or equal than the requested API -- that is the current version is a suitable min-api-level for the argument API.
      • isGreaterOrEqualThan

        public boolean isGreaterOrEqualThan​(int api,
                                            int extensionLevel)
        Compares this version with the specified API and extension level, and returns true if this version is greater or equal than the requested API -- that is the current version is a suitable min-api-level for the argument API.