In an Android app module's build file, what do compileSdk, minSdk and targetSdk each control, and how do they relate?
answer
- three numbers, three different jobs
- what the compiler can see
- who may install the app
- runtime behaviour and compatibility mode
basics
~10 scompileSdk sets which Android APIs your code compiles against, minSdk sets the oldest Android version that can install the app, and targetSdk declares which version's runtime behaviour the app was tested for.
solid answer
~40 s`compileSdk` is the SDK platform the build compiles against: it decides which classes and methods your code may reference, and it does not change how the app behaves at run time. `minSdk` lands in the merged manifest's `<uses-sdk>`: the system refuses to install the app below that API level, Google Play hides it from those devices, and lint warns when you call a newer API without a version check. `targetSdk` attests the version you tested against: on a newer device the platform keeps compatibility behaviours for older targets, and raising it opts you into each release's behaviour changes, such as Android 17 (API 37) removing the opt-out from ignored large-screen orientation and resizability limits. Teams usually keep `minSdk <= targetSdk <= compileSdk`, but `targetSdk` and `compileSdk` are not tied to each other.
code
kotlin · 12 linesandroid {
namespace = "com.example.notes"
compileSdk = 37
defaultConfig {
applicationId = "com.example.notes"
minSdk = 24
targetSdk = 37
versionCode = 42
versionName = "3.1.0"
}
}go deeper
Know which of the three numbers controls installation, which controls compilation and which controls runtime behaviour, and say it without mixing them up.
Explain compatibility behaviours: why an app targeting an older level behaves differently on a new device, and why lint's minSdk warnings matter at run time.
Describe how you run a target bump safely: read the release's behaviour changes, test on that release, and keep the compile, target and minimum moves separate.
Frame the three numbers as policy: who owns the yearly target bump against Play's deadline, and how minSdk follows library floors and user data.
## Three numbers, three different jobs Every Android app module declares three **API levels** in its `build.gradle.kts`. They look alike, and new Android engineers often treat them as one dial, but each answers a different question: | Setting | Question it answers | When it acts | |---|---|---| | `compileSdk` | Which Android APIs can my code reference? | At compile time | | `minSdk` | What is the oldest Android version that may install the app? | At install time and in Google Play's device filtering | | `targetSdk` | Which Android version's behaviour has the app been tested against? | At run time, on every device | An **API level** is a single integer that identifies one revision of the framework API: Android 17 is API level 37. It is not the marketing version number, and you cannot derive one from the other. ## compileSdk: what the compiler can see `compileSdk` picks the Android SDK platform your Kotlin and Java sources are compiled against. - Raising it makes newer classes and methods visible to the compiler, so you can call them, behind a version check when they are newer than `minSdk`. - Raising it does not by itself opt the app into a release's behaviour changes; that is the job of `targetSdk`. - Library updates often need a newer `compileSdk`, and Android Studio warns when the value conflicts with the Android Gradle plugin or a dependency's requirements. - For a minor platform release, the expanded block `compileSdk { version = release(36) { minorApiLevel = 1 } }` compiles against a minor API level. ## minSdk: who can install the app - The build writes it into the merged manifest as `android:minSdkVersion` on `<uses-sdk>`. The system refuses to install the app on a device with a lower API level, and Google Play uses the same value to keep the app off those devices. - Lint warns when code calls an API newer than `minSdk` without a guard such as `if (Build.VERSION.SDK_INT >= ...)`. Ignoring the warning still compiles, and the app crashes on the older device when it reaches that call. - A library's own `minSdk` is a floor for every app that uses it: the app's `minSdk` must be equal to or greater than each library's. ## targetSdk: which behaviour the app gets `targetSdk` has two purposes: it sets the app's runtime behaviour, and it attests which Android version you tested against. When the device runs a newer Android than the target, the platform can keep **compatibility behaviours** so the app keeps working as it did. The classic example: API 23 introduced runtime permissions, and an app that still targeted 22 kept install-time permissions on an API 23 device. Raising the target opts the app into the behaviour changes of every release it skips over. Two from Android 17 (API level 37): - Android 16 started ignoring orientation, aspect-ratio and resizability restrictions on large screens (smallest width 600dp and up) for apps targeting 36, with an opt-out. Apps targeting 37 no longer have that opt-out. - Apps targeting 37 get a new lock-free implementation of `android.os.MessageQueue`, which can break code that reflects on its private fields. Google Play adds a policy floor on top: from 31 August 2026, new apps and app updates must target Android 16 (API level 36) or higher, with lower floors for Wear OS, Android Automotive OS, TV and XR apps. ## How the three relate `targetSdk` is not connected to `compileSdk`: the target may be lower than, equal to or even higher than the compile level, though in practice teams keep `minSdk <= targetSdk <= compileSdk`. A yearly platform update usually runs in this order: 1. Raise `compileSdk` and update libraries; fix compile errors and new lint warnings. 2. Read the new release's behaviour changes for apps that target it. 3. Raise `targetSdk`, then test on a device or emulator running that release. 4. Treat `minSdk` separately, as a product decision about which users you still serve. ## Mistakes that come up in interviews - Saying `targetSdk` decides who can install the app; that is `minSdk`. - Believing a higher `compileSdk` changes behaviour on devices; it only changes what compiles. - Treating a target bump as a number edit instead of a tested behaviour change. - Assuming that code which compiles runs everywhere down to `minSdk`; only a version check or a compatibility library makes that true.
- What happens when you raise compileSdk but leave targetSdk where it was?Your code can reference the newer APIs, behind a version check where they exceed `minSdk`, and library updates that demand the newer `compileSdk` start to build. Runtime behaviour does not change: on a newer device the platform still applies the compatibility behaviour of the older target. Opting into the new behaviour is a separate, tested step of raising `targetSdk`.
- Why can't an app on Google Play keep an old targetSdk forever?Play enforces a target API floor. From 31 August 2026 new apps and updates must target Android 16 (API 36) or higher, and existing apps below Play's floor for them (API 35) stop being offered to new users on devices running a newer Android than the app targets. An old target also keeps users out of the security and privacy changes that each release gates on `targetSdk`.
- targetSdk decides which devices are allowed to install the app
- A higher compileSdk changes how the app behaves on devices
- Raising targetSdk only unlocks new APIs and needs no testing
- If it compiles against compileSdk, it runs on every device down to minSdk
- compileSdk and targetSdk must always be the same number
Assumes Android 17 (API level 37) as the newest target; targeting 37 removes the large-screen orientation and resizability opt-out that API 36 still allowed.