Android Studio Kotlin项目Gradle同步失败问题求助及解决方案
解决Gradle同步失败:找不到com.android.application插件7.2.0版本
我最近从零开始用Kotlin创建Android Studio项目时,遇到了Gradle同步失败的问题,每次同步都会弹出找不到指定插件的报错,折腾了一番后终于解决了,分享给遇到同样问题的开发者。
报错信息
同步失败时的提示如下:
Build文件'C:\Users\Yun\AndroidStudioProjects\Prac_App_2\build.gradle'第3行:未在以下任何源中找到插件[id: 'com.android.application', version: '7.2.0', apply: false]
建议:使用--info或--debug选项获取更多日志输出;使用--scan获取完整分析信息。异常信息:org.gradle.api.plugins.UnknownPluginException: 未在以下任何源中找到插件[id: 'com.android.application', version: '7.2.0', apply: false]
- Gradle核心插件(插件不在'org.gradle'命名空间下)
- 插件仓库(无法解析插件工件'com.android.application:com.android.application.gradle.plugin:7.2.0')
已搜索的仓库:Gradle中央插件仓库、Google、MavenRepo
原项目配置
根目录build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.2.0' apply false id 'com.android.library' version '7.2.0' apply false id 'org.jetbrains.kotlin.android' version '1.6.21' apply false } task clean(type: Delete) { delete rootProject.buildDir }
app模块build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { compileSdk 32 defaultConfig { applicationId "com.example.prac_app_2" minSdk 23 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.6.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
修复方案
在Miguel Reid Ruiz的帮助下,我通过在根目录的build.gradle中添加buildscript块解决了问题。这个块指定了Gradle需要的仓库和插件依赖,让Gradle能够正确找到并下载所需的插件版本。
修复后的根目录build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.2.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21" } } plugins { id 'com.android.application' version '7.2.0' apply false id 'com.android.library' version '7.2.0' apply false id 'org.jetbrains.kotlin.android' version '1.6.21' apply false } task clean(type: Delete) { delete rootProject.buildDir }
简单来说,虽然原配置在plugins块中声明了插件,但缺少了buildscript部分来告知Gradle去哪里找这些插件的工件,补充之后就能正常同步了。
内容的提问来源于stack exchange,提问作者Yun
相关产品推荐
相关产品推荐

