Unity Editor Android Compatibility

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

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

  1. 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.

  2. 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

  1. Go to Project Settings > Player > Android > Publishing Settings > Build, and select Launcher Gradle Template. This generates the Plugins/Android/launcherTemplate.gradle file.

  2. Modify Plugins/Android/launcherTemplate.gradle to include the namespace attribute. The namespace attribute value should match the package attribute found in the launcher module's AndroidManifest.xml file.

    android {
        // TODO: Replace with your app's package name.
        namespace 'com.google.android.gms.example'
    }
    

Generate mainTemplate.gradle

  1. Go to Project Settings > Player > Android > Publishing Settings > Build, and select: Main Gradle Template. This generates the Plugins/Android/mainTemplate.gradle file.

.

  1. Modify Plugins/Android/mainTemplate.gradle to include the namespace attribute with value com.unity3d.player.

    android {
        namespace 'com.unity3d.player'
    }
    

Export to Android Studio

  1. Modify the Android build settings to export the Unity project.

  2. To export to Android Studio, modify the Android build settings. Select File (or Unity Editor on macOS) > Build Settings and check Export Project:

  3. Press the Export button at the bottom of the Build Settings dialog. Unity generates an Android project at the selected location.

  4. Open the exported project in Android Studio.

Update Gradle JDK configuration to use JDK 17

  1. Using Android Studio, open the Gradle settings from File (or Android Studio on macOS) > Settings > Build, Execution, Deployment > Build Tools > Gradle.
  2. 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:

  1. Modify the gradle.properties and remove the android.enableR8=false attribute.

  2. 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.