Skip to content

Android ライブラリの統合 ​

このページでは、大半の Android アプリで使用されるライブラリ(OkHttp を組み合わせた Retrofit、Room、JSON シリアライザー)およびレイヤードアプリケーション構造に対応する完全な Koin のセットアップ方法を説明します。ライブラリのクラスはビルダーから生成されるため、プロバイダー関数(provider function)を使って宣言します。その仕組み(create(::function) やアノテーション付き関数)については Definitions(定義) で解説しています。このページでは Android 向けのセットアップのみを取り上げます。

Retrofit と OkHttp ​

kotlin
interface ApiService {
    @GET("users/{id}")
    suspend fun getUser(@Path("id") id: String): User
}
kotlin
import org.koin.plugin.module.dsl.create

fun okHttpClient(): OkHttpClient =
    OkHttpClient.Builder()
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .addInterceptor(HttpLoggingInterceptor().apply {
            level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
        })
        .build()

fun retrofit(client: OkHttpClient, json: Json): Retrofit =
    Retrofit.Builder()
        .baseUrl(BuildConfig.API_URL)
        .client(client)
        .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
        .build()

fun apiService(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java)

val networkModule = module {
    single { create(::okHttpClient) }
    single { create(::retrofit) }
    single { create(::apiService) }
}
kotlin
@Module
class NetworkModule {

    @Singleton
    fun okHttpClient(): OkHttpClient =
        OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
            })
            .build()

    @Singleton
    fun retrofit(client: OkHttpClient, json: Json): Retrofit =
        Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(client)
            .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
            .build()

    @Singleton
    fun apiService(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java)
}
kotlin
val networkModule = module {
    single {
        OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
            })
            .build()
    }
    single {
        Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(get())
            .addConverterFactory(get<Json>().asConverterFactory("application/json".toMediaType()))
            .build()
    }
    single { get<Retrofit>().create(ApiService::class.java) }
}

OkHttpClient と Retrofit は single として宣言します。それぞれがコネクションプールとスレッドプールを保持するため、複数インスタンスを生成するとリソースの浪費につながります。BuildConfig.API_URL はビルドタイプごとに定義する buildConfigField です。Json インスタンスは次のセクションから取得します。

JSON シリアライゼーション ​

kotlin
fun json(): Json = Json {
    ignoreUnknownKeys = true
    explicitNulls = false
}

val serializationModule = module {
    single { create(::json) }
}
kotlin
@Module
class SerializationModule {

    @Singleton
    fun json(): Json = Json {
        ignoreUnknownKeys = true
        explicitNulls = false
    }
}
kotlin
val serializationModule = module {
    single {
        Json {
            ignoreUnknownKeys = true
            explicitNulls = false
        }
    }
}

同様のパターンは Gson(single { create(::gson) } と fun gson(): Gson = GsonBuilder().create())や Moshi にも適用できます。共有インスタンスを 1 つにすることで、シリアライザーの設定を一元管理できます。

Room ​

kotlin
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}
kotlin
fun appDatabase(context: Context): AppDatabase =
    Room.databaseBuilder(context, AppDatabase::class.java, "app-database").build()

fun userDao(database: AppDatabase): UserDao = database.userDao()

val databaseModule = module {
    single { create(::appDatabase) }
    single { create(::userDao) }
}
kotlin
@Module
class DatabaseModule {

    @Singleton
    fun appDatabase(context: Context): AppDatabase =
        Room.databaseBuilder(context, AppDatabase::class.java, "app-database").build()

    @Singleton
    fun userDao(database: AppDatabase): UserDao = database.userDao()
}
kotlin
val databaseModule = module {
    single {
        Room.databaseBuilder(androidContext(), AppDatabase::class.java, "app-database").build()
    }
    single { get<AppDatabase>().userDao() }
}

データベースは single として宣言します。RoomDatabase のインスタンス生成はコストが高く、Room のドキュメントでもプロセスごとにデータベースにつき 1 つのインスタンスとすることが求められています。ここでの Context は起動時に androidContext() によって提供される Application です(Android での Koin の起動 を参照)。DAO は軽量であり、Room は呼び出しごとに同じインスタンスを返すため、single と factory のどちらでも動作します。

WorkManager ​

Worker は専用の WorkerFactory を通じて依存関係を取得します。セットアップと worker 定義については WorkManager を参照してください。

レイヤードアーキテクチャ ​

典型的な Android アプリは、データレイヤー(ネットワーク、データベース、リポジトリ)、ドメインレイヤー(ユースケース)、UI レイヤー(ViewModel)で構成されます。各レイヤーはそのライフタイムに応じた定義タイプを使用します。

レイヤークラス定義
データOkHttpClient、Retrofit、AppDatabase、データソース、リポジトリsingle
ドメインユースケース(状態を持たないもの)factory
UIViewModelviewModel
kotlin
val dataModule = module {
    includes(networkModule, databaseModule, serializationModule)
    single<UserRemoteDataSource>()
    single<UserLocalDataSource>()
    single<UserRepositoryImpl>() bind UserRepository::class
}

val domainModule = module {
    factory<GetUserUseCase>()
}

val uiModule = module {
    viewModel<UserViewModel>()
}
kotlin
@Singleton
class UserRemoteDataSource(private val api: ApiService)

@Singleton
class UserLocalDataSource(private val dao: UserDao)

@Singleton
class UserRepositoryImpl(
    private val remote: UserRemoteDataSource,
    private val local: UserLocalDataSource,
) : UserRepository

@Factory
class GetUserUseCase(private val repository: UserRepository) {
    suspend operator fun invoke(userId: String): User = repository.getUser(userId)
}

@KoinViewModel
class UserViewModel(
    private val getUser: GetUserUseCase,
    private val savedStateHandle: SavedStateHandle,
) : ViewModel()
kotlin
val dataModule = module {
    includes(networkModule, databaseModule, serializationModule)
    singleOf(::UserRemoteDataSource)
    singleOf(::UserLocalDataSource)
    singleOf(::UserRepositoryImpl) bind UserRepository::class
}

val domainModule = module {
    factoryOf(::GetUserUseCase)
}

val uiModule = module {
    viewModelOf(::UserViewModel)
}

ViewModel はユースケースに依存し、ユースケースは UserRepository インターフェースに依存しており、UserRepositoryImpl の存在を知っているのはデータモジュールのみです。テスト時にデータソースを差し替えるには、UserRepository のバインディングをオーバーライドします(単体テスト(Unit Testing) を参照)。各レイヤーが別個の Gradle モジュールに分割されている場合は、マルチモジュールアプリ(Multi-Module Apps) を参照してください。