কম্পিউটার টিউটোরিয়াল

Android Studio-এ Dependency (লাইব্রেরি) যোগ করার পদ্ধতি

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)

BOM (Bill of Materials) উদাহরণ

dependencies {
    // Firebase BOM - সব Firebase লাইব্রেরির ভার্সন অটো ম্যানেজ
    implementation platform('com.google.firebase:firebase-bom:32.7.0')
    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-firestore'
    // ভার্সন লিখতে হয় না
    
    // Room BOM
    implementation platform('androidx.room:room-bom:2.6.1')
    implementation 'androidx.room:room-runtime'
    implementation 'androidx.room:room-ktx'
}

সাধারণ সমস্যা ও সমাধান

অনুশীলনব্যাখ্যা
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 findRepository মিসিং বা লাইব্রেরি মুছে গেছেRepository যোগ করুন, অল্টারনেটিভ খুঁজুন
Manifest merger failedDependency-এর Manifest কনফ্লিক্টtools:replace, tools:remove ব্যবহার করুন
  1. অ্যান্ড্রয়েডে ডাইনামিক্যালি বাটন যোগ করা

  2. অ্যান্ড্রয়েডে লাইন আঁকা (Canvas এবং Paint ব্যবহার করে)

  3. Android Studio-এ JAR ফাইল লাইব্রেরি হিসেবে যোগ করার পদ্ধতি

  4. Android-এ সার্কুলার ImageView-এ শ্যাডো এবং বর্ডার যোগ করা