LoginSignup
12
7

More than 5 years have passed since last update.

JitPackでmulti moduleなAndroid Libraryを配信する

Last updated at Posted at 2018-02-24

以下のようなmulti module project構成のAndroid LibraryをJitPackで配信できるようにします

root
├── core // 主となる機能を格納したmodule
└── plugin  // coreに対して機能を追加するmodule

coreのみ配信

まずはcoreのみ配信してみます

Library側

root直下のbuild.gradleに以下を追加します

build.gradle
buildscript {
  ...

  dependencies {
    // gradle version毎にversionを変更する必要あり
    // https://github.com/dcendents/android-maven-gradle-plugin#note-on-releases
    classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' 
    ...
  }
}

次に、core/build.gradleに以下の記述を追加します

core/build.gradle
apply plugin: 'com.github.dcendents.android-maven'

group="com.github.$USERNAME.$REPO"

これをGithubにpushするとJitPackでlibraryを配信可能になります

versionはbranch名やtag, commitなど指定可能ですが、今回はbranch名をexampleでpushした体で進めようと思います
この場合、以下のようなversionで配信されます

example-SNAPSHOT

Library利用側

配信したLibraryを利用する場合は以下のように記述します

app/build.gradle
repositories {
  ...
  maven { url 'https://jitpack.io' } // ※ Private repositoryで配信されている場合はauthTokenの設定も必要
}


dependencies {
   ...
   implementation 'com.github.$USERNAME:$REPO:example-SNAPSHOT@aar'
}

余談

JitPack配信のsetupをしていてこの辺でハマってしまったのですが、
前知識として"multi moduleなlibraryを com.github.$USERNAME.$REPO:$MODULE:$VERSIONのように読み込むことができる"という情報を仕入れていたためcoreのみ配信可能な状態で以下のような設定をしてしまいました

app/build.gradle
dependencies {
   ...
   implementation 'com.github.$USERNAME.$REPO:core:example-SNAPSHOT@aar'
}

しかし配信されるmoduleが1つだけの場合JitPackが気を利かせて(?)module名を使わないpathに変更してくれるため、このpathでは読み込むことができませんでした :innocent: :innocent: :innocent:

multi moduleなlibraryを一つ一つsetupする際にはご注意ください :cry:

coreplugin を配信

次にpluginを配信します

Library側

rootのbuild.gradleは既に設定済みなので、plugin/build.gradleのみ変更します
変更内容はcore/build.gradleと同じです

plugin/build.gradle
apply plugin: 'com.github.dcendents.android-maven'

group="com.github.$USERNAME.$REPO"

これをGithubにpushします

Library利用側

corepluginが利用可能になりましたので読み込んでみます

coreのみ利用する

pluginはいらないよという場合は以下のようにしてcoreだけ利用することができます

app/build.gradle

dependencies {
   ...
   implementation 'com.github.$USERNAME.$REPO:core:example-SNAPSHOT@aar'
}

corepluginを利用する

corepluginを利用する場合は2通りの設定方法があります

app/build.gradle

dependencies {
   ...
   implementation 'com.github.$USERNAME.$REPO:core:example-SNAPSHOT@aar'
   implementation 'com.github.$USERNAME.$REPO:plugin:example-SNAPSHOT@aar'
}

もしくは

app/build.gradle

dependencies {
   ...
   implementation 'com.github.$USERNAME:$REPO:example-SNAPSHOT@aar'
}

module名を指定しない場合、repository内の全てのmoduleを落としてくるので新しくmoduleが追加されたりした場合、それも配信されます

どちらの方法で読み込むかはユースケースごとに選択すると良いかと思います

参考

https://jitpack.io/docs/ANDROID/
https://android.jlelse.eu/publish-multi-module-android-libraries-on-jitpack-339213f6224c

12
7
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
12
7