Google is committed to advancing racial equity for Black communities. See how.

Build for Android 12 with Unity

When building an Android project that targets Android 12 (SDK level 31), you may encounter the following error:

Could not determine the dependencies of task ':launcher:compileDebugJavaWithJavac'.
> Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.
> Configure project :launcher
WARNING: The option 'android.enableR8' is deprecated and should not be used anymore.
It will be removed in a future version of the Android Gradle plugin, and will no longer allow you to disable R8.
Build-tool 31.0.0 is missing DX at <android-sdk-path>/sdk/build-tools/31.0.0/dx
File ~/.android/repositories.cfg could not be loaded.
Build-tool 31.0.0 is missing DX at <android-sdk-path>/sdk/build-tools/31.0.0/dx

This is caused by an incompatibility between Android Build Tools version 31.0.0 and the Unity build system. As of Android Build Tools version 31.0.0, DX has been removed in favor of D8, leading to breakage in Unity builds for Android.

This error may be triggered in the following scenarios:

  • Upgrading to ARCore Extensions for Unity's AR Foundation version 1.26,
  • Targeting Android SDK level 31 in any Unity project, regardless of ARCore Extensions version,
  • Targeting Android SDK level 30 in any Unity project while Build Tools version 31.0.0 is installed, regardless of ARCore Extensions version.

Workaround

We are working with Unity to resolve this incompatibility. In the meantime, follow instructions to build a project that targets Android 12:

  1. In Project Settings > Player > Android tab > Publishing Settings > Build, select both:

    1. Custom Main Gradle Template,
    2. Custom Launcher Gradle Template.

    A screenshot showing the Publishing Settings, Build pane with both Gradle
Template options selected

  2. Apply the following changes to both generated files:

    • Assets/Plugins/Android/mainTemplate.gradle
    • Assets/Plugins/Android/launcherTemplate.gradle

    If present, remove the following comment at the top of the file:

    // GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
    

    Then modify compileSdkVersion and buildToolsVersion as follows:

    buildToolsVersion '30.0.3'
    

When building, Unity will download Build-Tools version 30.0.3 and use it to build your project while maintaining the selected targetSdkVersion.

Create custom Gradle builds

On Unity versions 2019.4, 2020.1, and 2020.2, which are built with older versions, you will need to set a custom Gradle build to Gradle version 6.1.1 or later. You will also need Android Gradle Plugin 4.0.1 or later.

Apps targeting SDK 31 require Gradle Version 6.1.1 or later.

  1. Go to Preferences > External Tools > Android > Gradle, and set the custom Gradle build to Gradle 6.1.1 or later. See Gradle build tool for downloads.
  2. Apply the following changes to both generated files:

    • Assets/Plugins/Android/mainTemplate.gradle
    • Assets/Plugins/Android/launcherTemplate.gradle
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        // Must be Android Gradle Plugin 4.0.1 or later. For a list of
        // compatible Gradle versions refer to:
        // https://developer.android.com/studio/releases/gradle-plugin
        classpath 'com.android.tools.build:gradle:4.0.1'
    }
}

allprojects {
   repositories {
      google()
      jcenter()
      flatDir {
        dirs 'libs'
      }
   }
}

Apply changes for apps targeting Android 12

If your app targets Android 12, then you must explicitly declare the android:exported attribute. For all changes in Android 12, see Behavior changes in Android 12.

  1. In Project Settings > Player > Android tab > Publishing Settings > Build, select Custom Main Manifest.

  2. Apply the following changes to Assets/Plugins/Android/AndroidManifest.xml:

    1. If present, remove the following comment at the top of the file:

      <!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
      
    2. Add the android:exported attribute to the <activity> tag:

       <application>
           <activity android:name="com.unity3d.player.UnityPlayerActivity"
                     android:theme="@style/UnityThemeSelector"
                     android:exported="true">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
               <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
           </activity>
       </application>