افزونه Compose Compiler Gradle را تنظیم کنید

برای Gradle، از افزونه Compose Compiler Gradle برای تنظیم و پیکربندی Compose استفاده کنید.

با کاتالوگ‌های نسخه Gradle راه‌اندازی کنید

افزونه Compose Compiler Gradle را تنظیم کنید:

  1. در فایل libs.versions.toml ، هرگونه ارجاع به کامپایلر Compose را حذف کنید.
  2. در بخش‌های versions و plugins ، وابستگی جدید را اضافه کنید:
[versions]
kotlin = "2.3.10"

[plugins]
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

// Add this line
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
  1. در فایل build.gradle.kts ریشه پروژه، کد زیر را به بخش plugins اضافه کنید.
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler) apply false
}
  1. در هر ماژولی که از Compose استفاده می‌کند، افزونه را اعمال کنید:
plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler)
}

اگر پروژه از تنظیمات پیش‌فرض استفاده می‌کرد، اکنون باید ساخته و کامپایل شود. اگر گزینه‌های سفارشی را در کامپایلر Compose پیکربندی کرده بود، بخش بعدی را دنبال کنید.

کامپایلر Compose را بدون کاتالوگ‌های نسخه Gradle راه‌اندازی کنید

افزونه را به فایل‌های build.gradle.kts مرتبط با ماژول‌هایی که Compose در آنها استفاده می‌شود، اضافه کنید:

plugins {
    id("org.jetbrains.kotlin.plugin.compose") version "2.3.10" // this version matches your Kotlin version
}

مسیر کلاس (classpath) را به فایل build.gradle.kts پروژه سطح بالای خود اضافه کنید:

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:2.3.10")
    }
}

گزینه‌های پیکربندی با افزونه Gradle کامپایلر Compose

برای پیکربندی کامپایلر Compose با استفاده از افزونه Gradle، بلوک composeCompiler را به فایل build.gradle.kts ماژول در سطح بالا اضافه کنید:

android {  }

composeCompiler {
    reportsDestination = layout.buildDirectory.dir("compose_compiler")
    stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
}

برای لیست کامل گزینه‌های موجود، به مستندات مراجعه کنید.

وابستگی‌های Compose را تنظیم کنید

همیشه از آخرین نسخه Compose BOM استفاده کنید: 2026.03.00 .

برای فعال کردن قابلیت Compose در اندروید استودیو، پرچم compose را در داخل Android BuildFeatures روی true تنظیم کنید.

تعریف زیر را به فایل build.gradle برنامه خود اضافه کنید:

گرووی

android {
    buildFeatures {
        compose true
    }
}

کاتلین

android {
    buildFeatures {
        compose = true
    }
}

فایل اجرایی Compose BOM و زیرمجموعه‌ای از وابستگی‌های کتابخانه Compose را اضافه کنید:

گرووی

dependencies {

    def composeBom = platform('androidx.compose:compose-bom:2026.03.00')
    implementation composeBom
    androidTestImplementation composeBom

    // Choose one of the following:
    // Material Design 3
    implementation 'androidx.compose.material3:material3'
    // or skip Material Design and build directly on top of foundational components
    implementation 'androidx.compose.foundation:foundation'
    // or only import the main APIs for the underlying toolkit systems,
    // such as input and measurement/layout
    implementation 'androidx.compose.ui:ui'

    // Android Studio Preview support
    implementation 'androidx.compose.ui:ui-tooling-preview'
    debugImplementation 'androidx.compose.ui:ui-tooling'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    // Optional - Add window size utils
    implementation 'androidx.compose.material3.adaptive:adaptive'

    // Optional - Integration with activities
    implementation 'androidx.activity:activity-compose:1.13.0'
    // Optional - Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.10.0'
    // Optional - Integration with LiveData
    implementation 'androidx.compose.runtime:runtime-livedata'
    // Optional - Integration with RxJava
    implementation 'androidx.compose.runtime:runtime-rxjava2'

}

کاتلین

dependencies {

    val composeBom = platform("androidx.compose:compose-bom:2026.03.00")
    implementation(composeBom)
    androidTestImplementation(composeBom)

    // Choose one of the following:
    // Material Design 3
    implementation("androidx.compose.material3:material3")
    // or skip Material Design and build directly on top of foundational components
    implementation("androidx.compose.foundation:foundation")
    // or only import the main APIs for the underlying toolkit systems,
    // such as input and measurement/layout
    implementation("androidx.compose.ui:ui")

    // Android Studio Preview support
    implementation("androidx.compose.ui:ui-tooling-preview")
    debugImplementation("androidx.compose.ui:ui-tooling")

    // UI Tests
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    // Optional - Add window size utils
    implementation("androidx.compose.material3.adaptive:adaptive")

    // Optional - Integration with activities
    implementation("androidx.activity:activity-compose:1.13.0")
    // Optional - Integration with ViewModels
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.10.0")
    // Optional - Integration with LiveData
    implementation("androidx.compose.runtime:runtime-livedata")
    // Optional - Integration with RxJava
    implementation("androidx.compose.runtime:runtime-rxjava2")

}