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

Gradle同步Android项目报错:无法加载com.android.build.gradle.AppPlugin类

解决Gradle同步报错:Unable to load class 'com.android.build.gradle.AppPlugin'

报错信息

同步Android项目时出现以下核心报错:

Unable to load class 'com.android.build.gradle.AppPlugin'.

This is an unexpected error. Please file a bug containing the idea.log file.
Build file 'C:\Users\user\Desktop\android\android-anpr-master\app\build.gradle' line: 1

A problem occurred evaluating project ':app'.
> Could not find implementation class 'com.android.build.gradle.AppPlugin' for plugin 'com.android.application' specified in jar:file:/C:/Users/aprotiere/.gradle/caches/jars-9/f84232a270d8a27731cbe96e1da64c89/gradle-8.1.2.jar!/META-INF/gradle-plugins/com.android.application.properties.

问题分析

报错根源是AGP(Android Gradle插件)版本与配置不匹配,同时存在多处配置错误:

  • AGP 8.1.2已废弃com.android.build.gradle.AppPlugin类,旧的apply plugin语法无法适配新版本
  • 模块依赖中错误添加了AGP的implementation依赖(AGP应在根目录buildscript的classpath中声明)
  • buildToolsVersion '19.0.3'版本过旧,与compileSdkVersion 31严重不兼容
  • 存在无效依赖implementation ':1.6.1',缺少完整坐标
  • 部分maven仓库使用http协议,可能导致资源拉取失败

修复步骤

1. 修正根目录build.gradle配置

确保google()仓库优先级更高(AGP依赖优先从谷歌仓库拉取):

// 顶级构建文件,可添加所有子项目/模块通用的配置选项。
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.2'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        // 旧http仓库替换为https地址,或直接移除(javacv已迁移至mavenCentral)
        // maven { url 'https://maven2.javacv.googlecode.com/git/' }
    }
}

2. 修正app模块build.gradle配置

  • 使用新版本AGP的plugins块替代旧的apply plugin语法
  • 移除无效的AGP implementation依赖
  • 修复无效依赖坐标,移除或更新过旧的buildToolsVersion(AGP 7.0+会自动匹配compileSdk版本)
  • 合并重复的本地jar依赖声明

修改后的配置:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31
    // AGP 7.0+无需手动指定buildToolsVersion,自动匹配compileSdk版本
    // buildToolsVersion '31.0.0'

    defaultConfig {
        applicationId "com.DVLA.testapp"
        minSdkVersion 14
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
    // 若仍需旧javacv仓库,替换为https地址
    // maven { url 'https://maven2.javacv.googlecode.com/git/' }
}

dependencies {
    // 修复无效依赖,示例为javacv的正确坐标(根据实际需求调整)
    implementation 'org.bytedeco:javacv:1.6.1'
    implementation 'joda-time:joda-time:2.12.5'
    // 合并本地jar依赖声明,避免重复
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':libraries:opencv')
}

3. 检查Gradle Wrapper版本

确保gradle/wrapper/gradle-wrapper.properties中的Gradle版本与AGP 8.1.2兼容(AGP 8.1.x对应Gradle 8.1-8.2):

distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.2-bin.zip

4. 清理缓存并重新同步

  • 执行Android Studio的File > Invalidate Caches...,勾选清除缓存并重启
  • 手动删除项目根目录下的.gradle文件夹和app/build文件夹
  • 重新执行Gradle同步

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 01:30:32