Build Unity for Android
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
-
The Unity Editor is version locked to a specific version of Gradle, and earlier versions are incompatible with the latest Google Mobile Ads.
-
For Unity Editor 2021341f1 or higher, enabling the Gradle build pre-processor automatically applies necessary settings.
-
For Unity Editor versions below 2021341f1, manual configuration of Android and Gradle settings is required, including enabling Custom Gradle Templates and setting the Target API Level to 34 or higher.
-
For Unity Editor versions 2019.4 - 2021337f1, you need to export the project to Android Studio and update Gradle, JDK, and various build and project files.
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.
To build Android, select your preferred Unity Editor version:
2023.1 or higher
Enable Gradle build pre-processor
The Gradle build preprocessor is enabled by default in Unity Editor 2021341f1 and later. The processor automatically applies all necessary Gradle and player settings to support the latest Google Mobile Ads SDK on earlier versions of the Unity Editor.Enable Gradle build preprocessor
Manually configure Android and Gradle settings
Enable Custom Gradle Templates
Go to Project Settings> Player> Android> Publishing Settings> Build
and enable Custom Main Gradle Template
and Custom Gradle Properties Template
.
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 Level2021341f1 - 2022.3
Enable Gradle build pre-processor
The Gradle build preprocessor is enabled by default in Unity Editor 2021341f1 and later. The processor automatically applies all necessary Gradle and player settings to support the latest Google Mobile Ads SDK on earlier versions of the Unity Editor.Enable Gradle build preprocessor
Manually configure Android and Gradle settings
Enable Custom Gradle Templates
Go to Project Settings> Player> Android> Publishing Settings> Build
and enable Custom Main Gradle Template
and Custom Gradle Properties Template
.
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 LevelAdd jetifier ignore list to `gradleTemplate.properties`
Edit Assets/Plugins/Android/gradleTemplate.properties
and add the following line:
android.jetifier.ignorelist=annotation-experimental-1.4.0.aar
Remove `minSDKVersion` from `AndroidManifest.xml`
Edit Assets/Plugins/Android/GoogleMobileAdsPlugin.androidlib/AndroidManifest.xml
and remove android:minSdkVersion="21"
from the uses-sdk
node.
Your AndroidManifest.xml
should look like the following:
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.google.unity.ads" android:versionName="1.0" android:versionCode="1"> <uses-sdk/> <application> <uses-libraryandroid:required="false"android:name="org.apache.http.legacy"/> </application> </manifest>
2019.4 - 2021337f1
Prerequisites
Before continuing, ensure you have the following:
- Download and install the latest stable version of Android Studio.
- Optional: For details on building for Android, see Java versions in Android builds
Enable Custom Gradle Templates
Go to Project Settings> Player> Android> Publishing Settings> Build
and enable Custom Main Gradle Template
and Custom Gradle Properties Template
.
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 LevelExport to Android Studio
Modify the Android build settings by selecting File (or Unity Editor on MacOS)> Build Settings and check Export Project:
Export ProjectIf 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 configurationIf 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'applyfalse id'com.android.library'version'8.1.1'applyfalse } taskclean(type:Delete){ deleterootProject.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 thepackage
attribute fromlauncher/AndroidManifest.xml
- Set
sourceCompatibility
andtargetCompatibility
to Java 17
applyplugin:'com.android.application' dependencies{ implementationproject(':unityLibrary') } android{ namespace"com.google.android.gms.example" compileSdkVersion35 buildToolsVersion'35.0.0' compileOptions{ sourceCompatibilityJavaVersion.VERSION_17 targetCompatibilityJavaVersion.VERSION_17 } defaultConfig{ minSdkVersion28 targetSdkVersion35 applicationId'com.google.android.gms.example' ndk{ abiFilters'armeabi-v7a','arm64-v8a','x86','x86_64' } versionCode1 versionName'1.0' } aaptOptions{ noCompress=['.unity3d','.ress','.resource','.obb','.bundle','.unityexp'] ignoreAssetsPattern="!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~" } lintOptions{ abortOnErrorfalse } buildTypes{ debug{ minifyEnabledfalse proguardFilesgetDefaultProguardFile('proguard-android.txt') signingConfigsigningConfigs.debug jniDebuggabletrue } release{ minifyEnabledfalse proguardFilesgetDefaultProguardFile('proguard-android.txt') signingConfigsigningConfigs.debug } } packagingOptions{ doNotStrip'*/armeabi-v7a/*.so' doNotStrip '*/arm64-v8a/*.so' doNotStrip '*/x86/*.so' doNotStrip '*/x86_64/*.so' jniLibs{ useLegacyPackagingtrue } } bundle{ language{ enableSplit=false } density{ enableSplit=false } abi{ enableSplit=true } } } applyfrom:'../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
andtargetCompatibility
toJavaVersion.VERSION_17
applyplugin:'com.android.library' dependencies{ implementationfileTree(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') implementationproject('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' } } applyfrom:'GoogleMobileAdsPlugin.androidlib/packaging_options.gradle' gradle.projectsEvaluated{applyfrom:'GoogleMobileAdsPlugin.androidlib/validate_dependencies.gradle'}
Update unity/Library/GoogleMobileAdsPlugin.androidlib/build.gradle
Set the namespace
attribute with the value "com.google.unity.ads"
.
applyplugin:'android-library' dependencies{ implementationfileTree(dir:'bin',include:['<em>.jar']) implementationfileTree(dir:'libs',include:['</em>.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'] } } compileSdkVersion34 buildToolsVersion'30.0.2' defaultConfig{ targetSdkVersion31 } lintOptions{ abortOnErrorfalse } }
Run the Android Project
From Android Studio, run gradle sync, and run the project.