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

发布于 2025-02-04 07:13:58 字数 3855 浏览 3 评论 0原文

我在gradle中发现新的插件声明。首先在root 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'

gradle插件build> buildsrc,所以我也在下面也

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())
}

有如果使用

android_defaults.gradles.gradle

apply from: "$rootDir/buildsystem/helper.gradle"
apply plugin: "libraryCommonPlugin"

android {

    defaultConfig {
        ...

app/build.gradle.kts

plugins {
    id("com.android.application")

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

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

当我尝试构建时一个例外

出了什么问题:发生了一个问题评估脚本。 com/android/build/gradle/appplugin

由:java.lang.noclassdeffounderror: com/android/build/gradle/appplugin

从此自定义插件出现的问题,

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插件适用于两个地方?

谢谢!

I am discovering the new plugins declaration in Gradle. First applying below in the root Gradle file

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

instead of

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

And I also need the Gradle plugin in buildSrc so I have below in there, too

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())
}

And I have a common Gradle file that applies the plugin in child modules if is used

android_defaults.gradle

apply from: "$rootDir/buildsystem/helper.gradle"
apply plugin: "libraryCommonPlugin"

android {

    defaultConfig {
        ...

app/build.gradle.kts

plugins {
    id("com.android.application")

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

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

but then IDE throws an exception when I try to build

What went wrong: A problem occurred evaluating script.
com/android/build/gradle/AppPlugin

Caused by: java.lang.NoClassDefFoundError:
com/android/build/gradle/AppPlugin

Problems occuring from this custom plugin

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))
    }
}

So how can I have the Gradle plugin works for the both places?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2025-02-11 07:13:58

我有类似的问题。
尝试替换

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

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

I had similar problem.
Try to replace

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

with

implementation(libs.plugin.android)
implementation(libs.plugin.kotlin)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文