Has this ever happened to you? I created an Android project from an Android Studio template project, and tried to build and install it onto a device. I’d expect no problems. After all, it’s a template, straight from the horse’s mouth. So what could go wrong?
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
> The minCompileSdk (31) specified in a
dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
is greater than this module's compileSdkVersion (android-30).
Dependency: androidx.appcompat:appcompat:1.4.0.
AAR metadata file: /home/fullstackdev/.gradle/caches/transforms-2/files-2.1/8f4b7ca2bae2afe820e06df077d8d5c8/appcompat-1.4.0/META-INF/com/android/build/gradle/aar-metadata.properties.
Sometimes projects inexplicably fail, so I tried to invalidate caches and restart… Nope! And then I noticed The minCompileSdk (31) specified in a dependency's AAR metadata is greater than this module's compileSdkVersion... Dependency: androidx.appcompat:appcompat:1.4.0
. Oh.
In my build.gradle (:app)
, it said:
… android { compileSdkVersion 30 buildToolsVersion “30.0.3” …
I replaced compileSdkVersion 30
with compileSdkVersion 31
et voila, the build was fixed.
There’s a StackOverflow answer with 635 upvotes that discusses this issue..
But why not just go straight to the description in the Android documents.. It’s concise. Here’s what it says:
/**
* The android block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*/
compileSdkVersion 28
Let’s emphasize that: This means your app can use the API features included in this API level and lower.. So if you ever get a build failure related to compileSdkVersion, there’s very likely no harm in bumping this up to the most recent version. Just give it a try, and see what happens.
That’s it! Feel free to email me with any questions: fullstackdev@fullstackoasis.com.