# Kotlin Android ๊ฐ๋ฐ ๊ฐ์ด๋
## ํ์ต ๋ชฉํ
Kotlin๊ณผ Jetpack Compose๋ก Android ์ฑ์ ๊ฐ๋ฐํฉ๋๋ค.
## 1. Kotlin์ด๋?
JetBrains๊ฐ ๋ง๋ ํ๋์ JVM ์ธ์ด์
๋๋ค.
- Java์ 100% ํธํ
- ๊ฐ๊ฒฐํ๊ณ ์์ ํ ๋ฌธ๋ฒ
- Null Safety ์ง์
- ์ฝ๋ฃจํด์ผ๋ก ๋น๋๊ธฐ ์ฒ๋ฆฌ
## 2. Android Studio ์ค์น
1. developer.android.com์์ ๋ค์ด๋ก๋
2. SDK ์ค์น
3. ์ ํ๋ก์ ํธ ์์ฑ (Empty Compose Activity)
## 3. ๊ธฐ๋ณธ ๋ฌธ๋ฒ
```kotlin
// ๋ณ์
val name = "Kim" // ๋ถ๋ณ (๊ถ์ฅ)
var age = 25 // ๊ฐ๋ณ
// ํ์
๋ช
์
val score: Int = 100
val price: Double = 19.99
// Null Safety
var email: String? = null
val length = email?.length ?: 0
// ์กฐ๊ฑด๋ฌธ
val result = if (age >= 18) "์ฑ์ธ" else "๋ฏธ์ฑ๋
"
// when (switch ๋์ฒด)
when (score) {
in 90..100 -> "A"
in 80..89 -> "B"
else -> "C"
}
```
## 4. ํจ์
```kotlin
fun greet(name: String): String {
return "Hello, $name!"
}
// ๋จ์ผ ํํ์
fun add(a: Int, b: Int) = a + b
// ๊ธฐ๋ณธ๊ฐ
fun greet(name: String = "Guest") = "Hello, $name"
// ๋๋ค
val double = { x: Int -> x * 2 }
```
## 5. ํด๋์ค
```kotlin
// ๊ธฐ๋ณธ ํด๋์ค
class User(val name: String, var age: Int) {
fun greet() = "Hello, $name"
}
// ๋ฐ์ดํฐ ํด๋์ค (equals, hashCode, copy ์๋ ์์ฑ)
data class Product(val id: Int, val name: String, val price: Double)
// ์ฌ์ฉ
val user = User("Kim", 25)
val product = Product(1, "Laptop", 999.99)
val copy = product.copy(price = 899.99)
```
## 6. Jetpack Compose ๊ธฐ์ด
```kotlin
@Composable
fun Greeting(name: String) {
Text(
text = "Hello, $name!",
fontSize = 24.sp,
color = Color.Blue
)
}
@Preview
@Composable
fun PreviewGreeting() {
Greeting("Android")
}
```
## 7. ์ํ ๊ด๋ฆฌ
```kotlin
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("Count: $count", fontSize = 24.sp)
Button(onClick = { count++ }) {
Text("Add")
}
}
}
```
## 8. ๋ ์ด์์
```kotlin
// ์ธ๋ก ๋ฐฐ์น
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text("Title")
Text("Subtitle")
}
// ๊ฐ๋ก ๋ฐฐ์น
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth()
) {
Text("Left")
Text("Right")
}
// ๊ฒน์น๊ธฐ
Box(contentAlignment = Alignment.Center) {
Image(painter = painterResource(R.drawable.bg), contentDescription = null)
Text("Overlay")
}
```
## 9. ๋ฆฌ์คํธ
```kotlin
@Composable
fun ProductList(products: List<Product>) {
LazyColumn(
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(products) { product ->
ProductCard(product)
}
}
}
@Composable
fun ProductCard(product: Product) {
Card(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(16.dp)) {
Text(product.name, fontWeight = FontWeight.Bold)
Text("$${product.price}")
}
}
}
```
## 10. ๋ค๋น๊ฒ์ด์
```kotlin
@Composable
fun AppNavigation() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "home") {
composable("home") {
HomeScreen(onNavigate = { navController.navigate("detail/$it") })
}
composable("detail/{id}") { backStackEntry ->
val id = backStackEntry.arguments?.getString("id")
DetailScreen(id = id)
}
}
}
```
## 11. Retrofit API ์ฐ๋
```kotlin
// API ์ธํฐํ์ด์ค
interface ApiService {
@GET("users")
suspend fun getUsers(): List<User>
@POST("users")
suspend fun createUser(@Body user: User): User
}
// Retrofit ์ค์
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val api = retrofit.create(ApiService::class.java)
// ViewModel์์ ์ฌ์ฉ
class UserViewModel : ViewModel() {
private val _users = MutableStateFlow<List<User>>(emptyList())
val users: StateFlow<List<User>> = _users
fun loadUsers() {
viewModelScope.launch {
_users.value = api.getUsers()
}
}
}
```
## 12. ์ฑ ๋ฐฐํฌ
1. Build > Generate Signed Bundle / APK
2. ํค์คํ ์ด ์์ฑ ๋๋ ์ ํ
3. release ๋น๋ ์์ฑ
4. Google Play Console ์
๋ก๋
5. ์คํ ์ด ๋ฑ๋ก ์ ๋ณด ์์ฑ
6. ์ฌ์ฌ ์ ์ถ
## ์ถ์ฒ ํ์ต ์์
1. Kotlin ๊ธฐ๋ณธ ๋ฌธ๋ฒ
2. Compose UI ๊ธฐ์ด
3. ์ํ ๊ด๋ฆฌ
4. ๋ค๋น๊ฒ์ด์
5. API ์ฐ๋ ๋ฐ ๋ฐฐํฌ