Gradle Dependency Management: সম্পূর্ণ গাইড
Android Studio-তে বাহ্যিক লাইব্রেরি (Dependency) যোগ করার বিভিন্ন পদ্ধতি, সংস্করণ ম্যানেজমেন্ট এবং সেরা অনুশীলন।
Dependency যোগ করার প্রকারভেদ
১. Remote Binary Dependency (Maven/JCenter/Gradle Plugin Portal)
সবচেয়ে সাধারণ পদ্ধতি - Maven Repository থেকে ডাউনলোড হয়।
App-level build.gradle (Groovy DSL):
dependencies {
// Implementation - কম্পাইল টাইম + রানটাইম (সাধারণ)
implementation 'androidx.recyclerview:recyclerview:1.3.2'
// API - কম্পাইল টাইম + রানটাইম + কনসামাররা দেখতে পাবে (লাইব্রেরি মডিউলের জন্য)
api 'com.google.android.material:material:1.11.0'
// Compile Only - শুধু কম্পাইল টাইম, রানটাইমে নেই (Annotation Processor ইত্যাদি)
compileOnly 'org.projectlombok:lombok:1.18.30'
// Runtime Only - শুধু রানটাইমে, কম্পাইল টাইমে নেই
runtimeOnly 'com.github.tony19:logback-android:2.0.0'
// Test Dependencies
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
// Kotlin Symbol Processing (KSP)
ksp 'com.google.dagger:hilt-compiler:2.48'
}
Kotlin DSL (build.gradle.kts):
dependencies {
implementation("androidx.recyclerview:recyclerview:1.3.2")
implementation("com.google.android.material:material:1.11.0")
compileOnly("org.projectlombok:lombok:1.18.30")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
২. Local Binary Dependency (JAR/AAR ফাইল)
dependencies {
// Local JAR
implementation files('libs/mylibrary.jar')
// Local AAR
implementation files('libs/mylibrary.aar')
// পুরো libs ফোল্ডার
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
}
৩. Module Dependency (Multi-module Project)
dependencies {
// প্রজেক্টের অন্য মডিউল
implementation project(':mylibrary')
implementation project(':feature:login')
// ডাইনামিক ফিচার মডিউল
implementation project(path: ':dynamic_feature', configuration: 'default')
}
Repository কনফিগারেশন (settings.gradle / build.gradle)
Modern: settings.gradle (Dependency Resolution Management)
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url = uri("https://jitpack.io") }
maven { url = uri("https://maven.aliyun.com/repository/public") }
}
}
Legacy: Project-level build.gradle
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Deprecated - এখন শুধু রিড-অনলি
maven { url 'https://jitpack.io' }
}
}
Version Catalog (TOML) - আধুনিক Version Management (Gradle 7.4+)
gradle/libs.versions.toml
[versions]
agp = "8.3.0"
kotlin = "1.9.20"
coreKtx = "1.12.0"
recyclerview = "1.3.2"
material = "1.11.0"
glide = "4.16.0"
hilt = "2.48"
junit = "4.13.2"
espresso = "3.5.1"
[libraries]
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" }
material = { module = "com.google.android.material:material", version.ref = "material" }
glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" }
glide-compiler = { module = "com.github.bumptech.glide:compiler", version.ref = "glide" }
hilt-api = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hilt" }
junit = { module = "junit:junit", version.ref = "junit" }
espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "kotlin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
build.gradle.kts-এ ব্যবহার
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.recyclerview)
implementation(libs.material)
implementation(libs.glide)
ksp(libs.glide.compiler)
ksp(libs.hilt.compiler)
testImplementation(libs.junit)
androidTestImplementation(libs.espresso.core)
}
Transitive Dependencies & Conflict Resolution
# টার্মিনালে চালান ./gradlew dependencies # সব ডিপেন্ডেন্সি টREE ./gradlew app:dependencies # শুধু app মডিউল ./gradlew app:dependencies --configuration releaseRuntimeClasspath ./gradlew dependencyInsight --dependency recyclerview --configuration releaseRuntimeClasspath ./gradlew help --task dependencies
সেরা অনুশীলন (Best Practices)
| অনুশীলন | ব্যাখ্যা | |
|---|---|---|
| Version Catalog ব্যবহার করুন | কেন্ট্রালাইজড ভার্সন ম্যানেজমেন্ট, টাইপ সেফ, আপডেট সহজ | |
| implementation vs api | ডিফল্ট implementation; লাইব্রেরি মডিউলে api ব্যবহার করুন | |
| Dynamic Version (+) এড়িয়ান | 1.0.+ ব্যবহার করবেন না - রিপ্রducible বিল্ড ভেঙে | |
| JCenter ব্যবহার করবেন না | Deprecated, Maven Central বা Google Maven ব্যবহার করুন | |
| Dependency আপডেট নিয়মিত | ./gradlew dependencyUpdates (Ben Manes Plugin) ব্যবহার করুন | |
| Unused Dependency রিমুভ | Gradle Lint / ./gradlew unusedDependencies (Plugin) | |
| Bill of Materials (BOM) | Firebase, Play Services, Room BOM ব্যবহার করুন ভার্সন অ্যালাইনমেন্টে |
| Error | কারণ | সমাধান |
|---|---|---|
| Duplicate class | একই ক্লাস একাধিক লাইব্রেরিতে | exclude group/module, force version |
| Version conflict | ট্রানজিটিভ ডিপেন্ডেন্সি মিসম্যাচ | ResolutionStrategy.force, BOM |
| Could not find | Repository মিসিং বা লাইব্রেরি মুছে গেছে | Repository যোগ করুন, অল্টারনেটিভ খুঁজুন |
| Manifest merger failed | Dependency-এর Manifest কনফ্লিক্ট | tools:replace, tools:remove ব্যবহার করুন |