The Unity Editor is version locked to a specific version of Gradle which can conflict with dependencies required by newer Android packages, such as Google Mobile Ads. The following guide instructs you how to produce an Android build on earlier versions of the Unity Editor.
The following table recommends the maximum compatible Google Mobile Ads plugin version to use based on your Unity Editor.
Unity Editor | Recommended Google Mobile Ads Unity plugin version |
---|---|
2023.1 or higher | Latest |
2021.3.41f1 - 2022.3 | 9.1.0 |
2021.3.37f1 or earlier | 8.5.3 |
Prerequisites
- Download and install the latest stable version of Android Studio.
- To better understand building for Android, read Java versions in Android builds.
Update 2022.3 and earlier build settings
Unity 2022.3 and earlier requires build setting changes prior to exporting to Android Studio.
Generate the baseProjectTemplate.gradle
Enable custom Gradle templates. Go to Project Settings > Player > Android > Publishing Settings > Build, and select: Custom Base Gradle Template. This generates the
Plugins/Android/baseProjectTemplate.gradle
file.Modify
Plugins/Android/baseProjectTemplate.gradle
file to use Gradle tools version 8.1.1 or higher.plugins { id 'com.android.application' version '8.1.1' apply false id 'com.android.library' version '8.1.1' apply false } task clean(type: Delete) { delete rootProject.buildDir }
Additional changes for Unity 2021.3.37f1 and earlier
Unity 2021.3.37f1 and earlier requires additional changes to upgrade compatibility to Gradle 8.1.1. Complete the following steps:
Generate launcherTemplate.gradle
Go to Project Settings > Player > Android > Publishing Settings > Build, and select Launcher Gradle Template. This generates the
Plugins/Android/launcherTemplate.gradle
file.Modify
Plugins/Android/launcherTemplate.gradle
to include the namespace attribute. The namespace attribute value should match the package attribute found in the launcher module'sAndroidManifest.xml
file.android { // TODO: Replace with your app's package name. namespace 'com.google.android.gms.example' }
Generate mainTemplate.gradle
- Go to Project Settings > Player > Android > Publishing Settings > Build,
and select: Main Gradle Template. This generates the
Plugins/Android/mainTemplate.gradle
file.
.
Modify
Plugins/Android/mainTemplate.gradle
to include the namespace attribute with valuecom.unity3d.player
.android { namespace 'com.unity3d.player' }
Export to Android Studio
Modify the Android build settings to export the Unity project.
To export to Android Studio, modify the Android build settings. Select File (or Unity Editor on macOS) > Build Settings and check Export Project:
Press the Export button at the bottom of the Build Settings dialog. Unity generates an Android project at the selected location.
Open the exported project in Android Studio.
Update Gradle JDK configuration to use JDK 17
- Using Android Studio, open the Gradle settings from File (or Android Studio on macOS) > Settings > Build, Execution, Deployment > Build Tools > Gradle.
Locate The Gradle JDK drop-down. Modify the Gradle JDK to use JDK 17.
If you don't have JDK 17 installed, select the Download JDK option from the Gradle JDK drop-down menu and download a compatible version. We recommend the JetBrains runtime vendor with
arch64
support, to match what Android Studio distributes.
Update Gradle wrapper files
Modify the distributionUrl parameter inside gradle/wrapper/gradle-wrapper.properties
to use Gradle 8.0.1 or higher.
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip
Additional Android Studio changes for Unity 2021.3.37f1 and earlier
Unity 2021.3.37f1 and earlier requires additional changes to upgrade compatibility to Gradle 8.1.1. Complete the following steps:
Modify the
gradle.properties
and remove theandroid.enableR8=false
attribute.Modify
unityLibrary/GoogleMobileAdsPlugin.androidlib/build.gradle
and add a namespace attribute with value"com.google.unity.ads"
:apply plugin: 'android-library' dependencies { implementation fileTree(dir: 'bin', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar']) } android { namespace "com.google.unity.ads" sourceSets { main { manifest.srcFile 'AndroidManifest.xml' //java.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } compileSdkVersion 35 buildToolsVersion '30.0.3' defaultConfig { targetSdkVersion 31 } lintOptions { abortOnError false } }
Run the Android Project
After completing these steps the Unity application is ready. From Android Studio, run gradle sync, and run the project.