I have set up an "Empty Activity" project in Android Studio (API 33). I am trying to experiment with DatePicker, but adding an import line gives and "Unresolved reference: DatePicker" error.
DatePicker does not show up as an option in compose.material3 in the autocomplete dropdown list.
Is there something else I also need to import? Do I need to modify my build scrips?
The code (MainActivity.kt)
package id.ruff.dates
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import id.ruff.dates.ui.theme.DatesTheme
import androidx.compose.material3.DatePicker // the only line I have changed
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DatesTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
Greeting("Android")
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
DatesTheme {
Greeting("Android")
}
}
Gradle Script (build.gradle)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'id.ruff.dates'
compileSdk 33
defaultConfig {
applicationId "id.ruff.dates"
minSdk 33
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.5.1'
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
}
If you look at the BOM to library version mapping documentation and select the BOM you are using (2022.10.00
), you'll see:
androidx.compose.material3:material3 | 1.0.0
So you're using Compose Material3 1.0.0 when you use implementation 'androidx.compose.material3:material3'
But if you look at the DatePicker
documentation, you'll see
Added in
1.2.0-alpha05
So you won't be able to access an API added in Compose Material3 1.2 using a 1.0.0 version. Since Compose Material 1.2 isn't stable yet, you'll need to explicitly use the alpha dependency if you want to use this API, rather than relying on the version provided by the BOM (since the BOM only points to stable versions):
implementation 'androidx.compose.material3:material3:1.2.0-alpha05'
Note that Compose Material3 1.2 depends on Compose 1.6 alphas as well (since both are still under active development), so updating to the alpha version of Compose Material3 may cause you to have to upgrade other dependencies as well.