You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2019.4.0启用MultiDex后仍报Dex方法数超限求助

Fixing "Cannot fit requested classes in a single dex file" in Unity 2019.4.0

Hey there, let's work through this MultiDex issue you're stuck on. That 65k method limit error is super common when your project's dependencies pile up, and it sounds like your custom Gradle config isn't kicking in properly. Here are step-by-step fixes to get your build back on track:

1. Verify Your Custom Gradle Template Is Actually Active

  • First, double-check the Unity Player Settings: Go to Edit > Project Settings > Player > Android > Publishing Settings and make sure the Custom Gradle Template checkbox is definitely checked. It’s easy to accidentally uncheck this, which would make all your template edits irrelevant.
  • Confirm you’re modifying the right file: The correct template lives at Assets/Plugins/Android/mainTemplate.gradle in your project directory. Don’t waste time editing a template file outside this path.

2. Switch to the AndroidX MultiDex Dependency

Since you’ve enabled Jetifier, using the old support library version of MultiDex can cause compatibility issues. Update your dependencies block in mainTemplate.gradle to use the AndroidX variant:

dependencies {
    // Keep your other dependencies here
    implementation 'androidx.multidex:multidex:2.0.1'
}

This ensures better compatibility with AndroidX, which Jetifier is already handling for your project.

3. Ensure multiDexEnabled true Is in the Right Place

Make sure the multiDexEnabled flag is properly nested inside the defaultConfig block in your mainTemplate.gradle:

android {
    defaultConfig {
        // Your existing config (minSdkVersion, targetSdkVersion, etc.)
        minSdkVersion 19
        targetSdkVersion 28
        multiDexEnabled true // This line needs to be here
    }
    // Rest of your android block content
}

If this line is placed outside defaultConfig or in the wrong nested block, Gradle will ignore it entirely.

4. Fix Your AndroidManifest Application Setup

  • If you’re using a custom Application class (not relying on the default MultiDexApplication), you need to explicitly initialize MultiDex in that class:
    import android.content.Context;
    import androidx.multidex.MultiDex;
    import android.app.Application;
    
    public class MyCustomApp extends Application {
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
    
    Then update your AndroidManifest to point to this custom class:
    <application
        android:name=".MyCustomApp"
        <!-- Rest of your application attributes -->
    >
    
  • If you don’t have a custom Application class, double-check that android:name="androidx.multidex.MultiDexApplication" is correctly added to the <application> tag—no typos, no missing characters.

5. Clear Caches to Eliminate Stale Configs

Cached files often cause old, incorrect settings to stick around. Try these steps:

  • In Unity, go to File > Build Settings > Android and click Clean Build
  • Delete your project’s Temp and Library folders, then restart Unity to reimport all resources
  • Remove the Assets/Plugins/Android/gradle folder (if it exists) to let Unity re-download fresh Gradle dependencies

6. Check External Dependency Manager Settings

Ensure the External Dependency Manager (formerly Play Services Resolver) is set up to patch your Gradle template:

  • Go to Window > Package Manager > External Dependency Manager > Android Resolver > Settings
  • Make sure the Patch mainTemplate.gradle option is enabled. This ensures the dependency manager correctly integrates with your custom Gradle setup.

If you’ve tried all these steps and still see the error, try enabling Force Gradle Sync in Unity’s Android build settings. Also, double-check that your minimum API level is indeed set to 19 (MultiDex requires API 19+ to function without extra workarounds).

内容的提问来源于stack exchange,提问作者Jawad Amjad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:42:39