0Pricing
Jetpack Compose Academy · レッスン

DIの理由:テスト可能で疎結合なコード

Hiltが解決する問題を確認します。

「DIの理由:テスト可能で疎結合なコード」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What Is Dependency Injection?

Dependency injection means a class receives the objects it needs from the outside, instead of building them itself. Less wiring, more focus. 🔌

The Problem: new Everywhere

When a class calls new on its own dependencies, it gets locked to those exact types. Changing one thing forces you to edit many files.

class Repo {
    private val api = ApiService() // hard-wired
}

Inject Instead of Construct

Pass dependencies through the constructor and let something else create them. Now your class only states what it needs, not how to build it.

class Repo(private val api: ApiService) {
    // api is given to us
}

Decoupling Made Real

With injection your code depends on an interface, not a concrete class. You can swap the real one for another without touching the consumer.

interface ApiService { fun load(): String }
class Repo(private val api: ApiService)

Testability Is the Big Win

In tests you simply pass a fake dependency. No network, no database, no surprises, just fast and predictable checks. ✅

val fake = object : ApiService { override fun load() = "hi" }
val repo = Repo(fake)

Manual DI Gets Tedious

You can inject by hand, but wiring deep graphs means writing lots of boilerplate. Every new dependency ripples up through the constructors.

val api = ApiService()
val repo = Repo(api)
val vm = MyViewModel(repo) // by hand

Enter Hilt

Hilt is Android's recommended DI library. It generates the wiring for you at compile time, so you describe needs and Hilt builds the graph.

Hilt Sits on Top of Dagger

Hilt is built on Dagger but adds Android-aware defaults. You get compile-time safety with far less setup than raw Dagger.

Mark Your Application

Hilt starts at the app root. Annotate your Application class with @HiltAndroidApp to generate the top-level dependency container.

@HiltAndroidApp
class MyApp : Application()

Inject With @Inject

Add @Inject to a constructor and Hilt learns how to create that type. From then on it can supply it anywhere it is needed.

class Repo @Inject constructor(private val api: ApiService)

Compile-Time Safety

If a dependency is missing, Hilt fails at compile time, not at runtime. You catch wiring mistakes before the app ever ships. 🛡️

Quick Check

Which benefit best captures why we use dependency injection?

Recap: Why DI Matters

You learned that DI hands a class its dependencies, decoupling code and making tests trivial. Hilt automates that wiring at compile time. 🎉

よくある質問

「DIの理由:テスト可能で疎結合なコード」レッスンは無料ですか?

はい。「DIの理由:テスト可能で疎結合なコード」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「DIの理由:テスト可能で疎結合なコード」で何を学びますか?

Hiltが解決する問題を確認します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「DIの理由:テスト可能で疎結合なコード」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. DIの理由:テスト可能で疎結合なコード
  2. モジュール、@Provides、@Binds
  3. hiltViewModelと@HiltViewModel
  4. スコープとコンポーネントのライフタイム
← Jetpack Compose Academyに戻る