Targeting platform versions
To keep up with the latest technology, new versions of the Android platform are released frequently. As developers, this means we can incorporate the newest features and developments into our applications. The obvious drawback to this is the fact that only the very newest devices will be able to run this platform and these devices represent only a tiny proportion of the entire market. Take a look at this chart taken from the developer dashboard:
The dashboard can be found at developer.android.com/about/dashboards/index.html and contains this and other up-to-date information that is very useful when first planning a project.
As you can see, the vast majority of Android devices still run on older platforms. Fortunately, Android makes it possible for us to target these older devices while still being able to incorporate features from the most recent platform version. This is largely achieved through the use of the support library and by setting a minimum SDK level.
Deciding which platforms to target is one of the first decisions we will need to take, and although it is possible to change this at a later date, deciding early which features to incorporate and knowing how these will appear on older devices can greatly simplify the overall task.
To see how this is done, start a new Android Studio project, call it anything you choose, and select Phone and Tablet as the form factor and API 16 as the Minimum SDK.
From the list of templates, select Empty Activity and leave everything else as is.
Android Studio will automatically select the highest available SDK version as the target level. To see how this is applied, open the build.gradle (Module: app)
file from the project pane and note the defaultConfig
section, which will resemble the following code:
defaultConfig { applicationId "com.example.kyle.factoryexample" minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" }
This ensures that our project will compile correctly for this range of API levels, but if we were building an app that we intended to publish, then we would need to inform the Google Play store which devices to make our app available on. This can be done with the build.gradle
modular file, like so:
minSdkVersion 21 targetSdkVersion 24
We would also need to edit AndroidManifest.xml
file. For the example here, we would add the following uses-sdk
element to the manifest
node:
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
Once we have determined the range of platforms we wish to target, we can get on and see how the support library allows us to incorporate many of the latest features on many of the oldest devices.