public final class AndroidVersion extends java.lang.Object implements java.lang.Comparable<AndroidVersion>
Represents the version of a target or device.
A version is defined by an API level and an optional code name.null
instead.)While this class contains both values, its goal is to abstract them, so that code comparing 2+ versions doesn't have to deal with the logic of handle both values.
There are some cases where ones may want to access the values directly. This can be done
with getApiLevel()
and getCodename()
.
getApiString()
is to be used.Modifier and Type | Class and Description |
---|---|
static class |
AndroidVersion.AndroidVersionException
Thrown when an
AndroidVersion object could not be created. |
static class |
AndroidVersion.VersionCodes
SDK version codes mirroring ones found in Build#VERSION_CODES on Android.
|
Modifier and Type | Field and Description |
---|---|
static AndroidVersion |
ALLOW_SPLIT_APK_INSTALLATION
First version to allow split apks
|
static AndroidVersion |
ART_RUNTIME
First version to use ART by default.
|
static AndroidVersion |
BINDER_CMD_AVAILABLE
First version to feature binder's common interface "cmd" for sending shell commands to services.
|
static AndroidVersion |
DEFAULT
The default AndroidVersion for minSdkVersion and targetSdkVersion if not specified.
|
static int |
MIN_RECOMMENDED_API
Minimum API versions that are recommended for use in testing apps
|
static int |
MIN_RECOMMENDED_WEAR_API |
static AndroidVersion |
SUPPORTS_MULTI_USER
First version to have multi-user support (JB-MR2, API 17)
|
Constructor and 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(java.lang.String apiOrCodename)
Creates an
AndroidVersion from a string that may be an integer API level or a string
codename. |
Modifier and Type | Method and 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(AndroidVersion o)
Compares this object with the specified object for order.
|
int |
compareTo(int apiLevel,
java.lang.String codename) |
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.
|
int |
getFeatureLevel()
Returns the API level as an integer.
|
int |
hashCode() |
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 |
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.
|
public static final AndroidVersion DEFAULT
public static final AndroidVersion ART_RUNTIME
public static final AndroidVersion BINDER_CMD_AVAILABLE
public static final AndroidVersion ALLOW_SPLIT_APK_INSTALLATION
public static final AndroidVersion SUPPORTS_MULTI_USER
public static final int MIN_RECOMMENDED_API
public static final int MIN_RECOMMENDED_WEAR_API
public AndroidVersion(int apiLevel, @Nullable java.lang.String codename)
AndroidVersion
with the given api level and codename.
Codename should be null for a release version, otherwise it's a preview codename.public AndroidVersion(int apiLevel)
AndroidVersion
with the given api level of a release version (the codename
is null).public AndroidVersion(@NonNull java.lang.String apiOrCodename) throws AndroidVersion.AndroidVersionException
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()
.apiOrCodename
- A non-null API integer or a codename in its "ALL_CAPS" format. "REL" is
notable not a valid codename.AndroidVersion.AndroidVersionException
- if the input isn't a pure integer or doesn't look like a
valid string codename.public int getApiLevel()
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.
getCodename()
,
getApiString()
public int getFeatureLevel()
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.@Nullable public java.lang.String getCodename()
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.
@NonNull public java.lang.String getApiString()
public boolean isPreview()
public boolean isLegacyMultidex()
public boolean canRun(@NonNull AndroidVersion appVersion)
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.
public boolean equals(int apiLevel)
true
if the AndroidVersion is an API level equals to
apiLevel.public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
getApiString()
instead.toString
in class java.lang.Object
public int compareTo(@NonNull AndroidVersion o)
compareTo
in interface java.lang.Comparable<AndroidVersion>
o
- the Object to be compared.public int compareTo(int apiLevel, @Nullable java.lang.String codename)
public boolean isGreaterOrEqualThan(int api)