Build Unity for Android

The Unity Editor is version locked to a specific version of Gradle. Earlier versions of the Unity Editor use earlier versions of Gradle which are incompatible with the latest version of Google Mobile Ads.

The following table shows the maximum compatible Google Mobile Ads plugin version to use based on your Unity Editor:

Unity Editor 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

You can manually update Gradle to use the latest Google Mobile Ads Unity plugin, including older Unity Editors. To build Android, select your preferred Unity Editor version:

2023.1 or higher

Prerequisites

Before continuing, ensure you have the following:

Enable Custom Gradle Templates

Go to Project Settings > Player > Android > Publishing Settings > Build and enable Custom Main Gradle Template and Custom Gradle Properties Template.

Enable Custom Gradle Templates

Set Target API Level 34

From the main menu open Edit > Project Settings > Player > Android > Other Settings and set the Target API Level to API Level 34 or higher.

Set Target API Level

Run the Android Project

From Android Studio, run gradle sync, and run the project.

2021.3.4f1 - 2022.3

Prerequisites

Before continuing, ensure you have the following:

Enable Custom Gradle Templates

Go to Project Settings > Player > Android > Publishing Settings > Build and enable Custom Main Gradle Template and Custom Gradle Properties Template.

Enable Custom Gradle Templates

Set Target API Level 34

From the main menu open Edit > Project Settings > Player > Android > Other Settings and set the Target API Level to API Level 34 or higher.

Set Target API Level

Export to Android Studio

Modify the Android build settings by selecting File (or Unity Editor on MacOS) > Build Settings and check Export Project:

Export Project

Open Android Studio

This section contains steps performed within Android Studio.

Update Gradle JDK configuration

Open the Gradle settings from File (or Android Studio on MacOS) > Settings > Build > Execution > Deployment > Build Tools > Gradle. Locate the Gradle JDK drop-down and set the Gradle JDK to use JDK 17 or later.

Update Gradle JDK configuration

If you don't have JDK 17 installed, select the Download JDK options from the Gradle JDK menu bar and download a compatible version. We recommend the JetBrains runtime vendor with aarch64 support, to match what Android Studio distributes.

Update the project-level build.gradle

Set Gradle tools version to 8.1.1 or newer.


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
}

Update /gradle/gradle-wrapper.properties

Set distributionUrl to use Gradle 8.1.1 or newer.

distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip

Run the Android Project

From Android Studio, run gradle sync, and run the project.

2021.3.37f1 - 2019.4

Prerequisites

Before continuing, ensure you have the following:

Enable Custom Gradle Templates

Go to Project Settings > Player > Android > Publishing Settings > Build and enable Custom Main Gradle Template and Custom Gradle Properties Template.

Enable Custom Gradle Templates

Set Target API Level 34

From the main menu open Edit > Project Settings > Player > Android > Other Settings and set the Target API Level to API Level 34 or higher.

Set Target API Level

Export to Android Studio

Modify the Android build settings by selecting File (or Unity Editor on MacOS) > Build Settings and check Export Project:

Export Project

If you receive a warning that Android SDK platform API level 34 is missing, select the 'Update Android SDK' option.

Open Android Studio

This section contains steps performed within Android Studio.

Update Gradle JDK configuration

Open the Gradle settings from File (or Android Studio on MacOS) > Settings > Build > Execution > Deployment > Build Tools > Gradle. Locate the Gradle JDK drop-down and set the Gradle JDK to use JDK 17 or later.

Update Gradle JDK configuration

If you don't have JDK 17 installed, select the Download JDK options from the Gradle JDK menu bar and download a compatible version. We recommend the JetBrains runtime vendor with aarch64 support, to match what Android Studio distributes.

Update the project-level build.gradle

Set Gradle tools version to 8.1.1 or newer.


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
}

Update /gradle/gradle-wrapper.properties

Set distributionUrl to use Gradle 8.1.1 or newer.

distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip

Update launcher/build.gradle

  • Set the namespace attribute using the value of the package attribute from launcher/AndroidManifest.xml
  • Set sourceCompatibility and targetCompatibility to Java 17

apply plugin: 'com.android.application'

dependencies {
    implementation project(':unityLibrary')
}

android {
    namespace "com.google.android.gms.example"
    compileSdkVersion 35
    buildToolsVersion '35.0.0'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    defaultConfig {
        minSdkVersion 28
        targetSdkVersion 35
        applicationId 'com.google.android.gms.example'
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }
        versionCode 1
        versionName '1.0'
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', '.bundle', '.unityexp']
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

    lintOptions {
        abortOnError false
    }

    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            signingConfig signingConfigs.debug
            jniDebuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            signingConfig signingConfigs.debug
        }
    }

    packagingOptions {
        doNotStrip '*/armeabi-v7a/*.so'
        doNotStrip '*/arm64-v8a/*.so'
        doNotStrip '*/x86/*.so'
        doNotStrip '*/x86_64/*.so'
        jniLibs {
            useLegacyPackaging true
        }
    }

    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}

apply from: '../unityLibrary/GoogleMobileAdsPlugin.androidlib/packaging_options.gradle'

Update project-level settings.gradle

Set pluginManagement and dependencyResolutionManagement sections.


pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

include ':launcher', ':unityLibrary'
include 'unityLibrary:GoogleMobileAdsPlugin.androidlib'

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {

        google()
        mavenCentral()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

Update unityLibrary/build.gradle

  • Set namespace with the value "com.unity3d.player"
  • Set sourceCompatibility and targetCompatibility to JavaVersion.VERSION_17

    apply plugin: 'com.android.library'

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        // Android Resolver Dependencies Start
        implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
        implementation 'com.google.android.gms:play-services-ads:23.6.0'
        implementation 'com.google.android.ump:user-messaging-platform:3.1.0'
        // Android Resolver Dependencies End
        implementation(name: 'googlemobileads-unity', ext:'aar')
        implementation project('GoogleMobileAdsPlugin.androidlib')
    }

    // Android Resolver Exclusions Start
    android {
      packagingOptions {
          exclude ('/lib/armeabi/*' + '*')
          exclude ('/lib/mips/*' + '*')
          exclude ('/lib/mips64/*' + '*')
          exclude ('/lib/x86/*' + '*')
      }
    }
    // Android Resolver Exclusions End

    android {
        namespace "com.unity3d.player"
        compileSdkVersion 34
        buildToolsVersion '30.0.2'

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_17
            targetCompatibility JavaVersion.VERSION_17
        }

        defaultConfig {
            minSdkVersion 28
            targetSdkVersion 34
            ndk {
                abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
            }
            versionCode 1
            versionName '1.0'
            consumerProguardFiles 'proguard-unity.txt'
        }

        lintOptions {
            abortOnError false
        }

        aaptOptions {
            ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
        }

        packagingOptions {
            doNotStrip '*/armeabi-v7a/*.so'
            doNotStrip '*/arm64-v8a/*.so'
            doNotStrip '*/x86_64/*.so'
        }
    }


    apply from: 'GoogleMobileAdsPlugin.androidlib/packaging_options.gradle'
    gradle.projectsEvaluated { apply from: 'GoogleMobileAdsPlugin.androidlib/validate_dependencies.gradle' }

Update unity/Library/GoogleMobileAdsPlugin.androidlib/build.gradle

Set the namespace attribute with the 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 34
    buildToolsVersion '30.0.2'
    defaultConfig {
        targetSdkVersion 31
    }

    lintOptions {
        abortOnError false
    }
}

Run the Android Project

From Android Studio, run gradle sync, and run the project.