LoginSignup
3
6

More than 5 years have passed since last update.

GitHubのPrivateリポジトリのライブラリを参照する

Last updated at Posted at 2017-01-27

2017/02/03追記

publishNonDefault=trueに対応した版を書きました。そちらをご参照下さい。
GitHubのPrivateリポジトリのライブラリを参照する2

目的

GitHubをプライベートで使っていて、ライブラリモジュールなんかを分離して、それを別のプライベートアプリから使いたい場合などあると思います。
そういった際に、どうすればいいのかという手順です。

privateリポジトリ参照方法

ライブラリ側

リポジトリ直下にプロジェクトフォルダを置くこと

以下のような形は参照不可。

+ Root(checkout フォルダ)
 + libs
  + libProjectA
  + libProjectB

正解はこちら。

+ Root(checkout フォルダ)
  + libProjectA
  + libProjectB

1. ライブラリプロジェクトにgradle.propertiesを配置して以下を記述

gradle.properties
# publish対象ビルドタイプ。通常はreleaseのみ。
# debug版の配布も必要な場合のみ、trueに変更してuploadArchivesを再度実行する。
# その後releaseに戻すのを忘れないこと
publishBuild=release

2. ライブラリプロジェクトのbuild.gradleに追記

  • 先頭に追加。(ライブラリプロジェクトの作成方法によってはすでに記述されている場合も有り)
lib/build.gradle
buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
  • android{}内に追記。
lib/build.gradle
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
...
    defaultPublishConfig publishBuild
}

  • 末尾に追加。
lib/build.gradle
def repo = new File(rootDir, "../maven-repo") // mavenディレクトリ名(aarを出力させるルートディレクトリ)
def groupId = "jp.co.sample.group"  // グループ名
def libName = "libA" // ライブラリ名
def version = "1.0.0" // バージョン

// maven push
apply plugin: 'maven'
uploadArchives {
    repositories {
        mavenDeployer {
            if(publishBuild.equals('debug')){
                repo = new File("${repo.absolutePath}-${publishBuild}")
                //noinspection GrReassignedInClosureLocalVar
                libName = "${libName}-${publishBuild}"
            }

            repository url: "file://${repo.absolutePath}"
            pom.version = "${version}"
            pom.groupId = "${groupId}"   
            pom.artifactId = "${libName}"
        }
    }
}

参考:最終build.gradle

libA/build.gradle
apply plugin: 'com.android.library'

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    defaultPublishConfig publishBuild
//    publishNonDefault true // trueにしてはいけない,debugとreleaseを分けたい場合は、gradle.propertiesを変更する
}

dependencies {
    compile 'com.android.support:support-v4:23.3.0'
}

def repo = new File(rootDir, "../maven-repo") // mavenディレクトリ名(aarを出力させるルートディレクトリ)
def groupId = "jp.co.sample.group"   // グループ名
def libName = "libA" // ライブラリ名
def version = "1.0.0" // バージョン

// maven push

apply plugin: 'maven'
uploadArchives {
    repositories {
        mavenDeployer {
            if(publishBuild.equals('debug')){
                repo = new File("${repo.absolutePath}-${publishBuild}")
                //noinspection GrReassignedInClosureLocalVar
                libName = "${libName}-${publishBuild}"
            }

            repository url: "file://${repo.absolutePath}"
            pom.version = "${version}"
            pom.groupId = "${groupId}" 
            pom.artifactId = "${libName}"
        }
    }
}

注意:debug用とrelease用を分けたい場合は、【debug用とrelease用を分けたい場合】を参照。

3. mavenにpublishするタスクを実行する

必要があればバージョンアップすること(依存している全てのプロジェクトが影響を受けるので要注意!)

GradleTasksの中から、uploadArchivesというタスクを見つけてダブルクリックするか、ライブラリプロジェクト下で以下を実行する。

$> ./gradlew uploadArchives 

4. GitHubにcommit & Pushする

アプリ側(ライブラリ使用側)

git-repoを使います。

1. GitHubにSSH Key(公開鍵)を登録する

個人アカウントのSettingsでやります。
SSH Keyの作り方や、設定方法はあちこちにあるのでググって下さい。

2. アプリのルートプロジェクトのbuild.gradleに追記

app/下のbuild.gradleではなく、その上の階層にあるファイルです。

root/build.gradle
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'com.layer', name: 'gradle-git-repo-plugin', version: '2.0.2'
    }

3. アプリのbuild.gradleに追記

app/下のbuild.gradleです。

dependenciesの上に追加。

app/build.gradle

apply plugin: 'git-repo'

repositories {
    github("GitHubユーザー名", "リポジトリ名", "ブランチ", "mavenディレクトリ名")
}

dependenciesは通常通り書けます。

app/build.gradle
dependencies {
...
    compile 'グループID:ライブラリ名:1.0.0'
}

注意:debug用とrelease用で依存を分ける場合は、【debug用とrelease用を分けたい場合】を参照。

参考:最終build.gradle

app/build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "sample.jp.co.cy.samplelib"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

apply plugin: 'git-repo'

repositories {
    github("myGitHubName", "libRepositoryName", "master", "maven-repo")
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'
    compile 'jp.co.sample.group:libA:1.0.0'
}

debug用とrelease用を分けたい場合

ライブラリ側

  • release版は上記までの手順で出力出来る

  • debug版は、gradle.propertiesをpublishBuild=debugに変更してから、uploadArchivesを再度実行する

説明publishNonDefault trueでassembleを実行すると、下記命名規則でaarが作成されます。

libA-debug-1.0.0.aar
libA-release-1.0.0.aar

このままだと、git-repoが依存関係を解決出来ないので、参照フォルダと参照ライブラリ名を変えるだけで解決出来るよう、上記手段を取っています。
本当は1スクリプトで両方発行出来るといいのですが、今後の課題です。

アプリ側

repositories部分を変更。

app/build.gradle
repositories {
    github("GitHubユーザー名", "リポジトリ名", "ブランチ", "mavenディレクトリ名")
    github("GitHubユーザー名", "リポジトリ名", "ブランチ", "mavenディレクトリ名-debug")
}

dependencies部分を変更。

app/build.gradle
dependencies {
...
    debugCompile('グループID:ライブラリ名-debug:1.0.0')
    releaseCompile('グループID:ライブラリ名:1.0.0')
}

参考:最終build.gradle

app/build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "sample.jp.co.cy.samplelib"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

apply plugin: 'git-repo'

repositories {
    github("myGitHubName", "libRepositoryName", "master", "maven-repo")
    github("myGitHubName", "libRepositoryName", "master", "maven-repo-debug")
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'
    debugCompile('jp.co.sample.group:libA-debug:1.0.0')
    releaseCompile('jp.co.sample.group:libA:1.0.0')
}

参考

http://qiita.com/kgmyshin/items/87f560172c31c2fbd899
https://github.com/layerhq/gradle-git-repo-plugin
https://github.com/dcendents/android-maven-gradle-plugin/issues/11

3
6
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
3
6