Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Gradle cannot find compileSdk in the android block

ErrorCould 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.

  • Applies to Android Gradle plugin 8.x and 9.x, Gradle 8.x and 9.x, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.toml
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The failing file is settings.gradle, not a module build filecompileSdk is set in settings.gradle without the settings pluginFix

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.kts

2Fix

Move the line into android { }.

// app/build.gradle.kts
android {
    namespace = "com.example.app"
    compileSdk = 36
}

3Verify

Sync the project.

./gradlew :app:help

Confirmed by developer.android.com

Did this fix it?

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:assembleDebug

Confirmed by developer.android.com

Did this fix it?

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.gradle

2Fix

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 projects

Confirmed by developer.android.com

Did this fix it?

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

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 fix

Mobile 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.