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

Android Studio多模块项目Gradle Catalog使用异常及构建报错

Gradle Catalog多模块问题解决:android()方法找不到 + 新建模块不使用Catalog提示

问题描述

在Android项目中使用Gradle Catalog(libs.versions.toml)功能,单模块运行正常,但添加多模块后出现两个问题:

  • 新建模块时提示「New Module will not use Version catalog information」
  • :feature:stage模块的build.gradle第6行报错:
Build file 'C:\dev\workspace\android\feature\stage\build.gradle' line: 6

A problem occurred evaluating project ':feature:stage'.
> Could not find method android() for arguments [build_74ujjxctpngqx1up1meon6fyc$_run_closure1@3e267557] on project ':feature:stage' of type org.gradle.api.Project.

解决方案

1. 修复android()方法找不到的报错

报错根源是**:feature:stage模块未正确应用Android插件**。你在该模块的plugins块中给插件添加了apply false——这是根项目用来声明插件但不应用到自身的写法,子模块需要直接应用插件才能激活android{}配置块。

修改:feature:stage的build.gradle:

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

移除apply false,让插件直接作用于当前模块,android{}方法会由com.android.application插件提供,报错即可解决。

2. 让模块依赖完全使用Gradle Catalog管理

当前模块依赖仍为硬编码版本,需替换为libs.versions.toml中的别名,同时可利用bundle简化依赖声明:

首先补充libs.versions.toml的[libraries]部分,添加缺失的依赖配置:

[libraries]
# ... 原有内容保留
androidx-appcompat = "androidx.appcompat:appcompat:1.4.1"
material = "com.google.android.material:material:1.5.0"

然后修改:feature:stage的dependencies块:

dependencies {
    implementation libs.core.ktx
    implementation libs.androidx.appcompat
    implementation libs.material
    testImplementation libs.test.junit.core
    androidTestImplementation libs.test.junit
    androidTestImplementation libs.test.espresso
}

3. 消除「New Module will not use Version catalog information」提示

这个提示多因Android Studio版本兼容性或新建模块配置导致:

  • 确保Android Studio版本为**Electric Eel (2022.1.1)**及以上,旧版本对Gradle Catalog支持不完善
  • 新建模块时,在「Configure your new module」页面勾选「Use library versions from version catalog」选项(部分版本可见)
  • 若无该选项,新建模块后手动修改build.gradle,替换硬编码依赖为libs别名,并应用插件别名

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 07:45:31