Gradle के Basics
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
The build.gradle.kts File
किसी Kotlin project का build configuration -- plugins, dependencies, और settings -- आमतौर पर build.gradle.kts में declare किया जाता है, खुद Kotlin syntax का उपयोग करते हुए।
उदाहरण: The build.gradle.kts File
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
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
./gradlew build
./gradlew run
./gradlew test
⚠️ Run this in your own terminal or Node.js environment.
- गलत build file section edit करना (जैसे
dependenciesबनामplugins) और यह confuse होना कि कोई नई library क्यों नहीं पहचानी गई। build.gradle.ktsबदलने के बाद./gradlew build(या IDE sync) चलाना भूल जाना, जिससे नई configuration कभी लागू नहीं होती।- गलत 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 buildproject को 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: