When you first create an Android Studio project, you can set the minimum SDK level. Each platform has an equivalent API level. You set this value in the module’s Gradle build file, and you can change it whenever you need to during development.
- [Instructor] When you first create an Android Studio project, you can set the minimum SDK level. You do this in the gradle build file for your module. I'll go to the project window to the gradle script section and open build.gradle from the app module. In this application, my minimum SDK version is set to 19. That's Android 4.4, or KitKat. My compile and target SDK version are both set to 26 for Android 8, or Android O. To match that version, I'm using build tools version 26.0, and I'm using a support library that starts with 26.
Notice I'm using the plus character to indicate any version that starts with 26 is fine, and that's to make these projects a little bit more adaptable as Android 8 goes through its development cycle. Now when I run this application, I'll run it on three devices at the same time. I've created and am running three virtual devices. This first one is running on API 19, that's Android 4.4 or KitKat, the next one is on API 25 for Android seven, or Nougat, and the third one is API 26 for Android eight.
And for all three of them, when I click the run code button, I just see some information displayed on the screen. So now I'm going to add a bit of code in my main activity class. I pressed the shift key twice to open search everywhere, and then I'll select main activity, and then I'll go to the method on run button click, and I'll get rid of this existing code. In order to test the current device's API level, use this code. I'll create an integer named API level, and I'll get its value from Android.OS.build.version.SDK_int.
Now that shortens to just build.version.SDK_int, and that's the entire expression that I need. Next, I'll use my display message method and I'll output API level equals, and then I'll append that integer. And I'll run the code again on all three devices. When I click the button, I see that the first device is running API 19, the second 25, and the third 26.
You can also use that expression to do some conditional programming. For example, there might be a particular library or method call that you can only use at a certain API level and above, and you can test that code like this. I'll use a conditional block, and my condition will be if API level is greater than or equal to, and then I'll specify a particular version with version codes dot, and I'll see a whole listing of constants that represent various versions of Android.
Some of them actually have the complete code names like Froyo, Gingerbread, Honeycomb, and so on, while others are represented simply by individual characters. I'm going to look for Android M, also known as Android six, or Marshmallow. And if that condition is true, I'll display a message that says, "working on Marshmallow or later." I'll add an else condition, and in that condition, I'll display the message, "working on pre-Marshmallow version." Once again, I'll run the app on all three devices, and I'll run the code, and I see an accurate evaluation that tells me what I'm able to do on that particular device.
So the minimum SDK setting in the gradle build file lets you restrict which devices your app can be installed on, at least through the Google Play store, but you can also use conditional code where you need to branch your code intelligently, and make sure you aren't trying to call libraries and APIs that may not be present on older devices.
- Supporting different platform versions
- Setting minimum and target platforms
- Working with multiple screens
- Creating alternative resource directories, XML layout files, and bitmap resources
- Creating dynamic UIs with fragments
- Creating a project with fragments
- Controlling a fragment with Java code