Gradle插件DSL块插件不适用于buildsrc
我在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/AppPluginCaused 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有类似的问题。
尝试替换
为
I had similar problem.
Try to replace
with