Closed5
[Unity]Androidビルド時にgradleのバージョンを変更したらエラー
広告SDKを最新にした→
Android11に対応したSDKになった→
最新のSDKだとgradle3.4.0だと<query>タグが解釈できずエラーが起きてしまう
のような流れだったのでmainTemplate.gradleでgradleのバージョンを以下のように指定
classpath 'com.android.tools.build:gradle:3.4.3'
エラーログは↓の内容
A problem occurred evaluating project ':unityLibrary'.
> Failed to apply plugin [id 'com.android.library']
   > Using multiple versions of the Android Gradle plugin in the same build is not allowed.
     	'/Users/**Unityプロジェクトルート**/Temp/gradleOut/unityLibrary' is using version 3.4.3
     	'/Users/**Unityプロジェクトルート**/Temp/gradleOut/launcher' is using version 3.4.0
プロジェクト側で指定してるgradleバージョンと、Unity側のデフォルトのgradleバージョンが食い違ってしまっているので、Unity側の方もgradle3.4.3を使うように修正する必要がある。
Unity側のgradleバージョンは下記ファイルで指定されている。
使用しているUnityのバージョンは適宜置き換えていただいて。
/Applications/Unity/Hub/Editor/2019.4.10f1/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/baseProjectTemplate.gradle
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
allprojects {
    buildscript {
        repositories {**ARTIFACTORYREPOSITORY**
            google()
            jcenter()
        }
        dependencies {
            // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
            // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
            // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
            // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
            classpath 'com.android.tools.build:gradle:3.4.0'
            **BUILD_SCRIPT_DEPS**
        }
    }
    repositories {**ARTIFACTORYREPOSITORY**
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}
 classpath 'com.android.tools.build:gradle:3.4.0'
の行を3.4.3に変更する。
※ここでは直接書き換えているが、↑に
// To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
とあるように、Unity側で使用するGradleのパスを指定することもできる
無事ビルドできた✅
(Unityプロジェクトルート)/Temp/gradleOut/launcher/build/intermediates/merged_manifests/debug/AndroidManifest.xml:48: 
AAPT: error: unexpected element <queries> found in <manifest>.
元のエラーメモ
このスクラップは2021/02/17にクローズされました