הגדרת הפלאגין Compose Compiler Gradle

ב-Gradle, משתמשים בפלאגין Compose Compiler Gradle כדי להגדיר את Compose.

הגדרה באמצעות קטלוגים של גרסאות Gradle

מגדירים את הפלאגין Compose Compiler Gradle:

  1. בקובץ libs.versions.toml, מסירים את כל ההפניות ל-Compose Compiler.
  2. בקטעים versions ו-plugins, מוסיפים את התלות החדשה:

    [versions]
    kotlin = "2.3.21"
    
    [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" }
    
  3. בקטע plugins בקובץ build.gradle.kts של הפרויקט, מוסיפים את השורה הבאה:

    plugins {
    // Existing plugins
    alias(libs.plugins.compose.compiler) apply false
    }
    
  4. בכל מודול שמשתמש ב-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.21" // this version matches your Kotlin version
}

מוסיפים את נתיב המחלקה לקובץ build.gradle.kts ברמה העליונה של הפרויקט:

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

אפשרויות הגדרה באמצעות פלאגין 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.06.00.

מגדירים את הדגל compose לערך true בתוך BuildFeatures של Android כדי להפעיל את הפונקציונליות של Compose ב-Android Studio.

מוסיפים את ההגדרה הבאה לקובץ build.gradle של האפליקציה:

מגניב

android {
    buildFeatures {
        compose true
    }
}

Kotlin

android {
    buildFeatures {
        compose = true
    }
}

מוסיפים את ה-BOM של Compose ואת קבוצת המשנה של יחסי התלות בספריית Compose:

מגניב

dependencies {

    def composeBom = platform('androidx.compose:compose-bom:2026.06.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'

}

Kotlin

dependencies {

    val composeBom = platform("androidx.compose:compose-bom:2026.06.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")

}

compileSdk ותאימות לפלאגין של Android Gradle

בכל מהדורה של ספריית Compose, נעשה שימוש בגרסאות העדכניות ביותר של compileSdk כדי לספק גישה לתכונות העדכניות ביותר של Android. גרסאות חדשות יותר של compileSdk דורשות גרסאות חדשות יותר של פלאגין של Android Gradle, ולכן כדי להשתמש בגרסאות חדשות של Compose, צריך להשתמש בפרויקטים בגרסאות חדשות של פלאגין של Android Gradle. מומלץ לעדכן את compileSdk של הפרויקט לגרסאות העדכניות ביותר שפורסמו. אין קשר בין compileSdk לבין targetSdk.

לדוגמה, החל מ-Compose 1.12.0, הפרויקטים צריכים להשתמש ב-compileSdk 37 וב-פלאגין של Android Gradle‏ (AGP) 9.

כדי לבדוק איזו גרסה של AGP נתמכת ברמות שונות של API, אפשר לעיין במסמך בנושא תמיכה ברמות API בפלאגין של Android Gradle.