← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 3 of 7

Gradle के Basics

Gradle वह assistant है जो आपके Kotlin project को चाहिए वे हिस्से download करता है और एक single command से इसे build और run करना जानता है।
Syntax
kotlin
dependencies {
    implementation("group:artifact:version")
    testImplementation("group:artifact:version")
}

What Gradle Does

Gradle source code compile करना, tests चलाना, dependencies resolve करना, और एक पूरी application package करना automate करता है, किसी छोटे project से आगे किसी भी चीज़ के लिए manual kotlinc invocations की जगह लेते हुए।

उदाहरण: What Gradle Does

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "In a real project, Gradle would compile and run this instead of calling kotlinc directly" to the console, with a trailing newline
    println("In a real project, Gradle would compile and run this instead of calling kotlinc directly")
}

The build.gradle.kts File

किसी Kotlin project का build configuration -- plugins, dependencies, और settings -- आमतौर पर build.gradle.kts में declare किया जाता है, खुद Kotlin syntax का उपयोग करते हुए।

उदाहरण: The build.gradle.kts File

bash
plugins {
    kotlin("jvm") version "1.9.22"
    application
}

dependencies {
    implementation(kotlin("stdlib"))
}

application {
    mainClass.set("MainKt")
}

⚠️ Run this in your own terminal or Node.js environment.

Adding Dependencies

Third-party libraries, जैसे kotlinx-coroutines-core, एक implementation scope और version के साथ एक dependencies { } block के अंदर declared होती हैं, और Gradle उन्हें अपने आप download करता है।

उदाहरण: Adding Dependencies

bash
dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    testImplementation("org.jetbrains.kotlin:kotlin-test")
}

⚠️ Run this in your own terminal or Node.js environment.

Common Gradle Commands

./gradlew build project को compile और test करता है, ./gradlew run application की main class execute करता है, और ./gradlew test सिर्फ test suite चलाता है -- gradlew wrapper script का मतलब है कि किसी को अलग से Gradle install करने की ज़रूरत नहीं।

उदाहरण: Common Gradle Commands

bash
./gradlew build
./gradlew run
./gradlew test

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. गलत build file section edit करना (जैसे dependencies बनाम plugins) और यह confuse होना कि कोई नई library क्यों नहीं पहचानी गई।
  2. build.gradle.kts बदलने के बाद ./gradlew build (या IDE sync) चलाना भूल जाना, जिससे नई configuration कभी लागू नहीं होती।
  3. गलत source से snippets copy करके एक ही file में Groovy (build.gradle) और Kotlin DSL (build.gradle.kts) syntax मिलाना।
चैप्टर सारांश
  • Gradle एक build automation tool है जो Kotlin (और दूसरे JVM) projects को compile, test, और package करता है।
  • build.gradle.kts (Kotlin DSL) या build.gradle (Groovy DSL) किसी project के plugins, dependencies, और build configuration को declare करता है।
  • ./gradlew build project को compile करता है, tests चलाता है, और एक wrapper का उपयोग करके build artifacts produce करता है जिसके लिए अलग से Gradle install करने की ज़रूरत नहीं।
  • Dependencies एक dependencies { } block के तहत जोड़ी जाती हैं, ज़रूरी library और version specify करते हुए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.