728x90
반응형
구글에서 koin 검색하기 (https://insert-koin.io/)
사이트에 들어가면 아래와 같이 문구가 나오고 사용법에 대한 간략한 예시가 있다.
Koin - a smart Kotlin injection library to keep you focused on your app, not on your tools
이전 retrofit 적용하기 프로젝트에 koin을 적용해보자
github에 접근하여 버전 확인하고 app gradle에 추가(https://github.com/InsertKoinIO/koin)
implementation 'io.insert-koin:koin-core:3.2.0'
testImplementation 'io.insert-koin:koin-test:3.2.0'
코틀린 파일하나 만들고 네트워크 모듈 작성
더보기
val networkModule = module {
factory {
getHttpLoggingInterceptor()
}
factory {
getHttpClient(get())
}
factory {
getGsonConverterFactory()
}
factory {
getRetrofit(get(), get())
}
single {
getNetworkService(get())
}
}
fun getHttpLoggingInterceptor(): Interceptor = HttpLoggingInterceptor().apply {
setLevel(HttpLoggingInterceptor.Level.BODY)
}
fun getHttpClient(interceptor: Interceptor) = OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()
fun getGsonConverterFactory(): Converter.Factory = GsonConverterFactory.create()
fun getRetrofit(factory: Converter.Factory, client: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(factory)
.client(client)
.build()
fun getNetworkService(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java)
Application 작성
더보기
class App : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
modules(listOf(networkModule))
}
}
}
AndroidManifest에 Application추가
MainFragment 수정
검색만 해보고 이해가 안되던 부분이 실제 적용하니 생각보다 쉽게 적용이 가능했다.
반응형
'Development > Android' 카테고리의 다른 글
java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent) (0) | 2022.12.02 |
---|---|
[android] Exception occurred while executing 'install-incremental' (0) | 2022.07.20 |
[android] retrofit2 적용하기 (0) | 2022.06.22 |
[android] 사용하지 않는 리소스(Unused resources) 삭제 (0) | 2022.05.11 |
[android] touch event flow (0) | 2022.05.06 |
댓글