Remote Repositoryの構築と参照(Bumblebee版)

投稿日:  更新日:

Remote Repositoryの構築と参照方法について、まとめます。

※環境:Android Studio Bumblebee | 2021.1.1

スポンサーリンク

Remote Repositoryとは

Remote Repositoryはネットワークを経由して参照されることを目的にしたリポジトリです。言い換えれば、ネットワーク上に公開されたリポジトリです。

Android Studioで参照するライブラリ

リポジトリを所持するホストをリポジトリサーバと言います。

このサーバを何処に設置するかによって、リポジトリを公開する範囲が決まってきます。

例えば、誰もが参照可能な外部ネットワークに設置すれば、全世界に公開することになります。

「Central Repository」も「Remote Repository」の中の1つです。

スポンサーリンク

利用可能なスキーム

ライブラリの発行先リポジトリサーバはURLで指定します。

このURLのスキームで使用するプロトコルとサーバのタイプが決まります。

スキームは次のようなものが利用可能です。

スキームサーバのタイプ
fileローカルなストレージ
httpウェブサーバ
httpsウェブサーバ(Over SSL)
gcsGoogle Cloud Storage
s3Amazon Simple Storage Service
sftpFTPサーバ
※キャッシュの場所: C:¥Users¥ユーザ名¥.gradle¥caches
スポンサーリンク

ライブラリの構築

始めに、Module Libraryを作成(「Module Libraryの構築と参照」を参照)します。

作成後、ライブラリをリポジトリへ発行するためのプラグインと指示書(publishing)が必要になるので、build.gradleへ追記します。

指示書のrepositoriesで発行先リポジトリサーバのURLを指定します。

以下は、スキームfileを用いて、ローカルストレージへ発行する例です。

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 {   // 発行先のリポジトリ
            maven {
                url "file:///Y:/MyRepo"    // Windows Y:ドライブのMyRepoフォルダ
            }
        }
    }
}

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」を実行すると、タスクが作成されます。

build.gradleの再読み込みでタスク作成

タスクを実行すると、ビルドバリアントで指定されたビルドが行われた後に、ライブラリの発行が指示書に従って行われます。

以下は作成されたRemote Repositoryのフォルダ構成です。Mavenリポジトリ形式になっています。

Y:¥MyRepo
└── com
    └── example
        └── lib
            └── decoration
                ├── 0.0.1
                │   ├── decoration-0.0.1.aar
                │   ├── decoration-0.0.1.aar.md5
                │   ├── decoration-0.0.1.aar.sha1
                │   ├── decoration-0.0.1.aar.sha256
                │   ├── decoration-0.0.1.aar.sha512
                │   ├── decoration-0.0.1.module
                │   ├── decoration-0.0.1.module.md5
                │   ├── decoration-0.0.1.module.sha1
                │   ├── decoration-0.0.1.module.sha256
                │   ├── decoration-0.0.1.module.sha512
                │   ├── decoration-0.0.1.pom
                │   ├── decoration-0.0.1.pom.md5
                │   ├── decoration-0.0.1.pom.sha1
                │   ├── decoration-0.0.1.pom.sha256
                │   └── decoration-0.0.1.pom.sha512
                ├── maven-metadata.xml
                ├── maven-metadata.xml.md5
                ├── maven-metadata.xml.sha1
                ├── maven-metadata.xml.sha256
                └── maven-metadata.xml.sha512
スポンサーリンク

ライブラリの参照

Remote Repositoryをライブラリの参照先に追加します。

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {  // すべてのモジュールに有効な指示
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {       // ライブラリの参照先となるリポジトリのリスト
        google()
        mavenCentral()
		maven {
            url "file:///Y:/MyRepo"    // Remote Repositoryを追加
        }
    }
}
rootProject.name = "MyApplication"
include ':app'

「≧ Gradle Version 7.0」からrepositories命令の記述場所が、repositoriesModeの値により制限されるようになりました。注意が必要です。

RepositoriesMode概要
FAIL_ON_PROJECT_REPOSsettings.gradleでrepositoriesを宣言
モジュール名/build.gradleでrepositoriesを宣言たし場合、ビルドエラー
PREFER_PROJECTsettings.gradleで宣言したrepositoriesは無視
モジュール名/build.gradleでrepositories命令を宣言
PREFER_SETTINGSsettings.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'
}

これで、Remote 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 !! ]
スポンサーリンク

タスクのネーミングルール

タスクは発行するライブラリ毎に作成されます。

発行するライブラリが複数

publishAllPublicationsToMavenRepositoryタスクは一括で処理するタスクです。

この例では、sample-releaseとsample-debugライブラリが発行されます。

Y:¥MyRepo
└── com
    └── example
        └── lib
            ├── sample-debug
            │   ├── 0.0.1
            │   │   ├── sample-debug-0.0.1.aar
            │   │   ├── sample-debug-0.0.1.aar.md5
            │   │   ├── sample-debug-0.0.1.aar.sha1
            │   │   ├── sample-debug-0.0.1.aar.sha256
            │   │   ├── sample-debug-0.0.1.aar.sha512
            │   │   ├── sample-debug-0.0.1.module
            │   │   ├── sample-debug-0.0.1.module.md5
            │   │   ├── sample-debug-0.0.1.module.sha1
            │   │   ├── sample-debug-0.0.1.module.sha256
            │   │   ├── sample-debug-0.0.1.module.sha512
            │   │   ├── sample-debug-0.0.1.pom
            │   │   ├── sample-debug-0.0.1.pom.md5
            │   │   ├── sample-debug-0.0.1.pom.sha1
            │   │   ├── sample-debug-0.0.1.pom.sha256
            │   │   └── sample-debug-0.0.1.pom.sha512
            │   ├── maven-metadata.xml
            │   ├── maven-metadata.xml.md5
            │   ├── maven-metadata.xml.sha1
            │   ├── maven-metadata.xml.sha256
            │   └── maven-metadata.xml.sha512
            └── sample-release
                ├── 0.0.1
                │   ├── sample-release-0.0.1.aar
                │   ├── sample-release-0.0.1.aar.md5
                │   ├── sample-release-0.0.1.aar.sha1
                │   ├── sample-release-0.0.1.aar.sha256
                │   ├── sample-release-0.0.1.aar.sha512
                │   ├── sample-release-0.0.1.module
                │   ├── sample-release-0.0.1.module.md5
                │   ├── sample-release-0.0.1.module.sha1
                │   ├── sample-release-0.0.1.module.sha256
                │   ├── sample-release-0.0.1.module.sha512
                │   ├── sample-release-0.0.1.pom
                │   ├── sample-release-0.0.1.pom.md5
                │   ├── sample-release-0.0.1.pom.sha1
                │   ├── sample-release-0.0.1.pom.sha256
                │   └── sample-release-0.0.1.pom.sha512
                ├── maven-metadata.xml
                ├── maven-metadata.xml.md5
                ├── maven-metadata.xml.sha1
                ├── maven-metadata.xml.sha256
                └── maven-metadata.xml.sha512
スポンサーリンク

関連記事:

アプリ開発の中で作成したクラスや関数などを資産としてライブラリ化し、次のアプリ開発で再利用するといったスタイルは、開発効率を上げる常套手段です。 私のように個人でアプリを開発している場合、この恩恵は薄いのですが、ソフトハウス(死語?)で年間に多くのアプリを開発している現場では濃いと思います。 「誰かが欲しいと思う機能」は「誰もが欲しいと思う機能」です。同じような仕様のアプリ開発を受注することはありませんか?機能の骨幹はライブラリからの流用に任せて、外観は顧客の要望に合わせるだけで済んだら、楽です。 今回は、Android Studio(アプリ開発)で参照するライブラリの種類について、まとめます。 ...
Module Libraryの構築と参照方法について、まとめます。 ※環境:Android Studio Bumblebee | 2021.1.1 ...
Local Repositoryの構築と参照方法について、まとめます。 ※環境:Android Studio Bumblebee | 2021.1.1 ...
Central Repositoryの参照方法について、まとめます。 ...
リポジトリサーバをApache+WebDavで構築する方法です。 Remote Repositoryの詳細は「Remote Repositoryの構築と参照」を参照してください。 ...
リポジトリサーバをGoogle Cloud Storageで構築する方法です。 Remote Repositoryの詳細は「Remote Repositoryの構築と参照」を参照してください。 ...
Module Libraryの構築と参照方法について、まとめます。 この記事は「Module Libraryの構築と参照(2022.04発行)」をAndroid Studio Giraffe(2023.07)向けに書き直したものです。 GiraffeよりGradleでKotlin DSLが推奨になりました。それに従い、ビルドスクリプトをKotlinで記述したコードに置き換えています。 ※環境:Android Studio Giraffe | 2022.3.1    :Android Gradle Plugin 8.1.0    :Gradle 8.0 ...
Local Repositoryの構築と参照方法について、まとめます。 この記事は「Local Repositoryの構築と参照(2022.04発行)」をAndroid Studio Giraffe(2023.07)向けに書き直したものです。 Gradle Ver7.x以降、リポジトリの構築方法が若干変更になりました。 また、GiraffeよりGradleでKotlin DSLが推奨になりました。それに従い、ビルドスクリプトをKotlinで記述したコードに置き換えています。 ※環境:Android Studio Giraffe | 2022.3.1    :Android Gradle Plugin 8.1.0    :Gradle 8.0 ...
Remote Repositoryの構築と参照方法について、まとめます。 この記事は「Remote Repositoryの構築と参照(2022.04発行)」をAndroid Studio Giraffe(2023.07)向けに書き直したものです。 Gradle Ver7.x以降、リポジトリの構築方法が若干変更になりました。 また、GiraffeよりGradleでKotlin DSLが推奨になりました。それに従い、ビルドスクリプトをKotlinで記述したコードに置き換えています。 ※環境:Android Studio Giraffe | 2022.3.1    :Android Gradle Plugin 8.1.0    :Gradle 8.0 ...
スポンサーリンク