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

Android Gradle7.2构建报错:找不到DefaultDependencyHandler对象的compile()方法

问题根因

你使用的是Gradle 7.2版本,该版本已经完全移除了早期Gradle版本中用于声明依赖的compile、testCompile、androidTestCompile等废弃API,这类API早在Gradle 3.0时代就已被新的依赖配置关键字替代,因此构建时会抛出找不到compile()方法的错误。

解决方案

直接修改build.gradle中dependencies代码块的依赖声明关键字,替换规则如下:

  • 原compile替换为implementation(如果是库模块需要将依赖暴露给上层模块,可替换为api,你的应用模块直接用implementation即可)
  • 原testCompile替换为testImplementation
  • 原androidTestCompile替换为androidTestImplementation

修改后的dependencies代码块如下:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:leanback-v17:25.4.0'
    implementation 'com.android.support:recyclerview-v7:25.4.0'
    implementation 'com.android.support:preference-v7:25.4.0'
    implementation 'com.android.support:preference-v14:25.4.0'
    implementation 'com.android.support:preference-leanback-v17:25.4.0'
    implementation 'com.android.support:appcompat-v7:25.4.0'
    implementation 'com.android.support:palette-v7:25.4.0'
}

修改完成后重新同步Gradle即可正常构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 20:45:08