Jetpack Compose is Android’s modern declarative UI toolkit. Here’s how to set it up from scratch or add it to an existing project.
Add Compose to a new project
In Android Studio: File → New → New Project → Empty Compose Activity. This creates a project with Compose pre-configured.
Add Compose to an existing project
1. Configure the build
In your project-level build.gradle.kts:
plugins {
id("com.android.application") version "8.5.0" apply false
id("org.jetbrains.kotlin.android") version "2.0.0" apply false
id("org.jetbrains.kotlin.plugin.compose") version "2.0.0" apply false
}In your app-level build.gradle.kts:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.compose")
}
android {
namespace = "com.example.myapp"
compileSdk = 35
defaultConfig {
applicationId = "com.example.myapp"
minSdk = 24
targetSdk = 35
versionCode = 1
versionName = "1.0"
}
buildFeatures {
compose = true
}
kotlin {
jvmToolchain(17)
}
}
dependencies {
// Compose BOM — manages all Compose versions
val composeBom = platform("androidx.compose:compose-bom:2024.06.00")
implementation(composeBom)
// Core Compose
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.foundation:foundation")
// Activity integration
implementation("androidx.activity:activity-compose:1.9.0")
// Tooling (debug only)
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}Your first composable
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyTheme {
Greeting(name = "Android")
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello, $name!",
modifier = modifier
)
}
// Preview in Android Studio
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MyTheme {
Greeting(name = "World")
}
}Compose BOM
The Compose BOM (Bill of Materials) manages compatible versions for all Compose libraries. When you use the BOM, you don’t specify individual library versions:
val composeBom = platform("androidx.compose:compose-bom:2024.06.00")
implementation(composeBom)
implementation("androidx.compose.material3:material3") // version managed by BOMVerify your setup
# Check for dependency issues
./gradlew :app:dependencies | grep compose
# Build the project
./gradlew assembleDebug
# Run on device
./gradlew installDebugIf Compose is set up correctly, you should see @Composable functions render in the preview and on the device.