In order to compile Java 9+ source, please set compileSdkVersion to 30 or above – Android

by
Ali Hasan
android

The Problem:

When trying to build a project in Android Studio, an error occurs indicating that compileSdkVersion must be set to 30 or above to compile Java 9+ source, even though it has been set to 33 in the android/build.gradle file.

The Solutions:

Solution 1: Add the code to the `buildscript` section

To fix the error, you need to add the following code to the `buildscript` section in your build.gradle file:

subprojects { subproject ->
    afterEvaluate{
        if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

This will apply compileSdkVersion and buildToolsVersion to any android modules you have.

After adding this code, your build.gradle file should look something like this:

buildscript {
    ext {
        compileSdkVersion = 33
        buildToolsVersion = "33.0.0"
    }
    subprojects { subproject ->
        afterEvaluate{
            if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
                android {
                    compileSdkVersion rootProject.ext.compileSdkVersion
                    buildToolsVersion rootProject.ext.buildToolsVersion
                }
            }
        }
    }
}

Solution 2: Patch dependency

It is possible that one of the dependencies uses an older compileSdkVersion. If that is the case, patch the dependency to use the compileSdkVersion from android/build.gradle.

Q&A

I am trying to build a project in Android Studio and getting this error

In order to compile Java 9+ source, please set compileSdkVersion to 30 or above

How do I fix this?

Add the following code to the buildscript section in your build. gradle file.

I got this error too today

One of the dependencies in your react native project was using an older compileSdkVersion (29). Fixed it by patching the dependency to use the compileSdkVersion from android/build.gradle (https://github.com/vmurin/react-native-azure-auth/issues/185)

Video Explanation:

The following video, titled "How to Fix: The minCompileSdk (31) is greater than this module's ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

How to Fix: The minCompileSdk (31) is greater than this module's compileSdkVersion (android-30). 41K views · 2 years ago ...more. BoostMyTool.