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

Android Studio Gradle同步失败:找不到com.android.application插件

解决Gradle同步失败:找不到com.android.application插件问题

问题根源

  1. 插件仓库配置缺失:Android官方插件托管在Google Maven仓库,你的配置里插件解析阶段仅搜索了Gradle中央仓库,未访问Google仓库,导致找不到目标插件。
  2. 配置结构混乱:混淆了**根目录build.gradle与模块级build.gradle**的职责——implementation依赖、android配置块、application插件(Java命令行项目专用,Android项目不需要)都错误地放在了根配置文件中。

修复步骤

1. 修正根目录build.gradle

根配置文件仅负责项目级插件与依赖管理,内容调整为:

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

// 关键:让插件解析过程能访问Google仓库
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

2. 修正模块级(如app模块)build.gradle

将原根配置中属于模块的内容迁移至此:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 33
    buildToolsVersion '33.0.0'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        viewBinding true
    }

    defaultConfig {
        applicationId "你的应用ID" // 替换为实际应用ID
        minSdk 21 // 根据需求调整最低SDK版本
        targetSdk 33
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.0.0-rc02'
    implementation 'androidx.navigation:navigation-ui:2.0.0-rc02'
    implementation 'com.google.guava:guava:31.1-jre'

    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

3. 额外清理与验证

  • 删除根配置中的application插件(该插件为Java命令行项目设计,Android项目无需使用)
  • 确认Gradle版本与AGP版本匹配:7.2.2版本的AGP要求Gradle 7.3.3及以上,你当前使用的7.5.1符合要求

最后操作

点击Android Studio右上角的Invalidate Caches / Restart清理缓存,重启后再执行Gradle同步,避免残留配置干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 21:30:53