Bird
Raised Fist0
Android Kotlinmobile~20 mins

Android SDK and API levels in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: SDK Info Screen
This screen shows the current Android SDK version and API level of the device. It helps users understand what Android version their device is running.
Target UI
-------------------------
|    SDK Info Screen     |
|-----------------------|
| SDK Version:          |
| API Level:            |
|                       |
| [Refresh]             |
-------------------------
Display the current Android SDK version as a text label.
Display the current Android API level as a text label.
Add a Refresh button that updates the displayed SDK version and API level when tapped.
Use a vertical layout with proper spacing.
Ensure the text is centered horizontally.
Use accessible labels for screen readers.
Starter Code
Android Kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SdkInfoActivity : AppCompatActivity() {
    private lateinit var sdkVersionTextView: TextView
    private lateinit var apiLevelTextView: TextView
    private lateinit var refreshButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sdk_info)

        sdkVersionTextView = findViewById(R.id.sdkVersionTextView)
        apiLevelTextView = findViewById(R.id.apiLevelTextView)
        refreshButton = findViewById(R.id.refreshButton)

        // TODO: Add code to display SDK version and API level
        // TODO: Add click listener to refreshButton to update info
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SdkInfoActivity : AppCompatActivity() {
    private lateinit var sdkVersionTextView: TextView
    private lateinit var apiLevelTextView: TextView
    private lateinit var refreshButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sdk_info)

        sdkVersionTextView = findViewById(R.id.sdkVersionTextView)
        apiLevelTextView = findViewById(R.id.apiLevelTextView)
        refreshButton = findViewById(R.id.refreshButton)

        fun updateSdkInfo() {
            val sdkVersion = Build.VERSION.RELEASE ?: "Unknown"
            val apiLevel = Build.VERSION.SDK_INT.toString()
            sdkVersionTextView.text = "SDK Version: $sdkVersion"
            apiLevelTextView.text = "API Level: $apiLevel"
        }

        updateSdkInfo()

        refreshButton.setOnClickListener {
            updateSdkInfo()
        }
    }
}

This code uses Build.VERSION.RELEASE to get the Android SDK version as a string, like "13" or "12". It uses Build.VERSION.SDK_INT to get the API level as an integer, like 33 or 31.

We define a function updateSdkInfo() that sets the text of the two TextViews with this info. We call it once when the screen loads to show the info immediately.

The Refresh button has a click listener that calls updateSdkInfo() again, so if the device info changes (rare but possible), the user can update the display.

This keeps the UI simple and clear, with accessible text labels and a button that updates the info on demand.

Final Result
Completed Screen
-------------------------
|    SDK Info Screen     |
|-----------------------|
| SDK Version: 13       |
| API Level: 33         |
|                       |
| [Refresh]             |
-------------------------
When the screen opens, it shows the current SDK version and API level.
Tapping the Refresh button updates the SDK version and API level displayed.
Stretch Goal
Add a dark mode toggle button that switches the screen background and text colors between light and dark themes.
💡 Hint
Use AppCompatDelegate.setDefaultNightMode() with MODE_NIGHT_YES and MODE_NIGHT_NO to switch themes programmatically.

Practice

(1/5)
1. What does the minSdk version specify in an Android app?
easy
A. The latest Android version available
B. The Android version you compile your app against
C. The Android version your app is optimized for
D. The oldest Android version your app can run on

Solution

  1. Step 1: Understand minSdk meaning

    minSdk defines the minimum Android version that can run the app.
  2. Step 2: Differentiate from other SDK versions

    compileSdk is for building, targetSdk is optimization, but minSdk is the oldest supported version.
  3. Final Answer:

    The oldest Android version your app can run on -> Option D
  4. Quick Check:

    minSdk = oldest supported version [OK]
Hint: minSdk means minimum Android version supported [OK]
Common Mistakes:
  • Confusing minSdk with compileSdk
  • Thinking minSdk is the target optimization version
  • Assuming minSdk is the latest Android version
2. Which of the following is the correct way to set the compileSdk version to 33 in a Kotlin DSL Gradle file?
easy
A. compileSdkVersion 33
B. compileSdkVersion = 33
C. compileSdk = 33
D. compileSdk(33)

Solution

  1. Step 1: Identify Kotlin DSL syntax for compileSdk

    In Kotlin DSL, compileSdk is assigned directly with =.
  2. Step 2: Compare options

    compileSdkVersion 33 is Groovy syntax without assignment, compileSdkVersion = 33 uses the wrong property name, and compileSdk(33) is invalid function call syntax. Only compileSdk = 33 is correct.
  3. Final Answer:

    compileSdk = 33 -> Option C
  4. Quick Check:

    Kotlin DSL uses compileSdk = 33 [OK]
Hint: Kotlin DSL uses equals sign for assignments [OK]
Common Mistakes:
  • Using Groovy syntax in Kotlin DSL
  • Omitting equals sign
  • Using method call syntax incorrectly
3. Given this Gradle configuration snippet:
android {
  compileSdk = 31
  minSdk = 21
  targetSdk = 30
}
What is the highest Android version your app is optimized for?
medium
A. 30
B. 21
C. 31
D. Cannot determine from this

Solution

  1. Step 1: Identify meaning of targetSdk

    targetSdk is the Android version the app is optimized for and tested against.
  2. Step 2: Check given values

    Here, targetSdk is 30, so the app is optimized for Android 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    targetSdk = 30 means optimized for Android 30 [OK]
Hint: targetSdk shows optimization version, not compileSdk [OK]
Common Mistakes:
  • Confusing compileSdk with targetSdk
  • Thinking minSdk is optimization version
  • Choosing compileSdk as optimization version
4. You set minSdk = 30 but want your app to run on Android 21 devices. What is the problem in this Gradle config?
android {
  compileSdk = 33
  minSdk = 30
  targetSdk = 33
}
medium
A. compileSdk must be lower than minSdk
B. minSdk is too high, app won't run on Android 21
C. targetSdk must be equal to minSdk
D. No problem, app runs on all versions

Solution

  1. Step 1: Understand minSdk role

    minSdk sets the lowest Android version the app supports.
  2. Step 2: Check compatibility with Android 21

    Since minSdk is 30, devices with Android 21 cannot run the app.
  3. Final Answer:

    minSdk is too high, app won't run on Android 21 -> Option B
  4. Quick Check:

    minSdk = 30 excludes Android 21 devices [OK]
Hint: minSdk must be ≤ lowest Android version supported [OK]
Common Mistakes:
  • Thinking compileSdk affects device compatibility
  • Assuming targetSdk limits device support
  • Believing app runs on all versions regardless of minSdk
5. You want to use a new Android feature introduced in API level 31 but still support devices running API level 21. How should you configure your Gradle file to safely use this feature?
hard
A. Set minSdk = 21, targetSdk = 31, and use runtime checks for API 31 features
B. Set minSdk = 21 and targetSdk = 21
C. Set minSdk = 31 and targetSdk = 31
D. Set compileSdk = 21 and minSdk = 31

Solution

  1. Step 1: Understand minSdk and targetSdk roles

    minSdk is the oldest supported version; targetSdk is the optimized version.
  2. Step 2: Use runtime checks for new features

    To support API 21 devices but use API 31 features, keep minSdk = 21, set targetSdk = 31, and check API level at runtime before using new features.
  3. Step 3: Evaluate options

    Set minSdk = 21, targetSdk = 31, and use runtime checks for API 31 features correctly sets minSdk and targetSdk and mentions runtime checks. Others either restrict support or use wrong compileSdk.
  4. Final Answer:

    Set minSdk = 21, targetSdk = 31, and use runtime checks for API 31 features -> Option A
  5. Quick Check:

    minSdk low + targetSdk high + runtime checks = safe feature use [OK]
Hint: Use runtime API checks to support older devices safely [OK]
Common Mistakes:
  • Raising minSdk and losing older device support
  • Not using runtime checks for new APIs
  • Setting compileSdk lower than targetSdk
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard