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

构建即时应用项目遇Gradle错误:特征模块重复打包依赖库

Fixing Duplicate Dependency Errors in Android Instant App Feature Modules

Hey there, let's tackle this duplicate dependency issue you're hitting with your Instant App. This is a common gotcha with Instant App's modular structure, so let's break down why it's happening and how to fix it.

Why This Error Popped Up

Android Instant Apps enforce strict modular rules to keep the app lightweight and properly segmented. When multiple feature modules (:feature1, :feature2) include the exact same dependencies (like com.squareup.retrofit2:adapter-rxjava or :reviews_lib), Gradle throws this error because it can't safely package duplicate libraries across features—this would bloat the app and cause runtime conflicts.

You mentioned the project worked before? Here are a few likely reasons it started failing now:

  • AGP Version Update: Newer versions of the Android Gradle Plugin (AGP) have stricter validation for Instant App dependencies, catching issues that older versions let slide.
  • Accidental Dependency Changes: Maybe you recently added those dependencies directly to both feature modules (instead of inheriting them from the base), or removed them from the base module by mistake.
  • Transitive Dependency Shifts: A third-party library you depend on might have updated, changing which dependencies are pulled in transitively and creating duplicates.

Step-by-Step Fix

1. Move Shared Dependencies to the Base Module

All dependencies used by multiple feature modules must live in your base feature module. This ensures they're only packaged once and shared across all features.

Open your base module's build.gradle file and add the duplicate dependencies using the api configuration (so feature modules can inherit them):

dependencies {
    // Replace with your actual dependency versions
    api 'com.squareup.retrofit2:adapter-rxjava:2.9.0'
    api project(':reviews_lib')
    
    // Keep your other base dependencies here
}

2. Remove Duplicate Dependencies from Feature Modules

Go to :feature1 and :feature2's build.gradle files, and delete the lines where you declared those duplicate dependencies. Your feature modules will automatically inherit them from the base module as long as they have a dependency on the base:

dependencies {
    implementation project(':base')
    // Remove lines like 'implementation com.squareup.retrofit2:adapter-rxjava:...' or 'implementation project(:reviews_lib)'
}

3. Clean and Rebuild the Project

Old Gradle cache can sometimes cause lingering issues, so run these commands to clear it and rebuild:

./gradlew clean
./gradlew assembleDebug

Double-Check Your Base Module Configuration

Make sure your base module is properly configured as the Instant App's base feature. In older AGP versions, this means applying the com.android.feature plugin and setting baseFeature = true:

apply plugin: 'com.android.feature'

android {
    baseFeature true
    // Rest of your Android configuration (compileSdk, defaultConfig, etc.)
}

For newer AGP versions (using com.android.application), ensure your base module is set up as the application module that includes feature modules, but the core rule still applies: shared dependencies go in the base.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:29:59