Unity打包APK報錯 using a newer Android Gradle plugin to use compileSdk = 35
三個報錯信息如下
第一個
WARNING:We recommend using a newer Android Gradle plugin to use compileSdk = 35This Android Gradle plugin (7.1.2) was tested up to compileSdk = 32This warning can be suppressed by addingandroid.suppressUnsupportedCompileSdk=35
to this project's gradle.propertiesThe build will continue, but you are strongly encouraged to update your project to
use a newer Android Gradle Plugin that has been tested with compileSdk = 35
第二個
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
ERROR:D:\A_Unity\2022.3.26f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms\android-35\android.jar: D8: java.lang.NullPointerExceptionFAILURE: Build failed with an exception.* What went wrong:
Execution failed for task ':launcher:desugarReleaseFileDependencies'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.DexFileDependenciesTask$DexFileDependenciesWorkerAction> Error while dexing.* 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.orgBUILD FAILED in 1s
第三個
stderr[
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
ERROR:D:\A_Unity\2022.3.26f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms\android-35\android.jar: D8: java.lang.NullPointerExceptionFAILURE: Build failed with an exception.* What went wrong:
Execution failed for task ':launcher:desugarReleaseFileDependencies'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.DexFileDependenciesTask$DexFileDependenciesWorkerAction> Error while dexing.* 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.orgBUILD FAILED in 1s
]
stdout[
WARNING:We recommend using a newer Android Gradle plugin to use compileSdk = 35This Android Gradle plugin (7.1.2) was tested up to compileSdk = 32This warning can be suppressed by addingandroid.suppressUnsupportedCompileSdk=35
to this project's gradle.properties
錯誤原因:這個問題報錯的原因是,我使用的 Unity2022.3.26,與之對應的 Android Gradle plugin 也就是 Gradle 版本是 7.1.2,這是Unity 官方給的對應關系
打包 APK 時配置Player Settings-> Android -> OthersSettings -> Identification -> Target API Level -> API Level 35
經測試 API Level 設置為 34/33/32 等低版本都是可以正常打包的
解決方案一
Unity 官方回答:這個問題影響Unity版本2021.3和2022.3 LTS。
目前,由于谷歌構建工具中AAPT2和SDK 35之間的兼容性錯誤,無法在Unity 2021.3和2022.3上構建SDK 35。我們已經向b谷歌報告了這個問題
我們正在Unity內提供修復,但與此同時,請遵循以下解決方案:
在 Unity 項目內 打開 gradleTemplate.properties
,這個文件 生成方式 Player Settings → Publish Settings → Check “Custom Gradle Properties Template”
在 Plugins/Android 文件夾下生成 gradleTemplate.properties
用文本編輯器打開,然后在最后添加一行 android.aapt2FromMavenOverride=<PATH_TO_YOUR_SDK>/build-tools/35.0.0/aapt2.exe
完整路徑以Unity2022.3.26f1 為例安裝路徑 android.aapt2FromMavenOverride=2022.3.26f1/Editor\Data/PlaybackEngines/AndroidPlayer/SDK/build-tools/35.0.0/aapt2.exe
但是這個解決方案,我自己測試是無效的
解決方案二
錯誤原因中已經說了 API Level 設置低于 35 的都是可以正常打包的,那么如果不是必須要求設置 為 35 那么就降低版本,再打包
解決方案三
升級 Unity 版本,經過測試 Unity6 打包 APK 設置 API Level 35 打包正常