Android Studio में Gradle के माध्यम से लाइब्रेरी डिपेंडेंसी जोड़ने के विभिन्न तरीके और बेस्ट प्रैक्टिस।
विधि 1: ऐप-लेवल build.gradle (सबसे आम)
dependencies {
// Maven Central / JCenter से
implementation 'com.google.android.gms:play-services-location:21.0.1'
implementation 'com.karumi:dexter:6.2.3'
implementation 'androidx.recyclerview:recyclerview:1.3.0'
// लोकल JAR/AAR
implementation files('libs/mylibrary.jar')
implementation(name: 'mylibrary', ext: 'aar')
// मॉड्यूल डिपेंडेंसी
implementation project(':mylibrarymodule')
// टेस्ट डिपेंडेंसी
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
विधि 2: प्रोजेक्ट-लेवल settings.gradle (नया तरीका - Gradle 7+)
// settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' } // JitPack के लिए
}
}
विधि 3: Version Catalog (TOML) - आधुनिक बेस्ट प्रैक्टिस
gradle/libs.versions.toml:
[versions]
agp = "8.1.0"
kotlin = "1.9.0"
glide = "4.16.0"
retrofit = "2.9.0"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version = "1.12.0" }
glide = { group = "com.github.bumptech.glide", name = "glide", version.ref = "glide" }
glide-compiler = { group = "com.github.bumptech.glide", name = "compiler", version.ref = "glide" }
retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
build.gradle.kts (Kotlin DSL):
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.glide)
annotationProcessor(libs.glide.compiler)
implementation(libs.retrofit)
}
विधि 4: Project Structure डायलॉग (GUI)
- File → Project Structure (Ctrl+Alt+Shift+S)
- Dependencies टैब चुनें
- + → Library dependency
- लाइब्रेरी खोजें (जैसे "glide")
- चुनें और OK क्लिक करें
आम रिपॉजिटरी
google()- Google Maven (AndroidX, Play Services, Firebase)mavenCentral()- Maven Central (अधिकांश ओपन सोर्स)maven { url 'https://jitpack.io' }- JitPack (GitHub लाइब्रेरी)
कॉन्फिगरेशन प्रकार
| कॉन्फिगरेशन | उपयोग |
|---|---|
implementation | कंपाइल + रनटाइम, ट्रांजिटिव एक्सपोज़ नहीं (अनुशंसित) |
api | implementation + ट्रांजिटिव एक्सपोज़ (लाइब्रेरी के लिए) |
compileOnly | केवल कंपाइल टाइम, रनटाइम में नहीं (एनोटेशन प्रोसेसर) |
runtimeOnly | केवल रनटाइम (JDBC ड्राइवर) |
annotationProcessor | एनोटेशन प्रोसेसर (कंपाइल टाइम) |
kapt | Kotlin एनोटेशन प्रोसेसर |
testImplementation | यूनिट टेस्ट |
androidTestImplementation | इंस्ट्रूमेंटेशन टेस्ट |