Android Gradle同步时提示找不到implementation()方法如何解决?
问题原因
buildscript块下的dependencies是用来声明Gradle构建过程本身需要的依赖(比如Android Gradle插件),只能使用classpath关键字,不支持implementation/compile这类应用业务依赖的声明关键字。你当前的问题是将应用依赖错误写在了根目录build.gradle的buildscript依赖块中,文件内的注释也已经明确提示:Do not place your application dependencies here; they belong in the individual module build.gradle files。你之前尝试修改关键字为compile、调整Gradle版本都无法解决问题,核心原因就是依赖放错了位置,和关键字、Gradle版本无关。
修复步骤
- 第一步:删除根目录build.gradle中buildscript→dependencies下的两行implementation声明,修改后根目录build.gradle内容如下:
buildscript { repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.0.3" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } task clean(type: Delete) { delete rootProject.buildDir }
- 第二步:找到项目的模块级build.gradle文件,默认路径为
/home/qwirrr/AndroidStudioProjects/ClockIn/app/build.gradle,在该文件的dependencies块中添加你需要的业务依赖:
dependencies { // 保留原有已声明的其他依赖,新增以下两行 implementation 'androidx.fragment:fragment:1.3.6' implementation 'com.google.android.material:material:1.0.0' }
- 第三步:重新执行Gradle项目同步即可解决报错。
内容的提问来源于stack exchange,提问作者qwirrr
相关产品推荐
相关产品推荐

