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

自定义Gradle插件加载LibraryExtension类失败问题求助

问题描述

我在buildSrc中编写了自定义Android Library Gradle插件,代码如下:

package com.example.buildSource

import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

class AndroidLibraryPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        with(project) {
            pluginManager.apply("com.android.library")
            pluginManager.apply("org.jetbrains.kotlin.android")
            if (project.findProperty("useCompose") == true)
                pluginManager.apply("org.jetbrains.kotlin.plugin.compose")


            extensions.configure(LibraryExtension::class.java) { // 错误应该来自这里
                compileSdk = AndroidConfig.COMPILE_SDK

                defaultConfig {
                    minSdk = AndroidConfig.MIN_SDK

                    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
                }

                buildTypes {
                    getByName("debug") {
                        isMinifyEnabled = false
                        enableUnitTestCoverage = true
                        enableAndroidTestCoverage = true
                    }

                    getByName("release") {
                        isMinifyEnabled = true
                        isShrinkResources = true
                        proguardFiles(
                            getDefaultProguardFile("proguard-android.txt"),
                            "proguard-rules.pro"
                        )
                    }
                }

                flavorDimensions += "default"
                productFlavors {
                    create("development") {
                        dimension = "default"
                    }

                    create("production") {
                        dimension = "default"
                    }
                }

                compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_21
                    targetCompatibility = JavaVersion.VERSION_21
                }

                packaging.resources.excludes += "/META-INF/{AL2.0,LGPL2.1}"

                tasks.withType(KotlinCompile::class.java).configureEach {
                    compilerOptions {
                        jvmTarget.set(JvmTarget.JVM_21)
                    }
                }

                buildFeatures.compose = project.findProperty("useCompose") == true
            }
        }
    }
}

同一模块的build.gradle.kts配置:

//noinspection UseTomlInstead
plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
    google()
}

gradlePlugin {
    plugins {
        create("myLibPlugin") {
            id = "com.example.myLibPlugin"
            implementationClass = "com.example.buildSource.AndroidLibraryPlugin"
        }
    }
}

dependencies {
    compileOnly("com.android.tools.build:gradle:8.8.2")
    compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.0")
}

将插件应用到其他模块的build.gradle.kts:

plugins {
    id("com.example.myLibPlugin")
}

android {
    namespace = "com.example.core_ui"
    defaultConfig.consumerProguardFiles("consumer-rules.pro")
}

dependencies {

    val composeBom = platform(libs.androidx.compose.bom)

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(composeBom)
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)

    implementation(libs.androidx.constraintlayout.compose)

    implementation(libs.coil.compose)
    implementation(libs.coil.network.okhttp)

    testImplementation(libs.junit)

    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(composeBom)
    androidTestImplementation(libs.androidx.ui.test.junit4)

    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}

构建时抛出错误:

Unable to load class 'com.android.build.api.dsl.LibraryExtension'
com.android.build.api.dsl.LibraryExtension

请问该如何解决?是否是插件配置出错?


解决方案

这个错误的核心原因是buildSrc中的依赖无法在插件运行时被Gradle找到,因为你用了compileOnly,导致编译时能找到类,但运行时类路径缺失。具体修复步骤如下:

1. 替换依赖声明方式

将buildSrc的build.gradle.kts中compileOnly改为implementation,同时添加Android Gradle Plugin的API依赖(因为LibraryExtension属于AGP的API模块):

dependencies {
    implementation("com.android.tools.build:gradle:8.8.2")
    implementation("com.android.tools.build:gradle-api:8.8.2")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.0")
}

解释:

  • gradle-api包含了com.android.build.api.dsl包下的类,是AGP的公开API依赖,必须显式引入
  • implementation会将依赖打包到插件的类路径中,确保运行时能加载到所需类

2. 确保buildSrc的仓库配置正确

确认buildSrc的repositories块包含google()和mavenCentral(),这两个仓库是获取AGP和Kotlin插件的必要来源:

repositories {
    google()
    mavenCentral()
}

3. 清理并重新构建

执行以下操作确保缓存被清理:

  • 运行./gradlew clean
  • 删除项目根目录的.gradle文件夹
  • 重新执行构建命令

额外注意事项

  • 如果你使用的是Gradle 8+,建议将buildSrc的依赖管理迁移到settings.gradle.kts中的dependencyResolutionManagement,或者使用版本 catalog 统一管理版本,避免版本不一致问题
  • 确认插件应用的模块中,没有重复引入com.android.library插件,你的自定义插件已经自动应用了该插件,无需重复添加

内容的提问来源于stack exchange,提问作者Hưng

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 05:17:01