Gradle插件DSL块插件不适用于buildSrc

rjee0c15  于 2023-03-08  发布在  其他
关注(0)|答案(1)|浏览(161)

我在Gradle中发现了新的plugins声明。首先在根Gradle文件中应用以下声明

plugins {
    id("com.android.application") version "7.2" apply false
    id("com.android.library") version "7.2" apply false
}

代替

classpath 'com.android.tools.build:gradle:7.2.0'

我还需要buildSrc中的Gradle插件,所以我也有下面的插件

plugins {
    `kotlin-dsl`
}

gradlePlugin {
    plugins {
        register("LibraryCommonPlugin") {
            id = "libraryCommonPlugin"
            implementationClass = "LibraryCommonPlugin"
        }
    }
}

repositories {
    mavenCentral()
    gradlePluginPortal()
    google()
}

dependencies {
    compileOnly(libs.plugin.android)
    compileOnly(libs.plugin.kotlin)
    implementation(libs.plugin.gradleVersions)

    implementation(libs.kotlin)

    compileOnly(gradleApi())
    compileOnly(localGroovy())
}

我有一个通用的Gradle文件,如果使用了,它会在子模块中应用插件

    • 安卓默认值. gradle**
apply from: "$rootDir/buildsystem/helper.gradle"
apply plugin: "libraryCommonPlugin"

android {

    defaultConfig {
        ...
    • 应用程序/构建版本. gradle. kts**
plugins {
    id("com.android.application")

    kotlin("android")
    kotlin("kapt")
}

apply(from = "../buildsystem/android_defaults.gradle")

但是当我尝试生成时IDE抛出了异常
出了什么问题:评估脚本时出现问题。com/android/build/gradle/AppPlugin
原因:java. lang. NoClassDefFoundError:com/安卓/构建/升级版/应用插件
此自定义插件出现问题

class LibraryCommonPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        target.plugins.all { plugin ->
            when (plugin) {
                is AppPlugin -> target.configureAndroid()
                is LibraryPlugin -> target.configureLibrary()
                is KotlinBasePluginWrapper -> target.configureWithKotlinPlugin()
            }
            true
        }
    }

    private fun Project.configureAndroid() {
        androidDependencies()
        baseKotlinModuleDependencies()
        extensions.getByType<AppExtension>().configure()
    }

    private fun Project.configureLibrary() {
        androidDependencies()
        baseKotlinModuleDependencies()
        extensions.getByType<LibraryExtension>().configure()
    }

    private fun Project.configureWithKotlinPlugin() {
        baseKotlinDependencies()
        tasks.withType<KotlinCompile>().configureEach {
            this.kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
        }
    }
}

private fun BaseExtension.configure() {
    setCompileSdkVersion(ProjectConfig.compileSdkVersion)

    with(defaultConfig) {
        minSdk = ProjectConfig.minSdkVersion
        targetSdk = ProjectConfig.targetSdkVersion
        vectorDrawables.useSupportLibrary = true
    }

    with(compileOptions) {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
        isCoreLibraryDesugaringEnabled = true
    }
}

private fun Project.androidDependencies() {
    commonAndroidDependencies()
}

private fun Project.commonAndroidDependencies() {
    dependencies {
        add("coreLibraryDesugaring", Dependencies.Tools.desugarJdkLibs)
    }
}

private const val IMPLEMENTATION = "implementation"
private const val LINT_CHECKS = "lintChecks"

private fun Project.baseKotlinDependencies() {
    dependencies {
        add(IMPLEMENTATION, Dependencies.Base.kotlin)
        add(IMPLEMENTATION, Dependencies.Base.kotlinReflect)
    }
}

private fun Project.baseKotlinModuleDependencies() {
    dependencies {
        add(LINT_CHECKS, project(Dependencies.Modules.Base.customLint))
    }
}

那么我怎样才能让Gradle插件在这两个地方都工作呢?
谢谢!

fumotvh3

fumotvh31#

我也遇到过类似的问题,请尝试更换

compileOnly(libs.plugin.android)
compileOnly(libs.plugin.kotlin)

implementation(libs.plugin.android)
implementation(libs.plugin.kotlin)

相关问题