Gradle cannot find compileSdk in the android block
Could not find method compileSdk()Short answer
Gradle only knows compileSdk when the Android Gradle plugin that is applied provides it, and only inside the right block. Make sure it sits in the module's android block, that the plugin version is recent enough, and that you do not set it in settings.gradle without the settings plugin.
Fix it fast
1Find out which cause you have
Open the stack trace's first line after the error: it names the file and line. Then see which Android Gradle plugin version the project really applies.
grep -rn "com.android" build.gradle build.gradle.kts settings.gradle settings.gradle.kts gradle/libs.versions.toml2Match what you see, then jump to its fix
- 1
The failing line is at the top level of the file, or inside a block such as defaultConfig or buildTypescompileSdk is written outside the android blockFix - 2
The block is correct and the project was copied from a new template into an old buildThe Android Gradle plugin in use is too old to have compileSdkFix - 3
The failing file is settings.gradle, not a module build filecompileSdk is set in settings.gradle without the settings pluginFix
- 1
What does the error look like?
Could not find method compileSdk()org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException
Cause 1 of 3
compileSdk is written outside the android block
1Check
The DSL reference describes compileSdk as a property you add to the android block, next to the other module settings.
grep -n -B3 "compileSdk" app/build.gradle app/build.gradle.kts2Fix
Move the line into android { }.
// app/build.gradle.kts
android {
namespace = "com.example.app"
compileSdk = 36
}3Verify
Sync the project.
./gradlew :app:helpConfirmed by developer.android.com
Cause 2 of 3
The Android Gradle plugin in use is too old to have compileSdk
1Check
In the AGP 8.0 DSL reference compileSdkVersion is deprecated and marked as replaced by compileSdk, so an older plugin lacks the newer name. Compare the plugin version from the first step with the current release.
./gradlew buildEnvironment | grep -i "com.android.tools.build"2Fix
Upgrade the Android Gradle plugin, for example with Android Studio's AGP Upgrade Assistant. If you cannot upgrade yet, use the old name, which the 8.0 DSL still lists as deprecated.
// app/build.gradle (old plugin only)
android {
compileSdkVersion 34
}3Verify
Sync and build.
./gradlew :app:assembleDebugConfirmed by developer.android.com
Cause 3 of 3
compileSdk is set in settings.gradle without the settings plugin
1Check
AGP 8.0 introduced a settings plugin that allows an android block with compileSdk in settings.gradle, and it only works after that plugin is applied.
grep -n "com.android.settings" settings.gradle2Fix
Apply the com.android.settings plugin in settings.gradle before the android block, or move compileSdk back to the module's build file.
// settings.gradle
android {
compileSdk 36
}3Verify
Run a Gradle command and confirm the settings file loads.
./gradlew projectsConfirmed by developer.android.com
How do I stop it coming back?
- Change the Android Gradle plugin version on its own, in one commit, and sync right after.
- Copy build snippets together with the plugin version they came from.
- Keep compileSdk, minSdk and targetSdk in the same android block so they are easy to find.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile apps
Android Studio cannot pair the phone over Wi-Fi
The Pair devices over Wi-Fi dialog lists no device
Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.
4 causes, each with a fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile apps
adb tcpip 5555 fails because no device is found
adb: more than one device/emulator
adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.
4 causes, each with a fix