A failure occurred while executing CheckAarMetadataWorkAction

Valéry Raulet
3 min readJun 12, 2023

--

Photo by Stephen Tafra on Unsplash

I recently upgraded a Unity project from 2021 to 2022. Even though the upgrade went smoothly, I later realized that something was not working like before!

You may have encountered this error recently (it happened to me in 2022.3.0f1 & 2022.3.1f1):

Unity failed to build

When checking the logs, I discovered that Gradle failed to build the application for Android with the following message:

CommandInvokationFailure: Gradle build failed. 
G:\Software\UnityEditors\2022.3.1f1\Editor\Data\PlaybackEngines\AndroidPlayer\OpenJDK\bin\java.exe -classpath "G:\Software\UnityEditors\2022.3.1f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-7.2.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"
...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':launcher:checkReleaseAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
> One or more issues found when checking AAR metadata values:

Dependency ':java-library:' requires 'compileSdkVersion' to be set to 33 or higher.
Compilation target for module ':launcher' is 'android-32'
...

What I have here is a Java library (java-library.aar) that requires Android 33 to compile. I can easily check that by decompressing the library and navigation to META-INF\com\android\build\gradle\aar-metadata.properties:

aarFormatVersion=1.0
aarMetadataVersion=1.0
minCompileSdk=33
minAndroidGradlePluginVersion=1.0.0

In the properties file, minCompileSdk clearly specifies version 33.

However, if I check my Unity Player settings, I have the following:

Minimum & Target API Levels for Android SDK

I could solve this issue by setting the Target API Level to a version equal to or higher than 33. However, the device I am targeting requires a target API Level of up to 32!

To solve this, you need to customize your Launcher Gradle file. To achieve this, go to your player Publishing Settings and check Custom Launcher Gradle Template:

Custom Launcher Gradle Template

This will create a new file named launcherTemplate.gradle in Assets/Plugins/Android. All you have to do is open the file in an editor and make the following amendment:

android {
ndkPath "**NDKPATH**"

// -------------------------- STARTS HERE --------------------------
// compileSdkVersion **APIVERSION**
compileSdkVersion 33
// --------------------------- ENDS HERE ---------------------------

buildToolsVersion '**BUILDTOOLS**'

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

defaultConfig {
minSdkVersion **MINSDKVERSION**
targetSdkVersion **TARGETSDKVERSION**
applicationId '**APPLICATIONID**'
ndk {
abiFilters **ABIFILTERS**
}
versionCode **VERSIONCODE**
versionName '**VERSIONNAME**'
}

By default, Unity uses the same version as targetSdkVersion for compileSdkVersion. By hardcoding the version in this custom gradle file, we explicitly define which Android SDK to use for compiling with gradle.

Notes

Unity should automatically add the SDK dependency when building your project. It should installs

  • android-33 in <InstallPath>\2022.3.1f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms
  • 33.0.0 in
    <InstallPath>\2022.3.1f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\build-tools

If, for any reason, it fails to do so, you can manually force the installation. Open a terminal and execute the following:

cd <InstallPath>\2022.3.0f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK
cmdline-tools\6.0\bin\sdkmanager.bat "platforms;android-33"
cmdline-tools\6.0\bin\sdkmanager.bat "build-tools;33.0.0"

--

--

Valéry Raulet
Valéry Raulet

Written by Valéry Raulet

I have been interested in business and technology since I was about 10. My interest spans across so many fields but I hope you’ll find my writing useful!

Responses (4)