使用Heyzap Mediation集成第三方SDK时遇Gradle构建失败求助
This is a super common dependency conflict issue—here's how to fix it step by step:
Root Cause
The error Program type already present: android.support.v4.app.INotificationSideChannel happens because Heyzap Mediation already includes a version of the Android Support Library, and the third-party SDK you're adding is pulling in a different (or duplicate) version of the same library. Gradle can't merge these duplicate classes into your Dex archive, hence the failure.
Solutions
1. Force a Unified Support Library Version
This is the quickest fix for most cases:
- Open your Unity project, go to Player Settings > Publishing Settings, check the box for Custom Gradle Template (this generates
Assets/Plugins/Android/mainTemplate.gradle). - Open the generated
mainTemplate.gradlefile, and add this configuration inside theandroidblock (or right before thedependenciesblock):
Unity 2018.3 uses Support Library 28.0.0 by default, so this version should be compatible with Heyzap and most third-party SDKs.configurations.all { // Force the same version of support-v4 across all dependencies resolutionStrategy.force 'com.android.support:support-v4:28.0.0' // You can add other support libraries here if needed, e.g., appcompat-v7 resolutionStrategy.force 'com.android.support:appcompat-v7:28.0.0' }
2. Exclude Duplicate Dependencies from Specific SDKs
If you know exactly which third-party SDK is causing the conflict, you can explicitly exclude the conflicting support library from its dependency:
- In the
dependenciesblock ofmainTemplate.gradle, modify the SDK's implementation line to exclude the support-v4 module. For example, if it's AdMob:
Use this if the forced version approach causes issues with the third-party SDK.implementation ('com.google.android.gms:play-services-ads:18.3.0') { exclude group: 'com.android.support', module: 'support-v4' }
3. Enable Multidex (If Needed)
Sometimes the conflict is compounded by having too many methods in your build. Enable Multidex to resolve this:
- In Player Settings > Other Settings, set Minimum API Level to 21 or higher (Android 5.0+).
- If you need to support lower API levels, add this to
mainTemplate.gradle:android { defaultConfig { multiDexEnabled true } } dependencies { implementation 'com.android.support:multidex:1.0.3' } - If you have a custom
Applicationclass, make it extendMultiDexApplication; otherwise, addMultiDex.install(this)to theonCreatemethod of your default application.
4. Clean Build Cache
Old cache files can cause unexpected issues, so do a full clean before rebuilding:
- Close Unity.
- Delete the
Library,Temp, and AndroidBuildfolders from your project directory. - Reopen Unity and try building again.
Additional Notes
- Make sure you're using the latest compatible version of Heyzap Mediation—older versions might have outdated dependency references.
- Check the documentation of your third-party SDK to confirm their recommended Support Library version, and align it with Unity's default if possible.
内容的提问来源于stack exchange,提问作者Muhammad Ahmad Saleem

