0
0
Android Kotlinmobile~5 mins

Android SDK and API levels in Android Kotlin

Choose your learning style9 modes available
Introduction

The Android SDK and API levels help your app work on different Android devices and versions.

When you want your app to run on many Android phones with different Android versions.
When you need to use new Android features that only work on newer versions.
When you want to make sure your app does not crash on older devices.
When you are testing your app on different Android versions.
When you update your app to support the latest Android features.
Syntax
Android Kotlin
android {
    compileSdk = 33
    defaultConfig {
        minSdk = 21
        targetSdk = 33
    }
}

compileSdk is the Android version your app is built against.

minSdk is the oldest Android version your app supports.

targetSdk is the Android version your app is tested for and optimized to run on.

Examples
This setup means the app supports Android 5.0 (API 21) and above, and is built for Android 13 (API 33).
Android Kotlin
android {
    compileSdk = 33
    defaultConfig {
        minSdk = 21
        targetSdk = 33
    }
}
This setup supports older devices from Android 4.1 (API 16) but targets Android 11 (API 30).
Android Kotlin
android {
    compileSdk = 30
    defaultConfig {
        minSdk = 16
        targetSdk = 30
    }
}
This means the app only runs on Android 13 (API 33) and above.
Android Kotlin
android {
    compileSdk = 33
    defaultConfig {
        minSdk = 33
        targetSdk = 33
    }
}
Sample App

This is a simple Android app setup in Kotlin that supports devices with Android 5.0 (API 21) and newer.

Android Kotlin
plugins {
    id("com.android.application")
    kotlin("android")
}

android {
    compileSdk = 33

    defaultConfig {
        applicationId = "com.example.sdkapilevel"
        minSdk = 21
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"
    }
}

fun main() {
    println("App supports Android API 21 and above.")
}
OutputSuccess
Important Notes

Always test your app on devices or emulators with different API levels to catch compatibility issues.

Using a higher compileSdk lets you use new Android features but does not affect which devices can install your app.

Setting minSdk too high limits your app to fewer devices, but setting it too low may require extra work to support old versions.

Summary

The Android SDK and API levels control which Android versions your app supports and uses.

compileSdk is the version you build against, minSdk is the oldest supported version, and targetSdk is the version your app is optimized for.

Choosing the right API levels helps your app reach more users and use new features safely.