Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException

build 失敗して出ますよね。

意味は、

設定しているリポジトリにそのモジュールのそのバージョンがないよ。

ということです。

今、こういうの出ました。


Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find app.cash.sqldelight:android-paging3-extensions:2.0.0-alpha03.
Searched in the following locations:
  - https://dl.google.com/dl/android/maven2/app/cash/sqldelight/android-paging3-extensions/2.0.0-alpha03/android-paging3-extensions-2.0.0-alpha03.pom
  - https://repo.maven.apache.org/maven2/app/cash/sqldelight/android-paging3-extensions/2.0.0-alpha03/android-paging3-extensions-2.0.0-alpha03.pom

エラーの https から始まるリンクから「バージョン」を含む文字列までを省いてブラウザでアクセスします。

👉 https://dl.google.com/dl/android/maven2/app/cash/sqldelight/android-paging3-extensions/hatena-bookmark

👉 https://repo.maven.apache.org/maven2/app/cash/sqldelight/android-paging3-extensions/hatena-bookmark

maven central にある、android-paging3-extensions の最新バージョンは、「2.0.0-alpha01」ですね!

build.glade のバージョンを変更すれば通りますね!

 

まとめ


Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException

が出たときは、build.gradle に設定したリポジトリに、そのバージョンが存在しません。エラーメッセージそのままですね!

ほぼ、利用してるリポジトリは以下2つのリポジトリであることが多いでしょう。


repositories {
  google()
  mavenCentral()
}

以下の2つをブラウザにブックマークしておくといいですね。

👉 Google's Maven Repository hatena-bookmark
👉 Maven Central Repository Search hatena-bookmark


Kotlin スコープ関数 の上手な使い分け その3 - with

kotlin scope function

 

■ with の便利な使い方 (公式)


public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
  contract {...}
  return receiver.block()
}

with is a non-extension function that can access members of its argument concisely: you can omit the instance name when referring to its members.

with は非拡張関数で、引数のメンバに簡潔にアクセスできる。メンバを参照する際にインスタンス名を省略できる。

Grouping function calls on an object

オブジェクトの関数呼び出しをグループ化する。

Call multiple methods on an object instance (with)

オブジェクトのインスタンスに対して複数のメソッドを呼び出す。

👉 Kotlin Examples: Learn Kotlin Programming By Example hatena-bookmark
👉 Scope functions | Kotlin hatena-bookmark
👉 Idioms | Kotlin hatena-bookmark

 

■ まとめ

「with」は「記述の長い View などの連続操作をまとめる」のに多く使わています。


with(binding) {
  progressBar.visibility = View.VISIBLE
  cancelButton.visibility = View.VISIBLE
  goButton.visibility = View.GONE
  seeFileButton.visibility = View.GONE
}


with(viewBinding.recyclerView) {
  setHasFixedSize(true)
  adapter = ProductSearchAdapter()
  layoutManager =
    LinearLayoutManager(
      this@ProductSearchActivity,
      LinearLayoutManager.VERTICAL,
      false
    )
}


with(holder.binding) {
  person = items[position]
  executePendingBindings()
}

with の戻り値を使わないことのほうが多く見えますが、戻り値はあります。


val windowDpSize = with(LocalDensity.current) {
  windowSize.toDpSize()
}

👉 Kotlin スコープ関数 の上手な使い分け その1 - apply hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その2 - also hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その3 - with hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その4 - let hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その5 - run hatena-bookmark


Kotlin スコープ関数 の上手な使い分け その2 - also

kotlin scope function

apply と also は似ています。

👉 Kotlin スコープ関数 の上手な使い分け その1 - apply hatena-bookmark

 

■ also の便利な使い方 (公式)


public inline fun <T> T.also(block: (T) -> Unit): T {
  contract {...}
  block(this)
  return this
}

also works like apply: it executes a given block and returns the object called. Inside the block, the object is referenced by it, so it's easier to pass it as an argument. This function is handy for embedding additional actions, such as logging in call chains.

also も apply と同じように動作します。与えられたブロックを実行し、呼び出されたオブジェクトを返します。ブロックの内部では、オブジェクトは it によって参照されるので、引数として渡すのは簡単です。この関数は、コールチェーンにロギングなどの追加アクションを埋め込むのに便利です。


val jake = Person("Jake", 30, "Android developer") 
  .also {                                          
    writeCreationLog(it)                         
  }

Additional effects

追加効果

👉 Kotlin Examples: Learn Kotlin Programming By Example hatena-bookmark
👉 Scope functions | Kotlin hatena-bookmark
👉 Idioms | Kotlin hatena-bookmark

 

■ まとめ

「also」は「オブジェクトに対しての追加処理」に使う と良さそうです。

あと、Singleton インスタンスの生成時のコードで見かけることが印象に強いです。


@JvmStatic
fun getInstance(context: Context): PowerSpinnerPersistence =
  instance ?: synchronized(this) {
    instance ?: PowerSpinnerPersistence().also {
      instance = it
      sharedPreferenceManager =
        context.getSharedPreferences("com.skydoves.powerspinenr", Context.MODE_PRIVATE)
    }
  }


companion object {
  @Volatile
  private var INSTANCE: MySingleton? = null
  fun getInstance(context: Context) =
    INSTANCE ?: synchronized(this) {
      INSTANCE ?: MySingleton(context).also {
        INSTANCE = it
      }
    }
}

👉 Kotlin で書きたい「正しいシングルトン(Singleton)」 hatena-bookmark

こんな使い方も公式に書いてました。

Swap two variables

2つの変数を入れ替える


var a = 1
var b = 2
a = b.also { b = a }

👉 Kotlin スコープ関数 の上手な使い分け その1 - apply hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その2 - also hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その3 - with hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その4 - let hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その5 - run hatena-bookmark