Unity 2021.3.38 with Android API 34 Build fails

Hi everyone. My project builds fine with API 33. When I Changed it to API 34 it gives the following error.

I hope there will be a solution until August :slight_smile:

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':launcher:bundleReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.Aapt2ProcessResourcesRunnable
   > AAPT2 aapt2-4.2.0-7147631-windows Daemon #0: Unexpected error during link, attempting to stop daemon.
     This should not happen under normal circumstances, please file an issue if it does.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':launcher:processReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask$TaskAction
   > AAPT2 aapt2-4.2.0-7147631-windows Daemon #1: Unexpected error during link, attempting to stop daemon.
     This should not happen under normal circumstances, please file an issue if it does.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

* Get more help at https://help.gradle.org

BUİLD FAILED in 7s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
2 Likes

Same problem with Unity 2021.3.23f1 version. Any help?

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':launcher:bundleReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.Aapt2ProcessResourcesRunnable
   > AAPT2 aapt2-4.2.0-7147631-windows Daemon #0: Unexpected error during link, attempting to stop daemon.
     This should not happen under normal circumstances, please file an issue if it does.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':launcher:processReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask$TaskAction
   > AAPT2 aapt2-4.2.0-7147631-windows Daemon #1: Unexpected error during link, attempting to stop daemon.
     This should not happen under normal circumstances, please file an issue if it does.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================

* Get more help at https://help.gradle.org

BUİLD FAILED in 15s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Do you have a repro project you could share with us? What additional libraries are you using in your project?

1 Like

It seems the issue accour with Facebook. I tried to replicate with minimum SDK. And created a new project with only:

  • UEDM 1.2.179
  • Facebook 17.0.1
  • com.google.android.appbundle: 1.8.0
  • com.google.play.common: 1.8.1
  • com.google.play.core: 1.8.1
  • com.google.play.review: 1.8.1
1 Like

any update for this issue?

Hi folks, I assume this is for 2021 not 2023 right? If 2021 please do change the title if you can so other folks could find it.

We are aware of this issue, we continue investigating with high priority and expect to have some further information soon. This is an AGP (Android Gradle Plugin) linking bug. The bug will show up when specific Android legacy libraries are added as dependencies to an Android Project, and the project targets API 34. These libraries are usually added through a third-party SDK.

In the meantime, we have identified a couple of workarounds you could try. Can you try them in your projects and give us feedback if they work and if your projects look good?

Option 1 - set compileSDK to 33
Enable custom templates /launcherTemplate and set compileSdk 33 while raising targetSdk 34. This should enable your project to move past the issue. (This may not work for you if some of your plugins require compileSdk 34)

Option 2 - Direct Gradle to use a newer AAPT2 version to compile your project. You can do this by

  1. Visit the AAPT2 page on Google Maven
  2. Download AAPT2 7.2.2 JAR for your build architecture
  3. Unpack jar file
  4. From Unity editor, go to Player SettingsPublishingCustom Gradle Templates
  5. Enable Custom Gradle Properties Template.
  6. Open the newly created: Assets/Plugins/Android/gradleTemplate.properties file.
  7. Add a line with android.aapt2FromMavenOverride=/replace/with/path/to/your/aapt2
    This will instruct the Android Gradle Plugin to use this newer version of the linking tool.
  8. Build as usual. This should enable your project to move past the issue.
2 Likes

Someone else on this forum tried setting Min API level to 24 to bypass similar issue. Try if it works.

1 Like

@Voxel-Busters thanks for the tip - sadly minSDK 24 did not work in our reproduction project with those library dependencies, FB inclusion, and the specific agp/gradle settings.

I tried it using

android.aapt2FromMavenOverride=C:\Users\MONSTER\Downloads\aap\aapt2.exe

But this is the error I get:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':launcher:linkReleaseManifestForAssetPacks'.
> Illegal char <:> at index 68: D:\GitHub\TheGame\Library\Bee\Android\Prj\IL2CPP\Gradle\launcher\C:

Can you please explain the part “unpck” and “path”. I unpacked it using jar xf.

Oh. Is it possible to share the sample project to see if there are any other quicker solutions possible? I’m not sure why I’m unable to reproduce the issue on my end. Also, thanks for checking the suggested solution!

@LegacySystem you likely need to escape those try \ instead of \ eg C:\Users\MONSTER\Downloads\aap\aapt2.exe or this should also work with gradle if i recollect C:/Users/MONSTER/Downloads/aap/aapt2.exe try it out and let us know

@Voxel-Busters You can reproduce it a couple of ways with Unity 2021.3 default setup.

  1. Add Facebook SDK and change Target API to 34 and build
    OR
  2. Create a blank project in 2021.3 change Target API to 34
  3. Enable custom templates gradle.properties and main Template
  4. On main Template under *deps add
    implementation ‘androidx.legacy:legacy-support-v4:1.0.0’
    implementation ‘androidx.legacy:legacy-support-v13:1.0.0’
  5. On gradle properties add
    android.useAndroidX=true
    android.enableJetifier=true

Thank you. I tried it both andboth seemed to pass that error. But I got a new error (I love the smell of new build error in the morning:smile:).

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':launcher:dexBuilderRelease'.
> Could not resolve all files for configuration ':launcher:detachedConfiguration4'.
   > Failed to transform jetified-play-services-measurement-api-21.6.2-runtime.jar to match attributes {artifactType=ext-dex-dexBuilderRelease, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for DexingExternalLibArtifactTransform: C:\Users\MONSTER\.gradle\caches\transforms-2\files-2.1\841f89a34660ff6a15f4a05b1ef11144\jetified-play-services-measurement-api-21.6.2-runtime.jar.
         > Error while dexing.

A quick search suggested to update the Firebase. I was using 11.9. Updated to 12.0 and Got this error:

* What went wrong:
Execution failed for task ':launcher:dexBuilderRelease'.
> Could not resolve all files for configuration ':launcher:detachedConfiguration4'.
   > Failed to transform jetified-play-services-measurement-api-22.0.0-runtime.jar to match attributes {artifactType=ext-dex-dexBuilderRelease, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for DexingExternalLibArtifactTransform: C:\Users\MONSTER\.gradle\caches\transforms-2\files-2.1\152dc3d04a4b917333541e0fc6835cc1\jetified-play-services-measurement-api-22.0.0-runtime.jar.

So now the issue is about Firebase and not Facebook.
I am using :
Firebase Crashlytics,
Firebase Analytics
Firebase Remote Config

Update:
I enabled “multiDexEnabled true” and enabled minify. The build is successfull but game is not working at all :smile: Nothing works the way it should :smile:

1 Like

@LegacySystem Thank you very much for letting us know. Out of curiosity - does the game work as expected
with targetAPI 33 with and without that new aapt2?

Tried custom AAPT2, got the same build errors as @LegacySystem :

Failed to transform xxx.jar to match attributes ...

I tried setting compileSDKversion to 33 and targetSDKversion to 34. When submitting to Play store, we still get target SDK = 33.

Any solution?

Yes. I have no problem when using API 33. I am still sending updates using API 33, firebase 11.9 without aap2 changes. I will try withh aap2 and api 33

Just want to confirm that I’m able to build with custom AAPT2. The error I mentioned earlier is a result of invalid path to aapt2.

Notification has some bug on aapt2 method. We are getting an error when requesting for notification permissions on Android 13 and 14 on the store.

          Fatal Exception: java.lang.Exception: AndroidJavaException : java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.app.FragmentManagerImpl.checkStateLoss(android.app.FragmentManagerImpl)
       at android.app.FragmentManagerImpl.enqueueAction(android.app.FragmentManagerImpl)
       at android.app.BackStackRecord.commitInternal(android.app.BackStackRecord)
       at android.app.BackStackRecord.commit(android.app.BackStackRecord)
       at com.unity3d.player.UnityPermissions.requestUserPermissions(com.unity3d.player.UnityPermissions)
       at com.unity3d.player.UnityPlayer.nativeRender(com.unity3d.player.UnityPlayer)
       at com.unity3d.player.UnityPlayer.access$500(com.unity3d.player.UnityPlayer)
       at com.unity3d.player.UnityPlayer$e$1.handleMessage(com.unity3d.player.UnityPlayer$e$1)
       at android.os.Handler.dispatchMessage(android.os.Handler)
       at android.os.Looper.loopOnce(android.os.Looper)
       at android.os.Looper.loop(android.os.Looper)
       at com.unity3d.player.UnityPlayer$e.run(com.unity3d.player.UnityPlayer$e)
       at UnityEngine.AndroidJNISafe.CheckException(UnityEngine.AndroidJNISafe)
       at UnityEngine.AndroidJavaObject._CallStatic(UnityEngine.AndroidJavaObject)
       at Unity.Notifications.Android.PermissionRequest.RequestPermission(Unity.Notifications.Android.PermissionRequest)
       at Unity.Notifications.Android.PermissionRequest..ctor(Unity.Notifications.Android.PermissionRequest.)
       at NotificationManager+<RequestAndroidNotificationPermission>d__8.MoveNext(NotificationManager+<RequestAndroidNotificationPermission>d__8)
       at UnityEngine.SetupCoroutine.InvokeMoveNext(UnityEngine.SetupCoroutine)
        

I solved this issue by installing Unity 6000.0.11f1. It comes with Gradle Version 8.4 and lets you build for Android 34.

Then I linked all Android dependencies of 6000.0.11f1 inside Unity 2022.3.38f1 (LTS)( released on 16. Juli 2024)
in Preferences → External Tools:

SDK:
C:\Program Files\Unity\Hub\Editor\6000.0.11f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK
NDK:
C:\Program Files\Unity\Hub\Editor\6000.0.11f1\Editor\Data\PlaybackEngines\AndroidPlayer\NDK
Gradle:
C:\Program Files\Unity\Hub\Editor\6000.0.11f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle

Then:
Player Settings->Other Settings-> Target API Level → API Level 34
Player Settings->Publishing Settings:
enable:

  • Custom Main Manifest
  • Custom Main Gradle Template
  • Custom Base Gradle Template
  • Custom Gradle Properties Template

inside file of Custom Gradle Properties Template add:
android.useAndroidX=false
android.enableJetifier=false

Hope it works!

2 Likes