Admob導入後Androidビルドエラーが発生した際の対処法

初めてAdmobをプロジェクトに導入したところ、androidビルドが通らなくなってしまった。
無事解決できたので、対処法を記載します。
f:id:snoopopo:20210828131459p:plain

エラー内容、困ったこと等

コンソールにはいくつかエラーが出てたが、原因特定に至る内容が書いてあったエラーのみ記載します。
(長いので目次から解決方法へ飛んでください)

CommandInvokationFailure: Gradle build failed. 
C:/Program Files/Unity/Hub/Editor/2019.4.23f1/Editor/Data/PlaybackEngines/AndroidPlayer\OpenJDK\bin\java.exe -classpath "C:\Program Files\Unity\Hub\Editor\2019.4.23f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':launcher:processReleaseResources'.
> Android resource linking failed
  C:\_game\week4\Temp\gradleOut\launcher\build\intermediates\merged_manifests\release\AndroidManifest.xml:41: AAPT: error: unexpected element <queries> found in <manifest>.
      

* 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

BUILD FAILED in 6s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
]
stdout[

> Configure project :launcher
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.4.23f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\build-tools\28.0.3\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.4.23f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platform-tools\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.4.23f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms\android-29\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.4.23f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\tools\package.xml. Probably the SDK is read-only

> Task :launcher:preBuild UP-TO-DATE
> Task :unityLibrary:preBuild UP-TO-DATE
> Task :unityLibrary:preReleaseBuild UP-TO-DATE
> Task :unityLibrary:checkReleaseManifest UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:preBuild UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:preReleaseBuild UP-TO-DATE
> Task :launcher:prepareLintJar UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:checkReleaseManifest
> Task :unityLibrary:processReleaseManifest
> Task :unityLibrary:packageReleaseRenderscript NO-SOURCE
> Task :unityLibrary:generateReleaseBuildConfig UP-TO-DATE
> Task :unityLibrary:generateReleaseResValues UP-TO-DATE
> Task :unityLibrary:prepareLintJar UP-TO-DATE
> Task :unityLibrary:prepareLintJarForPublish UP-TO-DATE
> Task :unityLibrary:mergeReleaseShaders UP-TO-DATE
> Task :unityLibrary:compileReleaseShaders UP-TO-DATE
> Task :unityLibrary:generateReleaseAssets UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:processReleaseManifest
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:compileReleaseAidl NO-SOURCE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:packageReleaseRenderscript NO-SOURCE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:compileReleaseRenderscript NO-SOURCE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:generateReleaseResValues
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:generateReleaseResources
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:packageReleaseResources
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:generateReleaseRFile
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:generateReleaseBuildConfig
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:prepareLintJar UP-TO-DATE
> Task :unityLibrary:packageReleaseAssets
> Task :unityLibrary:compileReleaseAidl NO-SOURCE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:generateReleaseSources
> Task :unityLibrary:compileReleaseRenderscript NO-SOURCE
> Task :unityLibrary:generateReleaseResources UP-TO-DATE
> Task :unityLibrary:packageReleaseResources UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:javaPreCompileRelease
> Task :unityLibrary:generateReleaseRFile UP-TO-DATE
> Task :unityLibrary:generateReleaseSources UP-TO-DATE
> Task :unityLibrary:processReleaseJavaRes NO-SOURCE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:compileReleaseJavaWithJavac
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:bundleLibCompileRelease
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:prepareLintJarForPublish UP-TO-DATE
> Task :unityLibrary:mergeReleaseJniLibFolders UP-TO-DATE
> Task :launcher:preReleaseBuild
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:mergeReleaseShaders
> Task :launcher:compileReleaseAidl NO-SOURCE
> Task :launcher:compileReleaseRenderscript NO-SOURCE
> Task :launcher:checkReleaseManifest UP-TO-DATE
> Task :launcher:generateReleaseBuildConfig UP-TO-DATE
> Task :launcher:generateReleaseSources UP-TO-DATE
> Task :launcher:mainApkListPersistenceRelease UP-TO-DATE
> Task :launcher:generateReleaseResValues UP-TO-DATE
> Task :launcher:generateReleaseResources UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:compileReleaseShaders
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:generateReleaseAssets
> Task :unityLibrary:javaPreCompileRelease UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:packageReleaseAssets
> Task :unityLibrary:compileReleaseJavaWithJavac UP-TO-DATE
> Task :unityLibrary:bundleLibCompileRelease UP-TO-DATE
> Task :unityLibrary:bundleLibRuntimeRelease UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:bundleLibRuntimeRelease
> Task :unityLibrary:bundleLibResRelease UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:processReleaseJavaRes NO-SOURCE
> Task :unityLibrary:transformNativeLibsWithMergeJniLibsForRelease UP-TO-DATE
> Task :unityLibrary:transformNativeLibsWithStripDebugSymbolForRelease UP-TO-DATE
> Task :unityLibrary:transformNativeLibsWithIntermediateJniLibsForRelease UP-TO-DATE
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:bundleLibResRelease
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:mergeReleaseJniLibFolders
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:transformNativeLibsWithMergeJniLibsForRelease
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:transformNativeLibsWithStripDebugSymbolForRelease
> Task :unityLibrary:GoogleMobileAdsPlugin.androidlib:transformNativeLibsWithIntermediateJniLibsForRelease
> Task :launcher:mergeReleaseResources UP-TO-DATE
> Task :launcher:javaPreCompileRelease UP-TO-DATE
> Task :launcher:createReleaseCompatibleScreenManifests UP-TO-DATE
> Task :launcher:processReleaseManifest
> Task :launcher:processReleaseResources FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
51 actionable tasks: 23 executed, 28 up-to-date
]
exit code: 1
UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.Command.Run (System.String command, System.String args, System.String workingdir, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
Rethrow as GradleInvokationException: Gradle build failed
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown.
UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message, System.Exception ex) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (UnityEditor.BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at <d76da2c9a45b4b02b93f56ae560b1d44>:0)
UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <fc4e84dee5004858ad88e6706bb25889>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

この事象が発生したときの環境

・Unit 2019.4.23f1 (Windows)
・GoogleMobileAds-v6.1.0

解決方法

エラー文が長すぎるので、大事なところだけを抜粋します↓

C:\_game\week4\Temp\gradleOut\launcher\build\intermediates\merged_manifests\release\AndroidManifest.xml:41: AAPT: error: unexpected element <queries> found in <manifest>.

ビルド時にUnityが自動生成されるAndroidManifest.xml 内の記述、<queries>が使えない、というエラーが出ている。

また、使っているGradle(androidのビルドツール)と要求されているgradleのバージョンの互換性がない、
とのエラーが出ているので、gradleのバージョンまわりを確認した。

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.

Unityで使われるgradleのバージョンを確認

プロジェクト内にbaseProjectTemplate.gradle というファイルが既にある人はそちらを参照。
ない人は、

PlayerSettings -> Publishing Settings -> Custom Base Gradle Template 

にチェックをつけると、このファイルが生成される。
f:id:snoopopo:20210828122831p:plain

Android Gradleプラグインのバージョンを変える

このbaseProjectTemplate.gradleファイル内の以下の部分を3.4.3に変更

classpath 'com.android.tools.build:gradle:3.4.0'

classpath 'com.android.tools.build:gradle:3.4.3'

補足

ちなみになんで、3.4.3なのかという話。
まずここの指定バージョンは、Gradleのバージョンではなく、Android Gradleプラグインのバージョン。

developer.android.com

そして元の原因だった<queries>タグの件については、以下のリリースノートで、 developer.android.com
使用中のバージョンが、3.4系だった場合は、3.4.3にするようにとの記述がある。
f:id:snoopopo:20210828130859p:plain

これで無事、ビルドが通るようになった!

参考にさせて頂いたサイト

zenn.dev