ApplicationComponent 実装の変遷 - Dagger2

2022-03-16 追記: 新しいDagger記事は以下リンクから

👉 MVVM で Hilt のパターン化 💉  

--------

へん‐せん【変遷】
[名](スル)時の流れとともに移り変わること。「歌もまた時代につれて変遷する」

Dagger て分かりづらいです。

タイトルがすでに謎ですが、以下のような実装のことを指しています。

アプリケーションコンテキストをオブジェクトグラフに追加する

ApplicationComponent とアプリケーションコンテキストの設定

アプリケーションコンテキスト を依存先として公開する

これまで数年に渡って変化し続けてるそんな必須の実装項目です。

ネット上をただ検索するだけでは、古い記事に最新の実装記述が埋没しています。

Dagger 2.9 以前( - 2017/02/04)


@Component(modules = [ApplicationModule::class, ...])
interface ApplicationComponent {
  ...
}


@Module
class ApplicationModule(private val applicationContext: Context) {
  @Provides fun provideApplicationContext() = applicationContext
}

これは Kotlin 記述で簡略化できます。


@Module
class ApplicationModule(@get:Provides val applicationContext: Context)


DaggerApplicationComponent
  .builder()
  .applicationModule(ApplicationModule(applicationContext))
  .build()

2.9 (2017/02/04 - ) @BindsInstance

👉 Release Dagger 2.9 · google/dagger 

We create a module that receives the application context as a constructor argument, and we create a provide method that exposes it. This works great, but then we can't have static @Provides methods anymore. And besides that, this strategy is actually going against the docs that are pretty explicit when it comes to this:

@BindsInstance methods should be preferred to writing a @Module with constructor arguments and immediately providing those values.

👉 User's Guide 


@Component(modules = ...)
interface ApplicationComponent {
  @Component.Builder
  interface Builder {
    @BindsInstance 
    fun applicationContext(applicationContext: Context): Builder
    fun build(): ApplicationComponent
  }
  ...
}


DaggerApplicationComponent
  .builder()
  .applicationContext(applicationContext)
  .build()

2.22 (2019/04/03 - ) @Component.Factory

👉 Release Dagger 2.22 · google/dagger 


@Component(modules = ...)
interface ApplicationComponent {
  @Component.Factory
  interface Factory {
    fun create(@BindsInstance applicationContext: Context): ApplicationComponent
  }
  ...
}


DaggerApplicationComponent
  .factory()
  .create(applicationContext)

まとめ

最近のコード記述を理解するには、少しだけ

「成り立ちを遡ってみる」

と理解しやすいことが多いように思います。

👉 Releases · google/dagger 
👉 Dagger 2 on Android: the shiny new @Component.Factory 

2022-03-16 追記: 新しいDagger記事は以下リンクから

👉 MVVM で Hilt のパターン化 💉  


さらば proguard、ようこそ R8。

DSL element 'useProguard' is obsolete and will be removed soon. Use 'android.enableR8' in gradle.properties to switch between R8 and Proguard..

DSL要素「useProguard」は廃止されており、まもなく削除されます。 gradle.propertiesで「android.enableR8」を使用して、R8とProguardを切り替えます。

gradle.properties


android.enableR8=true

build.gradle

とりあえずは、いけるが。

以下、編集風景。



しかし、minify とか、proguardFiles とか、どうなるの?

👉 Android Developers Blog: R8, the new code shrinker from Google, is available in Android studio 3.3 beta 


「Google Go」からの読み上げ機能が楽ちん!

👉 軽量版Googleアプリ「Google Go」、日本を含む世界で公開 音声読み上げ機能も - ITmedia NEWS 

カメラ起動からでは見えない「読み上げ機能」が分かりやすく使いやすく利用できるようになります。

ここでは、パソコンのディスプレイに写ったブラウザ上の英文を読み上げしてみました。



英文のまま読み上げたり、日本語に翻訳して読み上げたり、単語を選択して辞書を開いたりできます。

👉 Google Go: A lighter, faster way to search - Google Play のアプリ 


テキストエディタ nvALT【Mac】

こんな記事を見ました。

nvALT syncs with Simplenote. This is handy because nvALT is macOS only. So you can use the Simplenote iOS app to keep your extra brain nearby on the go.

Simplenote also has a macOS app. You may think: Why not use the Simplenote desktop application? Because — it’s not quite as fast. We’re talking milliseconds, but it’s enough that you feel the difference. It’s the difference between the $1000 Japanese garden shears and the $150 garden shears. They both cut just fine, but if you work in the garden all day, you will (probably?) feel the difference.

nvALT は Simplenote と同期できます。nvALT は mac だけのアプリなのでこれは便利です。よって、外出時に、Simplenote の iOS アプリを利用することができます。

Simplenote には mac OS アプリもあります。どうして、Simplenote のデスクトップアプリを使わないのか、とあなたは思うかもしれません。速さが違います。ミリ秒レベルの話ですが違いを認識するには十分な速さです。

👉 Fast Software, the Best Software — by Craig Mod 

テキストエディタ以外のアプリについても言及していますが、述べられてることは共感できることが多いです。

nvALT*
Simplenote
Ulysses
Sublime Text

Lightroom
Photoshop
Affinity Photo*

Sketch*
Figma*
Illustlator

Apple Maps*
Google Map

Keynote

「nvALT」使ってみましょう。

使ってみる

文章サイズのテキスト。

私は、編集するのに Simplenote の Mac用デスクトップクライアントを使っていました。

インストールしてみます。

👉 nvALT - BrettTerpstra.com 

サクサクに動きます。

しかも、Simplenote とクラウド連携できる。

Simplenote ユーザでない人は、Dropbox で連携も可能な様子。

良い、これ。

当分使ってみることにします。


簡単に Play Store の説明画像が作成できる「Android App Mockup (Beta)」

このサイト。

👉 App Mockup Builder for App Store & Play Store | AppMockUp 

ブラウザ上でPlayStoreにアップロードするための画像が簡単に作成できます。

まずテンプレートを選んで、

スクリーンショットをアップロードして、テキストやカラーなどを設定していきます。

すぐにそれらしくできちゃいます!!

便利なツールです。