Gradle での依存関係管理を理解するのは簡単で、多くの異なる手法ができます。 あなたがMavenやIvyを使い慣れているのであれば、Gradleはそれら二つの手法と完全な互換性を持っています。 カスタムした手法をサポートすることもできます。 If you aren't familiar with Gradle, there are great resources on their site to learn, it is recommended you give them a read to get comfortable with Gradle.
Gradle のプロジェクトは、プロジェクトのルートディレクトリにある build.gradle
ファイルで管理されます。
gdx-setup.jarを使ってlibgdxプロジェクトをビルドしたことがあるのであれば、この構造: 構造例について知っているでしょう。
ルートディレクトリと各サブディレクトリには build.gradle
ファイルがあり、明確にするためにルートディレクトリの build.gradle
ファイルに依存関係を定義します。 (一箇所で全てのファイル依存関係を制御する場合にはこのやり方を使うのが明確で簡単になるだけであって、依存関係の管理はサブディレクトリの各build.gradle
スクリプトごとに制御できるので覚えておいてください。)
ファイルには、セットアップ時に生成された既定の buildscript 項目が短く記述されています:
表示されるスクリプトの内容は、あなたのが使用する他モジュールによって少し変わってきます。 ここを参照してください。
buildscript { //Defines the repositories that are required by this script, e.g. android plugin repositories { //maven central repository, needed for the android plugin mavenCentral() //repository for libgdx artifacts maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } } // dependencies { //Adds the android gradle plugin as a dependency of this buildscript classpath 'com.android.tools.build:gradle:0.9+' } } allprojects { apply plugin: "eclipse" apply plugin: "idea" version = "1.0" ext { appName = "%APP_NAME%" gdxVersion = "1.0-SNAPSHOT" roboVMVersion = "0.0.10" } repositories { //Defines all repositories needed for all projects mavenLocal(); mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } } } project(":core") { apply plugin: "java" dependencies { //Defines dependencies for the :core project, in this example the gdx depdendency compile "com.badlogicgames.gdx:gdx:$gdxVersion" } } project(":desktop") { apply plugin: "java" dependencies { //Defines dependencies for the :desktop project, adds dependency on the :core project as well as //The gdx lwjgl backend and native dependencies compile project(":core") compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" } } project(":android") { apply plugin: "android" configurations { natives } dependencies { //defines dependencies for the :android project, depends on the :core project, //and also the android backends and all platform natives. Note the 'natives' classifier //in this project compile project(":core") compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" } }
上記の build.gradle の説明で記載したように、依存関係はルートのbuild.gradle
ファイルで設定されます。
外部への依存関係をプロジェクトに追加するには、 依存関係をbuild.scriptの正しい位置に正しく宣言しなければなりません。
(一部の) Libgdx 拡張はmavenで管理されており、mavenリポジトリに共有されています。つまり、build.gradle
ファイルを使ってとても簡単にプロジェクトにダウンロードできるということです。
各依存ファイルがどのような形式を取っているのか一覧を以下で見ることができます。
あなたが mavenに精通しているのであれば、以下のような形式を良く知っているでしょう:
compile '<groupId>:<artifactId>:<version>:<classifier>'
では、これがルートbuild.gradle
ファイルとどのように動作するのかを簡単な例を使って見てみましょう。
前述のように、異なるプラットフォーム固有フォルダごとに(例えば、 -desktop, -ios, -core) build.gradle
ファイルを個々に編集する必要はありません。
ルート build.gradle
ファイルを編集するだけです。
Here we see the dependencies for the FreeType Extension, say we want our Android project to have this dependency.
今回は、ルートディレクトリのbuild.gradle
ファイルにproject(":android")
スタブを記述します:
project(":android") { apply plugin: "android" configurations { natives } dependencies { compile project(":core") compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" } }
We know our FreeType extension has declarations:
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
So all we need to do is whack it in the dependencies stub
project(":android") { apply plugin: "android" configurations { natives } dependencies { compile project(":core") compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" } }
これで作業は全て完了し、プロジェクトはfreetype の依存関係を持ちました。 この後、依存ファイルを全て更新する必要があります。簡単でしょう?
Mavenで管理されているlibgdxの拡張は、build.gradle
スクリプトからインクルードできます:
core用の依存定義:
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
デスクトップ用の依存定義:
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
Android用の依存定義:
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
iOS用の依存定義:
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
HTML用の依存定義:
compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources" compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
core用の依存定義:
compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
デスクトップ用の依存定義:
compile "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-desktop"
Android用の依存定義:
compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion" natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86"
iOS用の依存定義:
compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion" natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-ios"
HTML用の依存定義: 互換性はありません!
core用の依存定義:
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
デスクトップ用の依存定義:
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
Android用の依存定義:
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
iOS用の依存定義 バージョン 1.6.1以降:
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
iOS用の依存定義 (旧バージョン):
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
HTML用の依存定義: 互換性がありません!
core用の依存定義:
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
デスクトップ用の依存定義:
compile "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion" compile "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"
Android用の依存定義:
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion" compile "com.badlogicgames.gdx:gdx-controllers-android:$gdxVersion"
iOS用の依存定義: Not supported, but you can still compile and run your iOS app. Controllers just won't be available
HTML用の依存定義:
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion:sources" compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion" compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion:sources"
core用の依存定義:
Dont put me in core!
デスクトップ用の依存定義:
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
Android用の依存定義: 互換性がありません!
iOS用の依存定義: 互換性がありません!
HTML用の依存定義y: 互換性がありません!
core用の依存定義:
compile "com.badlogicgames.box2dlights:box2dlights:1.3"
Android用の依存定義:
compile "com.badlogicgames.box2dlights:box2dlights:1.3"
HTML用の依存定義:
compile "com.badlogicgames.box2dlights:box2dlights:1.3:sources"
core用の依存定義:
compile "com.badlogicgames.gdx:gdx-ai:1.5.0"
Android用の依存定義:
compile "com.badlogicgames.gdx:gdx-ai:1.5.0"
HTML用の依存定義:
compile "com.badlogicgames.gdx:gdx-ai:1.5.0:sources"
and in ./html/src/yourgamedomain/GdxDefinition*.gwt.xml
add <inherits name="com.badlogic.gdx.ai"/>
注意: リポジトリ上にあるこのAIパッケージの最新バージョンは現在 1.5.0です。これより新しいバージョンや別のバージョンをダウンロードしたい場合は、 https://repo1.maven.org/maven2/com/badlogicgames/gdx/gdx-ai/をチェックしてください。
Gradleはbuildscriptに定義された全てのリポジトリを調べることで、依存管理に定義されたファイルを見つけます。 Gradleは Maven と Ivyを含む様々なリポジトリ形式を認識できます。
allprojects
スタブにて、リポジトリを定義する方法を見ることができます。これ以下がその例です。:
allprojects { repositories { // Remote Maven repo maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } // Maven Central Repo mavenCentral() // Local Maven repo mavenLocal() // Remote Ivy dir ivy { url "http://some.ivy.com/repo" } // Local Ivy dir ivy { url "../local-repo" } } }
外部依存管理はそのgroup、name、version、(たまに)classifierといった属性によって識別されます。
dependencies { compile group: 'com.badlogicgames.gdx', name: 'gdx', version: '1.0-SNAPSHOT', classifier: 'natives-desktop' }
外部依存関係を定義する時、Gradleではショートカットを使用することができます。上記の設定は、以下と同じです。:
dependencies {
compile 'com.badlogicgames.gdx:gdx:1.0-SNAPSHOT:natives-desktop'
}
ローカルの.jarファイルをmavenリポジトリを使って管理したい場合は、以下二つのコマンドを使って任意のローカル.jar fileファイルを取得してそのファイル(およびそのソース)をローカルmavenリポジトリへインストールします。
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
mvn install:install-file -Dfile=<path-to-source-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -Dclassifier=sources
その後新しい依存管理をインクルードするように gradle を設定するには、プロジェクトディレクトリのルートにある build.gradle ファイルを編集し、 core project の項目を編集します:
project(":core") { ... dependencies { ... compile "<group-id>:<artifact-id>:<version>" compile "<group-id>:<artifact-id>:<version>:sources" } }
After this you will need to refresh your dependencies for your IDE to see, so run:
Command line - $ ./gradlew --refresh-dependencies
Eclipse - $ ./gradlew eclipse
IntelliJ - $ ./gradlew idea
また、この手順で追加された依存管理をGWT 継承ファイルにインクルードする必要があることを忘れないでください。
maveで管理されていない依存関係を保持している場合でも、それらへの依存関係を維持し続けることができます!
そのためには、ルートbuild.gradle
ファイルのプロジェクトスタブにdependencies { } 項目を記述して以下のように追加します:
dependencies { compile fileTree(dir: 'libs', include: '*.jar') }
これでlibディレクトリ内にある全ての .jar ファイルは依存管理としてインクルードされます。
注意: "dir" はプロジェクトのルートへの相対パスであり、android projectの項目に依存管理を追加する場合は 'libs' を android/ ディレクトリ内に配置する必要があります。 core projectの項目に依存管理を追加する場合は、 'libs' を core/ ディレクトリ内に配置する必要があります。
以下のスクリプト例でより詳しく記載しています:
project(":android") { apply plugin: "android" configurations { natives } dependencies { compile project(":core") compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" compile fileTree(dir: 'libs', include: '*.jar') } }
It is worth nothing that these file dependencies are not included in the published dependency descriptor for your project, but they are included in transitive project dependencies within the same build.
project(例えばcore project)に フラットファイル
の依存管理を追加する場合、依存管理を android projectにも重複して宣言する必要があります。
これは、 Android Gradle プラグインが現時点ではtransitive フラットファイル
依存管理を制御できないためです。
例えば、あなたのプロジェクトの依存管理に libs
ディレクトリ内の全てのファイルを追加するつもりであれば、以下のようにする必要があります。
project(":core") { ... compile fileTree(dir: '../libs', include: '*.jar') ... } // And also project(":android") { ... compile fileTree(dir: '../libs', include: '*.jar') ... }
これは android projectでのみ必要で、他の全ての projectでは問題なく フラットファイル
の依存管理情報を継承します。
compile fileTree(dir: 'libs', include: '*.jar')
project(":core") { ... dependencies { ... compile fileTree(dir: 'libs', include: '*.jar') } }
最後に コマンドラインかIDEプラグインを使って Gradle プロジェクトをリフレッシュします。
最初に、Universal-Tween-Engineのリポジトリ (https://code.google.com/p/java-universal-tween-engine/)からtween-engine-apiをダウンロードして取得します。 この依存管理とソースファイルをローカルのmavenリポジトリにインストールするには、以下のコマンドを使用します:
mvn install:install-file -Dfile=tween-engine-api.jar -DgroupId=aurelienribon -DartifactId=tweenengine -Dversion=6.3.3 -Dpackaging=jar mvn install:install-file -Dfile=tween-engine-api-sources.jar -DgroupId=aurelienribon -DartifactId=tweenengine -Dversion=6.3.3 -Dpackaging=jar -Dclassifier=sources
With the tween engine jars in your local maven repo, add a dependency to them in your build.gradle in the root project file.
project(":core") { ... dependencies { ... compile "aurelienribon:tweenengine:6.3.3" compile "aurelienribon:tweenengine:6.3.3:sources" } }
継承情報を GdxDefinition.gwt.xml と GdxDefinitionSuperdev.gwt.xmlに追加します
<inherits name='aurelienribon.tweenengine'/>
それから依存管理情報をリフレッシュするだけです。
$ ./gradlew --refresh-dependencies
Gwt は特殊なので、このプロジェクトが依存しているモジュールは何か、このプロジェクトに依存しているモジュールは何か、をGWTコンパイラに知らせるために、設定を行う必要があります。
この設定はgwtサブディレクトリ内の gwt.xml
ファイルで行います。
GdxDefinition.gwt.xml
と GdxDefinitionSuperdev.gwt.xml
の両方を変更する必要があります。
既定の gwt.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd"> <module rename-to="html"> <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' /> <inherits name='com.badlogic.mygame.MyGame' /> <entry-point class='com.badlogic.mygame.client.GwtLauncher' /> <set-configuration-property name="gdx.assetpath" value="../android/assets" /> </module>
We depend on the libgdx gwt backend, as well as the core project, so we have them defined in a tag. So when you add your dependency via methods above, you need to add it here too!
以下hは、gwtでサポートされているlibgdx拡張です。
<inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
<inherits name='com.badlogic.gdx.physics.box2d.box2d-gwt' />
<inherits name='Box2DLights' />
<inherits name='com.badlogic.gdx.controllers.controllers-gwt' />
例: Universal Tween Engine
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd"> <module rename-to="html"> <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' /> <inherits name='com.badlogic.mygame.MyGame' /> //Let's inherit tween <inherits name='aurelienribon.tweenengine'/> <entry-point class='com.badlogic.mygame.client.GwtLauncher' /> <set-configuration-property name="gdx.assetpath" value="../android/assets" /> </module>
このページに書かれている内容は全て、プロジェクトの依存ファイルとしてUniversal Tween Engineを追加するための以下記事にて説明しているものです。