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

Android应用集成广告编译报错:Dex文件无法容纳所有类(Play Services相关)

Solution for "Cannot fit requested classes in a single dex file" Error When Integrating Ads

Hey there, let's fix this dex overflow issue you're facing! The root cause here is that you're including the entire Google Play Services bundle (com.google.android.gms:play-services:12.0.1), which is massive—it pulls in every single Google service module, even the ones you don't need. Adding the ads module on top of that pushes you over the 65k method limit for a single dex file.

Here's how to resolve this step by step:

1. Replace the Full Play Services Dependency with Specific Modules

First, remove the full play-services dependency entirely. Instead, only include the specific modules your app actually uses:

  • You mentioned using Android Wear, so you'll need the wearable module
  • Keep the ads module you already added

Update your module-level build.gradle dependencies to this:

// Keep the ads module for your ad integration
implementation 'com.google.android.gms:play-services-ads:12.0.1'
// Replace the full play-services with only the wearable module needed for Android Wear
implementation 'com.google.android.gms:play-services-wearable:12.0.1'

This drastically cuts down the number of methods added to your app, which should resolve the dex overflow issue immediately.

2. Enable Multidex as a Fallback (If Needed)

If after trimming dependencies you still hit the dex limit (unlikely, but possible), enable Multidex to split your code into multiple dex files:

  1. In your module-level build.gradle, add the Multidex dependency and enable multiDex:
    android {
        defaultConfig {
            // ...
            multiDexEnabled true
        }
    }
    
    dependencies {
        // ...
        implementation 'androidx.multidex:multidex:2.0.1' // Use the latest stable version
    }
    
  2. Update your Application class:
    • If you have a custom Application class, make it extend MultiDexApplication:
      public class MyApplication extends MultiDexApplication {
          // Your existing application logic here
      }
      
    • If you can't extend MultiDexApplication, override attachBaseContext:
      public class MyApplication extends Application {
          @Override
          protected void attachBaseContext(Context base) {
              super.attachBaseContext(base);
              MultiDex.install(this);
          }
      }
      
  3. Ensure your AndroidManifest.xml references your custom Application class (if applicable):
    <application
        android:name=".MyApplication"
        // Rest of your application configuration
    >
    </application>
    

3. Sync and Rebuild

After updating your dependencies and configuration, sync your Gradle project and rebuild your app. This should eliminate the "Cannot fit requested classes in a single dex file" error.

内容的提问来源于stack exchange,提问作者Lim Thye Chean

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:31:00