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

Android多模块构建错误:无法解析:payment模块依赖

解决Android多模块构建中debugReverseMetadataValues依赖解析错误

问题现象

执行gradle clean build命令构建多模块应用时,出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:checkDebugLibraries'.
> Could not resolve all dependencies for configuration ':app:debugReverseMetadataValues'.
   > Could not resolve project :payment.
     Required by:
         project :app
      > No matching variant of project :payment was found. The consumer was configured to find a component for use during 'android-reverse-meta-data', as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.8.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' but:
          - Variant 'debugApiElements' declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.8.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug':
              - Incompatible because this component declares a component for use during compile-time and the consumer needed a component for use during 'android-reverse-meta-data'        
          - Variant 'debugRuntimeElements' declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.8.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug':
              - Incompatible because this component declares a component for use during runtime and the consumer needed a component for use during 'android-reverse-meta-data'
          - Variant 'releaseApiElements' declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.8.1':
              - Incompatible because this component declares a component for use during compile-time, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a component for use during 'android-reverse-meta-data', as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug'
          - Variant 'releaseRuntimeElements' declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.8.1':
              - Incompatible because this component declares a component for use during runtime, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a component for use during 'android-reverse-meta-data', as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug'

项目结构

|--app
|--payment

当前模块配置

build.gradle.kts(:payment)

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
}

android {
    namespace = "com.stream.payment"
    compileSdk = 35

    defaultConfig {
        minSdk = 28
        targetSdk = 35

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }
    kotlinOptions {
        jvmTarget = "21"
    }
    buildFeatures {
        compose = true
        aidl = false
        buildConfig = false
        renderScript = false
        shaders = false
    }
}

dependencies {
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}

build.gradle.kts(:app)

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
    id("com.google.devtools.ksp") version "2.1.0-1.0.29"
}

android {
    namespace = "com.stream.myapp"
    compileSdk = 35

    defaultConfig {
        applicationId = "com.stream.myapp"
        minSdk = 27
        targetSdk = 35
        versionCode = 1
        versionName = "1.0"
    
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    
    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }
    kotlinOptions {
        jvmTarget = "21"
    }
    buildFeatures {
        compose = true
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    dynamicFeatures.addAll(listOf(":payment"))
}

dependencies {
    implementation(libs.androidx.navigation.compose)
    
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.material.icons.extended)
    
    testImplementation(libs.junit)
    
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    androidTestImplementation(libs.androidx.navigation.testing)
    
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
    
    //Room
    implementation(libs.androidx.room.runtime)
    ksp(libs.androidx.room.compiler)
    implementation(libs.androidx.room.ktx)
    
    implementation(project(path = ":payment", configuration = "default"))
}

settings.gradle.kts

pluginManagement {
    repositories {
        google {
            content {
                includeGroupByRegex("com\\.android.*")
                includeGroupByRegex("com\\.google.*")
                includeGroupByRegex("androidx.*")
            }
        }
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "My App"
include(":app")
include(":payment")

解决方案

1. 修改:payment模块的插件类型

动态特性模块必须使用dynamic-feature插件,而非application插件。修改:payment的plugins块:

plugins {
    alias(libs.plugins.android.dynamicFeature) // 替换原有的android.application
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
}

如果你的libs.versions.toml中未定义android.dynamicFeature,可以直接使用:

plugins {
    id("com.android.dynamic-feature")
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
}

2. 移除:app中多余的依赖声明

主模块已通过dynamicFeatures.addAll(listOf(":payment"))关联动态模块,无需额外添加implementation(project(path = ":payment", configuration = "default")),删除该行代码即可。

3. 统一minSdk版本

:payment模块的minSdk=28高于:app的minSdk=27,会导致兼容性问题。修改:payment的defaultConfig:

defaultConfig {
    minSdk = 27 // 调整为与app一致或更低
    targetSdk = 35
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

原因说明

  • 动态特性模块的插件类型错误:application插件生成的变体仅支持编译和运行时用途,无法满足动态模块所需的android-reverse-meta-data用途,必须使用dynamic-feature插件生成对应变体。
  • 重复依赖声明:主模块通过dynamicFeatures配置后,Gradle会自动处理动态模块的依赖关系,额外添加implementation依赖会触发错误的解析逻辑。
  • minSdk版本不兼容:动态模块的最低SDK版本不能高于主模块,否则会导致安装和运行时的兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 23:54:54