0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gradleで解決された依存関係の情報を見てみる

Posted at

Gradleにdependeciesで利用するライブラリを指定すると、メタファイルを解析して依存関係を解決し、そのライブラリが依存しているファイルを自動的に取り込んでくれます。
・・・・・・ってことは、その解決した情報はどこかにあるんだろうから、それ見てみたいなーと思って調べて見ました。

Androidのプロジェクトテンプレートから作られた、以下の依存関係が書かれたプロジェクトで調べて見ます。

dependencies {
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.activity)
    implementation(libs.androidx.constraintlayout)
}

ひとまず、tasks.registerでgradle taskを追加して、その中で実行します。
configurationsの中からリリースビルド時に使われるreleaseRuntimeClasspathの構成を取得して、
resolvedConfiguration.lenientConfigurationから、firstLevelModuleDependenciesを参照すると、このプロジェクトが直接依存しているライブラリの情報を取得することができます。

tasks.register("appDependency") {
    val configuration = configurations["releaseRuntimeClasspath"] ?: return@register
    configuration.resolvedConfiguration
        .lenientConfiguration
        .firstLevelModuleDependencies
        .map { "${it.moduleGroup}:${it.moduleName}:${it.moduleVersion}" }
        .sorted()
        .forEach {
            println(it)
        }
}

これを実行すると以下のように出力されます

androidx.activity:activity:1.10.1
androidx.appcompat:appcompat:1.7.0
androidx.constraintlayout:constraintlayout:2.2.1
androidx.core:core-ktx:1.16.0
com.google.android.material:material:1.12.0
org.jetbrains.kotlin:kotlin-stdlib:2.0.21

そして、allModuleDependenciesを参照すると、依存ライブラリが依存しているライブラリも含めたプロジェクトが依存しているすべての依存関係を取得することができます。

tasks.register("appDependency") {
    val configuration = configurations["releaseRuntimeClasspath"] ?: return@register
    configuration.resolvedConfiguration
        .lenientConfiguration
        .allModuleDependencies
        .map { "${it.moduleGroup}:${it.moduleName}:${it.moduleVersion}" }
        .sorted()
        .forEach {
            println(it)
        }
}

これを実行すると以下のように表示されます。

androidx.activity:activity:1.10.1
androidx.annotation:annotation-experimental:1.4.1
androidx.annotation:annotation-jvm:1.8.1
androidx.annotation:annotation:1.8.1
androidx.appcompat:appcompat-resources:1.7.0
androidx.appcompat:appcompat:1.7.0
androidx.arch.core:core-common:2.2.0
androidx.arch.core:core-runtime:2.2.0
androidx.cardview:cardview:1.0.0
androidx.collection:collection-jvm:1.4.2
androidx.collection:collection:1.4.2
androidx.concurrent:concurrent-futures:1.1.0
androidx.constraintlayout:constraintlayout-core:1.1.1
androidx.constraintlayout:constraintlayout:2.2.1
androidx.coordinatorlayout:coordinatorlayout:1.1.0
androidx.core:core-ktx:1.16.0
androidx.core:core-viewtree:1.0.0
androidx.core:core:1.16.0
androidx.cursoradapter:cursoradapter:1.0.0
androidx.customview:customview:1.1.0
androidx.documentfile:documentfile:1.0.0
androidx.drawerlayout:drawerlayout:1.1.1
androidx.dynamicanimation:dynamicanimation:1.0.0
androidx.emoji2:emoji2-views-helper:1.3.0
androidx.emoji2:emoji2:1.3.0
androidx.fragment:fragment:1.5.4
androidx.interpolator:interpolator:1.0.0
androidx.legacy:legacy-support-core-utils:1.0.0
androidx.lifecycle:lifecycle-common:2.6.2
androidx.lifecycle:lifecycle-livedata-core:2.6.2
androidx.lifecycle:lifecycle-livedata:2.6.2
androidx.lifecycle:lifecycle-process:2.6.2
androidx.lifecycle:lifecycle-runtime:2.6.2
androidx.lifecycle:lifecycle-viewmodel-savedstate:2.6.2
androidx.lifecycle:lifecycle-viewmodel:2.6.2
androidx.loader:loader:1.0.0
androidx.localbroadcastmanager:localbroadcastmanager:1.0.0
androidx.print:print:1.0.0
androidx.profileinstaller:profileinstaller:1.4.0
androidx.recyclerview:recyclerview:1.1.0
androidx.resourceinspection:resourceinspection-annotation:1.0.1
androidx.savedstate:savedstate:1.2.1
androidx.startup:startup-runtime:1.1.1
androidx.tracing:tracing:1.2.0
androidx.transition:transition:1.5.0
androidx.vectordrawable:vectordrawable-animated:1.1.0
androidx.vectordrawable:vectordrawable:1.1.0
androidx.versionedparcelable:versionedparcelable:1.1.1
androidx.viewpager2:viewpager2:1.0.0
androidx.viewpager:viewpager:1.0.0
com.google.android.material:material:1.12.0
com.google.errorprone:error_prone_annotations:2.15.0
com.google.guava:listenablefuture:1.0
org.jetbrains.kotlin:kotlin-bom:1.8.22
org.jetbrains.kotlin:kotlin-stdlib-common:2.0.21
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.22
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22
org.jetbrains.kotlin:kotlin-stdlib:2.0.21
org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3
org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.7.3
org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.3
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3
org.jetbrains:annotations:23.0.0
org.jspecify:jspecify:1.0.0

依存関係をチェックするタスクを作るとかに使えそうですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?