LoginSignup
140
145

More than 5 years have passed since last update.

【Android】Libraryを作ってprivateなgithubリポジトリにpushして、チームのみんなでgradleに書くだけで使えるようにするまで

Last updated at Posted at 2015-01-29

どのアプリを作るにしても共通のものを毎回実装するのは無駄なのでライブラリ化して使い回したいなぁとうすうす思っていたのでやってみました。

ライブラリプロジェクトつくってgithubにあげる

ライブラリプロジェクトを作って実装する

いろんなやり方があるのでしょうが、僕の場合は普通にNew Projectしてbuild.gradleの

apply plugin: 'com.android.application'

apply plugin: 'com.android.library'

に変更して中身をがりがり書きました。

また app というモジュール名が気持ち悪い方は モジュール名をlibraryなどと適切な名前に変えておきましょう。 setting.gradleの更新もお忘れなく。

以下は library に変更したとして説明してきます。

aarファイルの作成

aarは Android Library Projectの配布用ファイルといったところです。

このファイルを作成するには、プロジェクトのルートで下記コマンドを打って

./gradlew assembleRelease

下記ファイルが作られていることを確認できればOKです。

./library/build/outputs/aar/library.aar

maven-pluginを導入してpomなどの成果物を作る

ライブラリプロジェクトのbuild.gradleに下記を追記します。

def repo = new File(rootDir, "repository")

apply plugin: 'maven'

uploadArchives {
    repositories {
        mavenDeployer {
            repository url: "file://${repo.absolutePath}"
            pom.version = '1.0.0'             // version
            pom.groupId = 'com.genestream'    // グループ名
            pom.artifactId = 'common-library' // ライブラリ名
        }
    }
}

追記したら、プロジェクトのルートで下記コマンドを打ち成果物を作ります。

./gradlew uploadArchives

repositoryディレクトリの中にpomファイルなどができていれば成功です。

githubにpushする

あとは gh-pages ブランチを切って、そこにpushするだけです。

gh-pagesなのはそうすると後々URLが奇麗だから、ただそれだけです。

つくったライブラリを使用する

(一応) githubリポジトリがpublicな場合

使用したいアプリプロジェクトのアプリ用のbuild.gradleに下記を追記するだけ。

repositories {
    maven { url 'http://<githubユーザー名>.github.com/<githubリポジトリ名>/repository' }
}

dependencies {
    compile '<グループ名>:<ライブラリ名>:<バージョン>' 
}

今回の場合は下記になります。

repositories {
    maven { url 'http://genestream.github.com/library/repository' }
}

dependencies {
    compile 'com.genestream:library:1.0.0' 
}

簡単。

githubリポジトリがprivateな場合

puhblicな場合に比べて数行増えます。

まずgit-repoを導入します。

ルートのbuild.gradleに下記を追加します。

build.gradle
buildscript {
    repositories {
        maven { url "https://github.com/layerhq/releases-gradle/raw/master/releases" }
    }
    dependencies {
        classpath group: 'com.layer', name: 'git-repo-plugin', version: '1.0.0'
    }
}

次にアプリのbuild.gradleに下記を追加します。

build.gradle

apply plugin: 'git-repo'

repositories {
    github("<github name>", "<リポジトリ名>", "<ブランチ名>", "<Direcotry>")
}

dependencies {
    compile '<グループ名>:<ライブラリ名>:<バージョン>'
}

今回の場合は下記です。

apply plugin: 'git-repo'

repositories {
    github("genestream", "library", "gh-pages", "repository")
}

dependencies {
    compile 'com.genestream:library:1.0.+'
}

以上です!

140
145
1

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
140
145