Android-jetpack Compose
room db 실행 시 compile은 잘되나 튕김 발생
slow333
2023. 4. 24. 13:09
room database 구성 시 애러... 개고생함
gradle 설정 이 문제임
project gradle 설정
buildscript {
ext {
compose_ui_version = '1.3.3'
hilt_version = '2.44.2'
}
plugins {
id 'com.android.application' version '8.0.0' apply false
id 'com.android.library' version '8.0.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'org.jetbrains.kotlin.jvm' version '1.8.10' apply false
id "org.jetbrains.kotlin.kapt" version "1.8.10" apply false
id 'com.google.dagger.hilt.android' version '2.44.2' apply false
}
app gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
}
android {
.....
}
dependencies {
// viewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
// Hilt-dagger
//noinspection GradleDependency
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
// 여기 부터 ................................................
kapt 'androidx.hilt:hilt-compiler:1.0.0' // 이거 안하면 컴파일은 되나 애러남
implementation "androidx.hilt:hilt-navigation-compose:1.0.0" // 이거 안하면 컴파일은 되나 애러남
// 이거 하고 viewModel : NoteViewModel = hiltViewModel() 적어야함 !!!!!!!!!!!
// 여기 까지.............................................
// navigation
implementation "androidx.navigation:navigation-compose:2.5.3"
// Room
def room_version = "2.5.1"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$room_version")
implementation "androidx.room:room-ktx:$room_version"
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.6.4"
// implementation 'androidx.room:room-coroutines:2.1.0-alpha04'
implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.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'
implementation 'androidx.compose.material:material:1.4.2'
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' //
// animation
implementation("androidx.compose.animation:animation:1.4.2")
// coil
implementation("io.coil-kt:coil-compose:2.3.0")
//Icon expanded
implementation('androidx.compose.material:material-icons-extended:1.4.2')
}
kapt {
correctErrorTypes true
}
screen
fun NotesApp(navController: NavController, noteViewModel: NoteViewModel = hiltViewModel())
@Composable
fun NotesApp(navController: NavController, noteViewModel: NoteViewModel = hiltViewModel()){
val noteList = noteViewModel.noteList.collectAsState().value
NoteScreen(navController = navController, notes = noteList,
onSave = { noteViewModel.addNote(it) },
onRemove = { noteViewModel.removeNote(it) })
}
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "UnrememberedMutableState")
@Composable
fun NoteScreen(
navController: NavController,
notes: List<Note>,
onRemove: (Note) -> Unit,
onSave: (Note) -> Unit) {
Scaffold(topBar = {
MyTopAppBar( navController = navController, isMain = false, title = "Note App"
) }) {
BasicMySurface(modifier = Modifier
.fillMaxSize()
.padding(top = 76.dp),
borderStroke = BorderStroke(0.dp, Color.Transparent)
) {
Column(modifier = Modifier
.fillMaxSize()
.padding(8.dp),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally ) {
val title = rememberSaveable() { mutableStateOf("") }
val description = rememberSaveable() { mutableStateOf("") }
val valid = title.value.isNotEmpty() && description.value.trim().isNotEmpty()
val keyboardController = LocalSoftwareKeyboardController.current
TextField( modifier = Modifier.fillMaxWidth().padding(6.dp),
value = title.value, onValueChange = { title.value = it },
label = { Text(text = "title") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text, imeAction = ImeAction.Next),
keyboardActions = KeyboardActions.Default )
TextField( modifier = Modifier.fillMaxWidth().padding(6.dp),
value = description.value, onValueChange = { description.value = it },
label = { Text(text = "description") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text, imeAction = ImeAction.Done),
keyboardActions = KeyboardActions.Default )
Button(onClick = {
if (valid) {
onSave(Note(title = title.value, myNote = description.value))
title.value =""
description.value = ""
keyboardController?.hide() }
}) {
Text(text = "SAVE", Modifier.padding(10.dp),
style = MaterialTheme.typography.bodyLarge ) }
Divider(Modifier.padding(vertical = 10.dp))
LazyColumn() {
items(notes) { note ->
NoteCard(note = note, onRemove = {
onRemove(it) })
}
}
}
}
}
}