Local Repositoryの構築と参照方法について、まとめます。
※環境:Android Studio Bumblebee | 2021.1.1
Local Repositoryとは
Local Repositoryはユーザのホームフォルダに作成されるリポジトリです。
Linuxの場合
:~/.m2/repository
よって、ライブラリの提供範囲はユーザになります。

ライブラリはMavenリポジトリ形式です。
ライブラリの構築
始めに、Module Libraryを作成(「Module Libraryの構築と参照」を参照)します。
作成後、ライブラリをリポジトリへ発行するためのプラグインと指示書(publishing)が必要になるので、build.gradleへ追記します。
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'maven-publish' // 発行するためのプラグイン
}
android {
...
}
afterEvaluate { // 指示書に従った処理はビルド後に行う
publishing { // 発行するための指示書
publications {
decoration(MavenPublication) {
groupId = 'com.example.lib'
artifactId = 'decoration'
version = '0.0.1'
from components.release // ビルドバリアントを指定
}
}
// ←ここにrepositories(発行先のリポジトリを指定)がないので
// ←Local Repository(デフォルト)が発行先になる
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
| プロパティ | 概要 |
|---|---|
| groupId | 管理者を一意に識別する名前です。 「どこの誰が開発したライブラリであるか」を明確にするために、ドメイン(パッケージ)名を指定します。 【例】groupId = 'com.example.lib' |
| artifactId | ライブラリ名です。 ライブラリを参照する時、この名前が使用されます。 【例】artifactId = 'decoration' |
| version | バージョンです。 バージョンの運用方法は自由です。一貫性を持たせるためにSemantic Versioningに従うという選択肢もあります。 【例】version = '0.0.1' |
build.gradleを再読み込みするために「Sync Project」を実行すると、タスクが作成されます。

最後にタスクを実行します。
ビルドバリアントで指定されたビルドが行われた後に、ライブラリの発行が指示書に従って行われます。
以下はサンプルで作成されたLocal Repositoryのフォルダ構成です。Mavenリポジトリ形式になっています。
C:¥Users¥ユーザ名¥.m2¥repository
└── com // groupId
└── example // groupId
└── lib // groupId
└── decoration // artifactId
├── 0.0.1 // version
│ ├── decoration-0.0.1.aar
│ ├── decoration-0.0.1.module
│ └── decoration-0.0.1.pom
└── maven-metadata-local.xml
ライブラリの参照
Local Repositoryをライブラリの参照先に追加します。mavenLocal()関数が参照先を示す関数です。
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement { // すべてのモジュールに有効な指示
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories { // ライブラリの参照先となるリポジトリのリスト
google()
mavenCentral()
mavenLocal() // Local Repositoryを追加
}
}
rootProject.name = "MyApplication"
include ':app'
「≧ Gradle Version 7.0」からrepositories命令の記述場所が、repositoriesModeの値により制限されるようになりました。注意が必要です。
| RepositoriesMode | 概要 |
|---|---|
| FAIL_ON_PROJECT_REPOS | settings.gradleでrepositoriesを宣言 モジュール名/build.gradleでrepositoriesを宣言たし場合、ビルドエラー |
| PREFER_PROJECT | settings.gradleで宣言したrepositoriesは無視 モジュール名/build.gradleでrepositories命令を宣言 |
| PREFER_SETTINGS | settings.gradleでrepositoriesを宣言 モジュール名/build.gradleで宣言したrepositoriesは無視 |
参照するライブラリを依存関係リスト(dependencies)に追加します。フォーマットは「groupId:artifactId:version」です。
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
...
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.example.lib:decoration:0.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
これで、Local Repositoryのライブラリを参照するようになります。
サンプルの実行結果
サンプルの実行結果を示します。
import com.example.lib.decoration.Brackets
import com.example.lib.decoration.angle
import com.example.lib.decoration.round
import com.example.lib.decoration.square
...
{
val _str1 = Brackets("#", "#").bracket("Hello World !!")
println(_str1)
val _str2 = round("Hello World !!")
println(_str2)
val _str3 = angle("Hello World !!")
println(_str3)
val _str4 = square("Hello World !!")
println(_str4)
}
I/System.out: # Hello World !! # I/System.out: ( Hello World !! ) I/System.out: < Hello World !! > I/System.out: [ Hello World !! ]
タスクのネーミングルール
タスクは発行するライブラリ毎に作成されます。

publishToMavenLocalタスクは一括で処理するタスクです。
この例では、sample-releaseとsample-debugライブラリが発行されます。
C:¥Users¥ユーザ名¥.m2¥repository
└── com
└── example
└── lib
├── sample-debug
│ ├── 0.0.1
│ │ ├── sample-debug-0.0.1.aar
│ │ ├── sample-debug-0.0.1.module
│ │ └── sample-debug-0.0.1.pom
│ └── maven-metadata-local.xml
└── sample-release
├── 0.0.1
│ ├── sample-release-0.0.1.aar
│ ├── sample-release-0.0.1.module
│ └── sample-release-0.0.1.pom
└── maven-metadata-local.xml
関連記事:
