【Homebrew】git が2つある場合 【macOS】

git homebrew macos

👉 Re-installing Git on Mac OSX with Brew 

状況確認


~ % which git
/usr/local/bin/git

~ % git --version
git version 2.33.0

~ % which -a git 
/usr/local/bin/git
/usr/bin/git

~ % for i in $(which -a git); do echo -n "${i}:  "; ${i} --version; done
/usr/local/bin/git:  git version 2.33.0
/usr/bin/git:  git version 2.32.0 (Apple Git-132)

~ %  cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

~ % sudo rm -rf /usr/bin/git
Password:
rm: /usr/bin/git: Operation not permitted

私の mac には2つの git パッケージがインストールされている。


sudo rm -rf /usr/bin/git wont work for El Capitan due to SIP restriction

削除しようとしても macOS側の git は消せない。

通常、残しておくしかない。

対応方法

実行するときの git を Homebrew側に向けておく必要がある。

以下3つのどれかを、bash でも zsh でも起動スクリプトに追加する。


export PATH=$PATH:/usr/local/bin/git


export PATH=/user/local/bin:$PATH


alias git="/usr/local/bin/git"

AndroidStudio の git 不具合のときに少し気になったのでメモ。

👉 Cannot Run Git : Cannot identify version of git executable: no response – on startup 


Cannot Run Git : Cannot identify version of git executable: no response – on startup

よく出ますよね?

このダイアログ。

Cannot Run Git : Cannot identify version of git executable: no response – on startup

Cannot Run Git : Cannot identify version of git executable: no response – on startu

調べてみると、IDEA Intellij のバグトラッカーに issue があります。

Even if the CLT is installed, if the background process is heavy (for example, after booting your mac), typing "git --version" into the terminal may result in no response for several tens of seconds.

I've had this issue on Mac a few times after a reboot and auto-reopen of the previous windows.
Easy fix: quit IntelliJ with Cmd+Q and reopen it.

I got this on Windows after a restart. As Filippo says, restarting the IDE works as a fix.

👉 Cannot identify version of git executable: no response – on startup : IDEA-219762 

 

Git の設定

一応、Git の設定画面を見てみましょう。


[Preferences]

  ↓

[Version Control]

  ↓

[Git]

Cannot Run Git : Cannot identify version of git executable: no response – on startu

確認したら、IDE (AndroidStudio など) をリスタートです。

 

まとめ

IDE 再起動でダイアログは出なくなり正常に戻ります。

OS が忙しい時の Git コマンドのレスポンスの遅さが IDE に影響を及ぼしてるようです。

👉 【Homebrew】git が2つある場合 【macOS】 


MVVM で Hilt のパターン化 💉

シンプルなものにして整理、定型化しておきたい。

「どこに」←「どれを」インジェクトしてるか、とそれの記述をパターン化。

 

💉 View ← ViewModel

MVVM でいうところの View であるこれらに、ViewModel をインジェクトする場合。

コンストラクタからインジェクトできないいわゆる「Androidクラス」。
以下のものがこれに当てはまる。

- Activity
- Fragment
- View
- Service
- BroadcastReceiver

それぞれのクラスに付ける2つのアノテーションと ktx による記述でプロパティにインジェクトする。


@AndroidEntryPoint
class MainFragment : Fragment() {
  private val viewModel: MainViewModel by viewModels()


@HiltViewModel
class MainViewModel @Inject constructor(
  repository: MainRepository
) : ViewModel() {

 


💉 ViewModel ← Repository

Module を使った ViewModel コンストラクタへのインジェクト。

インターフェースの型でリンクする。


@HiltViewModel
class MainViewModel @Inject constructor(
  repository: MainRepository
) : ViewModel() {

Hilt 備え付けの Component でライフサイクルを考慮して、実装型でインジェクトする。

abstract、@Binds は、インターフェースを介す場合の、 @InstallIn( ViewModelComponent::class)、 @ViewModelScoped は、 ViewModel に対してのインジェクトのパターン。


@Module
@InstallIn(ViewModelComponent::class)
abstract class RepositoryModule {

  @Binds
  @ViewModelScoped
  abstract fun bindRepository(impl: DefaultRepository): MainRepository
}

 

💉 Repository ← DataSource

Androidクラスのようなプロパティへのインジェクトとは違う @Module、@Provides を使ったコンストラクタインジェクトの Hilt基本的パターン。

SingletonComponent は、旧 ApplcationComponent で生存期間最長。


class DefaultRepository @Inject constructor(
  private val localDao: LocalDao,
  private val contentResolver: ContentResolver,
  private val sharedPreferences: SharedPreferences
) : MainRepository {


@Module
@InstallIn(SingletonComponent::class)
object DataSourceModule {

  @Provides
  @Singleton
  fun provideLocalDao(
    @ApplicationContext context: Context
  ): LocalDao {
    return LocalDao(context)
  }

 

💉 DataSource ← Context

Context は、 @ApplicationContext と @ActivityContext がすでに用意されているので区別しながらアノテーション1つのみで注入できる。これ便利。


@Module
@InstallIn(SingletonComponent::class)
object DataSourceModule {

  @Provides
  @Singleton
  fun provideLocalDao(
    @ApplicationContext context: Context
  ): LocalDao {
    return LocalDao(context)
  }

しかし、どの位置でも自在にインジェクトしようとするとコケる。


[Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.

@Module 直下の @Provides メソッドの引数で使うものですか。

 

build.gradle


buildscript {
  ext.versions = [
    "hilt"       : "2.41",
  ]
  dependencies {
    classpath "com.google.dagger:hilt-android-gradle-plugin:${versions.hilt}"
  }
}

apply plugin: "org.jetbrains.kotlin.kapt"
apply plugin: "dagger.hilt.android.plugin"

dependencies {
  implementation "com.google.dagger:hilt-android:${versions.hilt}"
  kapt "com.google.dagger:hilt-android-compiler:${versions.hilt}"

  androidTestImplementation  "com.google.dagger:hilt-android-testing:${versions.hilt}"
  kaptAndroidTest "com.google.dagger:hilt-compiler:${versions.hilt}"
  testImplementation "com.google.dagger:hilt-android-testing:${versions.hilt}"
  kaptTest "com.google.dagger:hilt-compiler:${versions.hilt}"
}

👉 Gradle Build Setup 

 

💉 まとめ

多少の流儀はあるけど、 Component 記述がなくなっただけでも記述量は大幅に削減できます。

alpha らしく、まだコンポーネント名など変わるんかもしれんが、ここも適宜修正させてもらいます。

👉 【MVVM】 Kotlin Flow で使える5つの利用パターン 
👉 skydoves/Pokedex: 🗡️ Android Pokedex using Hilt, Motion, Coroutines, Flow, Jetpack (Room, ViewModel) based on MVVM architecture. 
👉 Hilt and Dagger annotations cheat sheet - Android Developers - Medium