or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application-setup.mdcontext-access.mddependency-injection.mdindex.mdmodule-management.mdscope-management.md

application-setup.mddocs/

0

# Application Setup

1

2

Core composable functions for setting up Koin dependency injection within Compose applications, providing various setup approaches from basic configuration to multiplatform and isolated contexts.

3

4

## Capabilities

5

6

### KoinApplication

7

8

Start a new Koin Application context and setup Compose context integration. Throws an error if Koin's default context is already set.

9

10

```kotlin { .api }

11

/**

12

* Start a new Koin Application context and setup Compose context

13

* if Koin's Default Context is already set, throw an error

14

*

15

* @param application - Koin Application declaration lambda

16

* @param content - following compose function

17

* @throws KoinApplicationAlreadyStartedException

18

*/

19

@Composable

20

fun KoinApplication(

21

application: KoinAppDeclaration,

22

content: @Composable () -> Unit

23

)

24

```

25

26

**Usage Examples:**

27

28

```kotlin

29

import org.koin.compose.KoinApplication

30

import org.koin.dsl.module

31

32

val appModule = module {

33

single<ApiService> { ApiServiceImpl() }

34

single<Repository> { RepositoryImpl(get()) }

35

}

36

37

@Composable

38

fun App() {

39

KoinApplication(

40

application = {

41

modules(appModule)

42

// Additional Koin configuration

43

}

44

) {

45

MainContent()

46

}

47

}

48

```

49

50

### KoinMultiplatformApplication

51

52

Start a new Koin Application context with automatic multiplatform configuration. Handles platform-specific context binding (Android) and logger setup automatically.

53

54

```kotlin { .api }

55

/**

56

* Start a new Koin Application context, configure default context binding (android) & logger, setup Compose context

57

* if Koin's Default Context is already set, throw an error

58

*

59

* Call composeMultiplatformConfiguration to help prepare/anticipate context setup, and avoid to have different configuration in KMP app

60

* this function takes care to setup Android context (androidContext, androidLogger) for you

61

*

62

* @param config - Koin Application Configuration (use koinConfiguration { } to declare your Koin application)

63

* @param logLevel - KMP active logger (androidLogger or printLogger)

64

* @param content - following compose function

65

* @throws KoinApplicationAlreadyStartedException

66

*/

67

@Composable

68

@KoinExperimentalAPI

69

fun KoinMultiplatformApplication(

70

config: KoinConfiguration,

71

logLevel: Level = Level.INFO,

72

content: @Composable () -> Unit

73

)

74

```

75

76

**Usage Examples:**

77

78

```kotlin

79

import org.koin.compose.KoinMultiplatformApplication

80

import org.koin.core.logger.Level

81

import org.koin.dsl.koinConfiguration

82

83

@Composable

84

fun App() {

85

KoinMultiplatformApplication(

86

config = koinConfiguration {

87

modules(appModule)

88

},

89

logLevel = Level.DEBUG

90

) {

91

MainContent()

92

}

93

}

94

```

95

96

### KoinIsolatedContext

97

98

Provides an isolated Koin context that can be used independently of the global Koin instance. Useful for testing or when multiple Koin contexts are needed.

99

100

```kotlin { .api }

101

/**

102

* Provides Koin Isolated context to be setup into LocalKoinApplication & LocalKoinScope via CompositionLocalProvider,

103

* to be used by child Composable.

104

*

105

* This allows to use an isolated context, directly in all current Composable API

106

*

107

* Koin isolated context has to created with koinApplication() function, storing the instance in a static field

108

*

109

* @param context - Koin isolated context

110

* @param content - child Composable

111

*/

112

@Composable

113

fun KoinIsolatedContext(

114

context: KoinApplication,

115

content: @Composable () -> Unit

116

)

117

```

118

119

**Usage Examples:**

120

121

```kotlin

122

import org.koin.compose.KoinIsolatedContext

123

import org.koin.dsl.koinApplication

124

125

// Create isolated Koin application

126

val isolatedKoin = koinApplication {

127

modules(testModule)

128

}

129

130

@Composable

131

fun TestComponent() {

132

KoinIsolatedContext(

133

context = isolatedKoin

134

) {

135

// This content uses the isolated Koin context

136

ComponentUsingKoin()

137

}

138

}

139

```

140

141

### KoinApplicationPreview

142

143

Lightweight Koin application setup specifically designed for Compose previews. This function is lighter than KoinApplication and allows parallel recomposition in Android Studio.

144

145

```kotlin { .api }

146

/**

147

* Composable Function to run a local Koin application and to help run Compose preview

148

* This function is lighter than KoinApplication, and allow parallel recomposition in Android Studio

149

*

150

* @param application - Koin application config

151

* @param content

152

*/

153

@Composable

154

fun KoinApplicationPreview(

155

application: KoinAppDeclaration,

156

content: @Composable () -> Unit

157

)

158

```

159

160

**Usage Examples:**

161

162

```kotlin

163

import androidx.compose.ui.tooling.preview.Preview

164

import org.koin.compose.KoinApplicationPreview

165

import org.koin.dsl.module

166

167

val previewModule = module {

168

single<ApiService> { MockApiService() }

169

}

170

171

@Preview

172

@Composable

173

fun UserScreenPreview() {

174

KoinApplicationPreview(

175

application = {

176

modules(previewModule)

177

}

178

) {

179

UserScreen()

180

}

181

}

182

```

183

184

### KoinContext (Deprecated)

185

186

Legacy function for using existing Koin context. This function is deprecated and should be replaced with proper Koin setup.

187

188

```kotlin { .api }

189

/**

190

* Use Compose with current existing Koin context, by default 'KoinPlatform.getKoin()'

191

*

192

* @param content - following compose function

193

*/

194

@Composable

195

@Deprecated("KoinContext is not needed anymore. This can be removed. Compose Koin context is setup with StartKoin()")

196

fun KoinContext(

197

context: Koin = retrieveDefaultInstance(),

198

content: @Composable () -> Unit

199

)

200

```

201

202

## Platform-Specific Configuration

203

204

### Android Platform

205

206

Automatically configures Android context and logger:

207

208

```kotlin

209

@Composable

210

internal actual fun composeMultiplatformConfiguration(

211

loggerLevel: Level,

212

config: KoinConfiguration

213

): KoinConfiguration {

214

val appContext = LocalContext.current.applicationContext

215

return koinConfiguration {

216

androidContext(appContext)

217

androidLogger(loggerLevel)

218

includes(config)

219

}

220

}

221

```

222

223

### JVM Platform

224

225

Uses print logger for desktop applications:

226

227

```kotlin

228

@Composable

229

internal actual fun composeMultiplatformConfiguration(

230

loggerLevel: Level,

231

config: KoinConfiguration

232

): KoinConfiguration {

233

return koinConfiguration {

234

printLogger(loggerLevel)

235

includes(config)

236

}

237

}

238

```

239

240

### JavaScript/WebAssembly/Native

241

242

Similar to JVM, uses print logger for web and native environments.

243

244

## Configuration Types

245

246

```kotlin { .api }

247

// From koin-core

248

interface KoinConfiguration {

249

fun modules(vararg modules: Module): KoinConfiguration

250

fun modules(modules: List<Module>): KoinConfiguration

251

// Platform-specific extensions added automatically

252

}

253

254

// From koin-dsl

255

typealias KoinAppDeclaration = KoinApplication.() -> Unit

256

257

// Logger levels

258

enum class Level {

259

NONE, ERROR, INFO, DEBUG

260

}

261

```

262

263

## Error Handling

264

265

- **KoinApplicationAlreadyStartedException**: Thrown when trying to start Koin when it's already started

266

- **Configuration errors**: Platform-specific setup failures (e.g., missing Android context)

267

- **Module loading errors**: Issues with dependency resolution during module loading