Skip to content

Commit 4c56aa0

Browse files
Update Quickstarts
1 parent 49b2d1a commit 4c56aa0

File tree

10 files changed

+522
-322
lines changed

10 files changed

+522
-322
lines changed

docs/quickstart/android-annotations.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
11
---
2-
title: Android - Annotations
2+
title: Android & Annotations
33
---
44

55
> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
6-
> You need around __10/15 min__ to do the tutorial.
6+
> You need around __10 min__ to do the tutorial.
7+
8+
:::note
9+
update - 2024-10-21
10+
:::
711

812
## Get the code
913

1014
:::info
11-
[The source code is available at on Github](https://github.com/InsertKoinIO/koin-getting-started/tree/main/android)
15+
[The source code is available at on Github](https://github.com/InsertKoinIO/koin-getting-started/tree/main/android-annotations)
1216
:::
1317

1418
## Gradle Setup
1519

16-
Let's configure the KSP Plugin like this:
20+
Let's configure the KSP Plugin like this, and the following dependencies:
1721

1822
```groovy
19-
apply plugin: 'com.google.devtools.ksp'
20-
21-
android {
22-
sourceSets {
23-
main.java.srcDirs += 'src/main/kotlin'
24-
test.java.srcDirs += 'src/test/kotlin'
25-
}
26-
// For KSP
27-
applicationVariants.configureEach { variant ->
28-
kotlin.sourceSets {
29-
getByName(name) {
30-
kotlin.srcDir("build/generated/ksp/${variant.name}/kotlin")
31-
}
32-
}
33-
}
23+
plugins {
24+
alias(libs.plugins.ksp)
3425
}
35-
```
36-
37-
Add the Koin Android dependency like below:
3826
39-
```groovy
4027
dependencies {
41-
// Koin
42-
implementation "io.insert-koin:koin-android:$koin_version"
43-
implementation "io.insert-koin:koin-annotations:$koin_ksp_version"
44-
ksp "io.insert-koin:koin-ksp-compiler:$koin_ksp_version"
28+
// ...
29+
30+
implementation(libs.koin.annotations)
31+
ksp(libs.koin.ksp)
4532
}
46-
```
4733
34+
// Compile time check
35+
ksp {
36+
arg("KOIN_CONFIG_CHECK","true")
37+
}
38+
```
4839

40+
:::note
41+
See `libs.versions.toml` for current versions
42+
:::
4943

5044
## Application Overview
5145

@@ -250,18 +244,15 @@ dependencies {
250244

251245
### Checking your modules
252246

253-
The `verify()` function allow to verify the given Koin modules:
247+
The `androidVerify()` function allow to verify the given Koin modules:
254248

255249
```kotlin
256250
class CheckModulesTest : KoinTest {
257251

258252
@Test
259253
fun checkAllModules() {
260254

261-
AppModule().module.verify(
262-
extraTypes = listOf(
263-
SavedStateHandle::class
264-
))
255+
AppModule().module.androidVerify()
265256
}
266257
}
267258
```

docs/quickstart/android-compose.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ title: Android - Jetpack Compose
55
> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
66
> You need around __10 min__ to do the tutorial.
77
8+
:::note
9+
update - 2024-10-21
10+
:::
11+
812
## Get the code
913

1014
:::info
@@ -73,7 +77,7 @@ Let's declare our first component. We want a singleton of `UserRepository`, by c
7377

7478
```kotlin
7579
val appModule = module {
76-
single<UserRepository> { UserRepositoryImpl() }
80+
singleOf(::UserRepositoryImpl) bind UserRepository::class
7781
}
7882
```
7983

@@ -95,12 +99,12 @@ class UserViewModel(private val repository: UserRepository) : ViewModel() {
9599

96100
> UserRepository is referenced in UserViewModel's constructor
97101
98-
We declare `UserViewModel` in our Koin module. We declare it as a `viewModel` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
102+
We declare `UserViewModel` in our Koin module. We declare it as a `viewModelOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
99103

100104
```kotlin
101105
val appModule = module {
102-
single<UserRepository> { UserRepositoryImpl() }
103-
viewModel { MyViewModel(get()) }
106+
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
107+
viewModelOf(::UserViewModel)
104108
}
105109
```
106110

@@ -125,7 +129,7 @@ The `koinViewModel` function allows us to retrieve a ViewModel instances, create
125129

126130
### The `UserStateHolder` class
127131

128-
Let's write a ViewModel component to display a user:
132+
Let's write a State holder component to display a user:
129133

130134
```kotlin
131135
class UserStateHolder(private val repository: UserRepository) {
@@ -139,28 +143,28 @@ class UserStateHolder(private val repository: UserRepository) {
139143

140144
> UserRepository is referenced in UserViewModel's constructor
141145
142-
We declare `UserViewModel` in our Koin module. We declare it as a `viewModel` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
146+
We declare `UserStateHolder` in our Koin module. We declare it as a `factoryOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
143147

144148
```kotlin
145149
val appModule = module {
146-
single<UserRepository> { UserRepositoryImpl() }
147-
factory { UserStateHolder(get()) }
150+
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
151+
factoryOf(::UserStateHolder)
148152
}
149153
```
150154

151155
### Injecting UserStateHolder in Compose
152156

153-
The `UserViewModel` component will be created, resolving the `UserRepository` instance with it. To get it into our Activity, let's inject it with the `get()` function:
157+
The `UserStateHolder` component will be created, resolving the `UserRepository` instance with it. To get it into our Activity, let's inject it with the `koinInject()` function:
154158

155159
```kotlin
156160
@Composable
157-
fun FactoryInject(userName : String, presenter: UserStateHolder = get()){
161+
fun FactoryInject(userName : String, presenter: UserStateHolder = koinInject()){
158162
Text(text = presenter.sayHello(userName), modifier = Modifier.padding(8.dp))
159163
}
160164
```
161165

162166
:::info
163-
The `get` function allows us to retrieve a ViewModel instances, create the associated ViewModel Factory for you and bind it to the lifecycle
167+
The `koinInject` function allows us to retrieve a ViewModel instances, create the associated ViewModel Factory for you and bind it to the lifecycle
164168
:::
165169

166170

@@ -186,6 +190,24 @@ class MainApplication : Application(){
186190
The `modules()` function in `startKoin` load the given list of modules
187191
:::
188192

193+
While starting the Compose application we need to link Koin to our current Compose application, with `KoinAndroidContext`:
194+
195+
```kotlin
196+
class MainActivity : AppCompatActivity() {
197+
198+
override fun onCreate(savedInstanceState: Bundle?) {
199+
super.onCreate(savedInstanceState)
200+
setContent {
201+
MaterialTheme {
202+
KoinAndroidContext {
203+
App()
204+
}
205+
}
206+
}
207+
}
208+
}
209+
```
210+
189211
## Koin module: classic or constructor DSL?
190212

191213
Here is the Koin moduel declaration for our app:
@@ -215,11 +237,6 @@ We can ensure that our Koin configuration is good before launching our app, by v
215237
Add the Koin Android dependency like below:
216238

217239
```groovy
218-
// Add Maven Central to your repositories if needed
219-
repositories {
220-
mavenCentral()
221-
}
222-
223240
dependencies {
224241
225242
// Koin for Tests

docs/quickstart/android-viewmodel.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ title: Android - ViewModel
33
---
44

55
> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
6-
> You need around __10/15 min__ to do the tutorial.
6+
> You need around __10 min__ to do the tutorial.
7+
8+
:::note
9+
update - 2024-10-21
10+
:::
711

812
## Get the code
913

@@ -19,7 +23,7 @@ Add the Koin Android dependency like below:
1923
dependencies {
2024
2125
// Koin for Android
22-
implementation "io.insert-koin:koin-android:$koin_version"
26+
implementation("io.insert-koin:koin-android:$koin_version")
2327
}
2428
```
2529

@@ -73,7 +77,7 @@ Let's declare our first component. We want a singleton of `UserRepository`, by c
7377

7478
```kotlin
7579
val appModule = module {
76-
single<UserRepository> { UserRepositoryImpl() }
80+
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
7781
}
7882
```
7983

@@ -93,16 +97,15 @@ class UserViewModel(private val repository: UserRepository) : ViewModel() {
9397

9498
> UserRepository is referenced in UserViewModel`s constructor
9599
96-
We declare `UserViewModel` in our Koin module. We declare it as a `viewModel` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
100+
We declare `UserViewModel` in our Koin module. We declare it as a `viewModelOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
97101

98102
```kotlin
99103
val appModule = module {
100-
single<UserRepository> { UserRepositoryImpl() }
101-
viewModel { MyViewModel(get()) }
104+
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
105+
viewModelOf(::UserViewModel)
102106
}
103107
```
104108

105-
> The `get()` function allow to ask Koin to resolve the needed dependency.
106109

107110
## Injecting ViewModel in Android
108111

docs/quickstart/android.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ title: Android
33
---
44

55
> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
6-
> You need around __10/15 min__ to do the tutorial.
6+
> You need around __10 min__ to do the tutorial.
7+
8+
:::note
9+
update - 2024-10-21
10+
:::
711

812
## Get the code
913

@@ -19,7 +23,7 @@ Add the Koin Android dependency like below:
1923
dependencies {
2024
2125
// Koin for Android
22-
implementation "io.insert-koin:koin-android:$koin_version"
26+
implementation("io.insert-koin:koin-android:$koin_version")
2327
}
2428
```
2529

@@ -73,7 +77,7 @@ Let's declare our first component. We want a singleton of `UserRepository`, by c
7377

7478
```kotlin
7579
val appModule = module {
76-
single<UserRepository> { UserRepositoryImpl() }
80+
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
7781
}
7882
```
7983

@@ -93,12 +97,12 @@ class UserPresenter(private val repository: UserRepository) {
9397

9498
> UserRepository is referenced in UserPresenter`s constructor
9599
96-
We declare `UserPresenter` in our Koin module. We declare it as a `factory` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
100+
We declare `UserPresenter` in our Koin module. We declare it as a `factoryOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
97101

98102
```kotlin
99103
val appModule = module {
100-
single<UserRepository> { UserRepositoryImpl() }
101-
factory { MyPresenter(get()) }
104+
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
105+
factoryOf(::UserStateHolder)
102106
}
103107
```
104108

@@ -151,7 +155,7 @@ The `modules()` function in `startKoin` load the given list of modules
151155

152156
## Koin module: classic or constructor DSL?
153157

154-
Here is the Koin module declaration for our app:
158+
Here is the Koin moduel declaration for our app:
155159

156160
```kotlin
157161
val appModule = module {

0 commit comments

Comments
 (0)