Kotlin DSL لـ Gradle
رحّل البرامج النصية `build.gradle` الخاصة بكم إلى `build.gradle.kts` باستخدام Kotlin DSL لتحسين سهولة القراءة وأمان الأنواع
Kotlin DSL لـ Gradle درس مجاني في Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Groovy & Gradle: JVM Automation and Build Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Welcome to Kotlin DSL
Welcome to Kotlin DSL for Gradle! This lesson explores how to switch from Groovy DSL to Kotlin DSL in your Gradle build scripts.
Kotlin DSL offers enhanced readability and type safety, making your build files more robust and easier to maintain. It's a modern approach to defining your Gradle projects.
Why Kotlin DSL?
Why make the switch to Kotlin DSL?
- Type Safety: Kotlin DSL is statically typed, meaning the compiler can catch errors at build time, not runtime. This reduces surprises!
- IDE Support: Get better auto-completion, refactoring, and navigation in your IDE (like IntelliJ IDEA) because it understands the types.
- Readability: Kotlin's concise syntax often makes build scripts clearer, especially for complex logic.
- Consistency: If your project already uses Kotlin for application code, using Kotlin DSL brings consistency to your entire codebase.
Basic Syntax: Plugins Block
Let's start with a fundamental difference: how you apply plugins. In Kotlin DSL, your build script file extension changes from .gradle to .gradle.kts.
Notice the use of id("plugin-id") and optional version "..." for applying plugins.
plugins {
id("java")
id("application") version "8.1.0"
}Declaring Dependencies
Declaring dependencies is very similar to Groovy DSL, but with Kotlin's function call syntax. You use named arguments for the dependency string.
Here's how you'd add common dependencies like JUnit for testing and Apache Commons for utilities.
dependencies {
implementation("org.slf4j:slf4j-api:2.0.7")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.0")
}Defining Custom Tasks
Creating custom tasks in Kotlin DSL is straightforward. You use tasks.register("taskName") { ... } to define a new task.
Inside the lambda, you can configure the task, for example, by adding a doLast action to execute some code when the task runs.
tasks.register("helloKotlin") {
group = "Greeting"
description = "Prints a friendly message using Kotlin DSL."
doLast {
println("Hello from a Kotlin DSL task!")
}
}Configuring Existing Tasks
You can also configure existing tasks, like the built-in jar task, using tasks.getByName("taskName") { ... }. This allows you to customize their behavior.
For example, you can set the base name of your JAR file or add a manifest entry.
tasks.getByName<Jar>("jar") {
archiveBaseName.set("my-app-kotlin")
manifest {
attributes["Implementation-Title"] = project.name
attributes["Implementation-Version"] = project.version
}
}Project Properties & Variables
Accessing project properties and defining variables is also type-safe. You can declare properties like group and version using property assignment.
For local variables, you use Kotlin's val or var keywords, enhancing clarity and preventing accidental reassignments.
group = "com.example"
version = "1.0.0"
val myProperty = "Some Value"
tasks.register("printProps") {
doLast {
println("Project Group: $group")
println("My Property: $myProperty")
}
}Type-Safe Accessors
A major benefit of Kotlin DSL is type-safe accessors. For common plugins like Java, you get dedicated blocks and properties with full IDE auto-completion.
Instead of string-based property access, you use object-oriented syntax, making it harder to make typos and easier to discover options.
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
application {
mainClass.set("com.example.MainApp")
}Migrating a Simple Groovy Script
Let's look at a quick comparison to see how a small Groovy DSL snippet translates to Kotlin DSL. Notice the use of id("..."), implementation(...), and property assignment.
The structure remains similar, but the syntax becomes more explicit and type-checked.
// Groovy DSL (build.gradle)
// plugins {
// id 'java'
// }
// dependencies {
// implementation 'org.apache.commons:commons-lang3:3.12.0'
// }
// Kotlin DSL (build.gradle.kts)
plugins {
java
}
dependencies {
implementation("org.apache.commons:commons-lang3:3.12.0")
}Quick Check on Kotlin DSL
Which of the following are key benefits of using Kotlin DSL over Groovy DSL for Gradle build scripts?
Recap: Kotlin DSL for Gradle
In this lesson, we explored the benefits and basic syntax of Kotlin DSL for Gradle.
- You learned why type safety, IDE support, and readability make Kotlin DSL a powerful choice.
- We covered how to apply plugins, declare dependencies, and define/configure tasks using the
.gradle.ktsfile extension. - You also saw how to work with project properties and utilize type-safe accessors for common configurations.
Embracing Kotlin DSL can lead to more robust and maintainable Gradle builds!
تعلم Groovy مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «Kotlin DSL لـ Gradle» مجاني؟
نعم — نص درس «Kotlin DSL لـ Gradle» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Groovy & Gradle: JVM Automation and Build Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.
ماذا ستتعلم في «Kotlin DSL لـ Gradle»؟
رحّل البرامج النصية `build.gradle` الخاصة بكم إلى `build.gradle.kts` باستخدام Kotlin DSL لتحسين سهولة القراءة وأمان الأنواع تتمرن على Groovy & Gradle: JVM Automation and Build Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Groovy & Gradle: JVM Automation and Build Engineering؟
لا تُشترط خبرة سابقة. Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «Kotlin DSL لـ Gradle»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Groovy & Gradle: JVM Automation and Build Engineering هذا؟
نعم. كل درس في Groovy & Gradle: JVM Automation and Build Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- Kotlin DSL لـ Gradle
- بناء المشاريع متعددة اللغات
- تكامل IDE وأدوات التطوير
- كتابة مهام مخصصة باستخدام Kotlin DSL