5

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

In my android/build.gradle file I have set

 compileSdkVersion = 33

How do I fix this?

Matt
  • 33,328
  • 25
  • 83
  • 97

2 Answers2

12

You can fix ths by adding this 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
            }
        }
    }
}

So afterwards it should look something like:

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
                }
            }
        }
    }
}

This will apply compileSdkVersion and buildToolsVersion to any android modules you have.
Source: https://stackoverflow.com/a/25736483

ut9081
  • 787
  • 6
  • 11
0

I got this error too today. In my case one of the dependencies (react-native-azure-auth) in my 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) and also opened a PR for it (https://github.com/vmurin/react-native-azure-auth/pull/186).

mihai1990
  • 632
  • 5
  • 20