コンパイルの設定
Kotlinマルチプラットフォームプロジェクトでは、成果物を作成するためにコンピレーションを使用します。各ターゲットは、例えば本番用やテスト用など、1つまたは複数のコンピレーションを持つことができます。
各ターゲットのデフォルトのコンピレーションは次のとおりです。
- JVM、JS、およびNativeターゲットの
main
およびtest
コンピレーション。 - Androidターゲットの場合、Androidビルドバリアントごとのコンピレーション。
本番コードやユニットテスト以外のもの(例えば、インテグレーションテストやパフォーマンステスト)をコンパイルする必要がある場合は、カスタムコンピレーションを作成できます。
成果物がどのように生成されるかは、以下で設定できます。
- プロジェクト内のすべてのコンピレーションを一度に設定する。
- 1つのターゲットに複数のコンピレーションが存在しうるため、1つのターゲットに対するコンピレーションを設定する。
- 特定のコンピレーションを設定する。
コンピレーションパラメーターの一覧と、すべてまたは特定のターゲットで使用可能なコンパイラーオプションを参照してください。
すべてのコンピレーションを設定する
この例では、すべてのターゲットに共通のコンパイラーオプションを設定します。
- Kotlin
- Groovy
kotlin {
compilerOptions {
allWarningsAsErrors.set(true)
}
}
kotlin {
compilerOptions {
allWarningsAsErrors = true
}
}
1つのターゲットに対するコンピレーションを設定する
- Kotlin
- Groovy
kotlin {
jvm {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}
}
kotlin {
jvm {
compilerOptions {
jvmTarget = JvmTarget.JVM_1_8
}
}
}
特定のコンピレーションを設定する
- Kotlin
- Groovy
kotlin {
jvm {
val main by compilations.getting {
compileTaskProvider.configure {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}
}
}
}
kotlin {
jvm {
compilations.main {
compileTaskProvider.configure {
compilerOptions {
jvmTarget = JvmTarget.JVM_1_8
}
}
}
}
}
カスタムコンピレーションを作成する
本番コードやユニットテスト以外のもの(例えば、インテグレーションテストやパフォーマンステスト)をコンパイルする必要がある場合は、カスタムコンピレーションを作成します。
例えば、jvm()
ターゲットのインテグレーションテスト用のカスタムコンピレーションを作成するには、compilations
コレクションに新しい項目を追加します。
カスタムコンピレーションの場合、すべての依存関係を手動で設定する必要があります。カスタムコンピレーションのデフォルトのソースセットは、commonMain
およびcommonTest
ソースセットに依存しません。
- Kotlin
- Groovy
kotlin {
jvm() {
compilations {
val main by getting
val integrationTest by compilations.creating {
defaultSourceSet {
dependencies {
// Compile against the main compilation's compile classpath and outputs:
implementation(main.compileDependencyFiles + main.output.classesDirs)
implementation(kotlin("test-junit"))
/* ... */
}
}
// Create a test task to run the tests produced by this compilation:
tasks.register<Test>("integrationTest") {
// Run the tests with the classpath containing the compile dependencies (including 'main'),
// runtime dependencies, and the outputs of this compilation:
classpath = compileDependencyFiles + runtimeDependencyFiles + output.allOutputs
// Run only the tests from this compilation's outputs:
testClassesDirs = output.classesDirs
}
}
}
}
}
kotlin {
jvm() {
compilations.create('integrationTest') {
defaultSourceSet {
dependencies {
def main = compilations.main
// Compile against the main compilation's compile classpath and outputs:
implementation(main.compileDependencyFiles + main.output.classesDirs)
implementation kotlin('test-junit')
/* ... */
}
}
// Create a test task to run the tests produced by this compilation:
tasks.register('jvmIntegrationTest', Test) {
// Run the tests with the classpath containing the compile dependencies (including 'main'),
// runtime dependencies, and the outputs of this compilation:
classpath = compileDependencyFiles + runtimeDependencyFiles + output.allOutputs
// Run only the tests from this compilation's outputs:
testClassesDirs = output.classesDirs
}
}
}
}
例えば、最終的な成果物で異なるJVMバージョン用のコンピレーションを結合する場合や、Gradleでソースセットを既に設定しており、マルチプラットフォームプロジェクトに移行したい場合など、他のケースでもカスタムコンピレーションを作成する必要があります。
JVMコンピレーションでJavaソースを使用する
プロジェクトウィザードでプロジェクトを作成すると、Javaソースがデフォルトで作成され、JVMターゲットのコンピレーションに含まれます。
Javaソースファイルは、Kotlinソースルートの子ディレクトリに配置されます。例えば、パスは次のとおりです。

共通ソースセットには、Javaソースを含めることはできません。
現在の制限により、KotlinプラグインはJavaプラグインによって構成されたいくつかのタスクを置き換えます。
jar
の代わりにターゲットのJARタスク(例えば、jvmJar
)。test
の代わりにターゲットのテストタスク(例えば、jvmTest
)。- リソースは、
*ProcessResources
タスクの代わりに、コンピレーションの同等のタスクによって処理されます。
このターゲットの公開はKotlinプラグインによって処理され、Javaプラグインに固有の手順は必要ありません。
ネイティブ言語との相互運用を設定する
Kotlinはネイティブ言語との相互運用性と、特定のコンピレーションのためにこれを設定するDSLを提供します。
ネイティブ言語 | サポートされているプラットフォーム | コメント |
---|---|---|
C | すべてのプラットフォーム(WebAssemblyを除く) | |
Objective-C | Appleプラットフォーム(macOS、iOS、watchOS、tvOS) | |
Objective-C経由のSwift | Appleプラットフォーム(macOS、iOS、watchOS、tvOS) | Kotlinは@objc 属性でマークされたSwift宣言のみを使用できます。 |
1つのコンピレーションは、複数のネイティブライブラリと相互作用できます。定義ファイルまたはビルドファイルのcinterops
ブロックで使用可能なプロパティを使用して、相互運用性を設定します。
- Kotlin
- Groovy
kotlin {
linuxX64 { // Replace with a target you need.
compilations.getByName("main") {
val myInterop by cinterops.creating {
// Def-file describing the native API.
// The default path is src/nativeInterop/cinterop/<interop-name>.def
definitionFile.set(project.file("def-file.def"))
// Package to place the Kotlin API generated.
packageName("org.sample")
// Options to be passed to compiler by cinterop tool.
compilerOpts("-Ipath/to/headers")
// Directories to look for headers.
includeDirs.apply {
// Directories for header search (an equivalent of the -I<path> compiler option).
allHeaders("path1", "path2")
// Additional directories to search headers listed in the 'headerFilter' def-file option.
// -headerFilterAdditionalSearchPrefix command line option equivalent.
headerFilterOnly("path1", "path2")
}
// A shortcut for includeDirs.allHeaders.
includeDirs("include/directory", "another/directory")
}
val anotherInterop by cinterops.creating { /* ... */ }
}
}
}
kotlin {
linuxX64 { // Replace with a target you need.
compilations.main {
cinterops {
myInterop {
// Def-file describing the native API.
// The default path is src/nativeInterop/cinterop/<interop-name>.def
definitionFile = project.file("def-file.def")
// Package to place the Kotlin API generated.
packageName 'org.sample'
// Options to be passed to compiler by cinterop tool.
compilerOpts '-Ipath/to/headers'
// Directories for header search (an eqivalent of the -I<path> compiler option).
includeDirs.allHeaders("path1", "path2")
// Additional directories to search headers listed in the 'headerFilter' def-file option.
// -headerFilterAdditionalSearchPrefix command line option equivalent.
includeDirs.headerFilterOnly("path1", "path2")
// A shortcut for includeDirs.allHeaders.
includeDirs("include/directory", "another/directory")
}
anotherInterop { /* ... */ }
}
}
}
}
Androidのコンピレーション
デフォルトでAndroidターゲット用に作成されるコンピレーションは、Androidビルドバリアントに関連付けられています。 各ビルドバリアントに対して、同じ名前でKotlinコンピレーションが作成されます。
次に、各バリアントに対してコンパイルされる各Androidソースセットに対して、Kotlinソースセットがそのソースセット名にターゲット名が付加された名前で作成されます。例えば、Androidソースセットdebug
とandroidTarget
という名前のKotlinターゲットの場合、KotlinソースセットandroidDebug
です。これらのKotlinソースセットは、バリアントのコンピレーションに適切に追加されます。
デフォルトのソースセットcommonMain
は、各本番(アプリケーションまたはライブラリ)バリアントのコンピレーションに追加されます。
同様に、commonTest
ソースセットは、ユニットテストおよびインストルメンテーションテストバリアントのコンピレーションに追加されます。
kapt
によるアノテーション処理もサポートされていますが、現在の制限により、Androidターゲットはkapt
依存関係が設定される前に作成する必要があり、Kotlinソースセットの依存関係内ではなく、トップレベルのdependencies {}
ブロックで行う必要があります。
kotlin {
androidTarget { /* ... */ }
}
dependencies {
kapt("com.my.annotation:processor:1.0.0")
}
ソースセット階層のコンピレーション
Kotlinは、dependsOn
関係を持つソースセット階層を構築できます。
ソースセットjvmMain
がソースセットcommonMain
に依存している場合:
jvmMain
が特定のターゲットに対してコンパイルされるたびに、commonMain
もそのコンピレーションに参加し、JVMクラスファイルなどの同じターゲットバイナリ形式にコンパイルされます。jvmMain
のソースは、commonMain
の宣言(内部宣言を含む)と、implementation
依存関係として指定されたものも含め、commonMain
の依存関係を「認識」します。jvmMain
には、commonMain
の期待される宣言のプラットフォーム固有の実装を含めることができます。commonMain
のリソースは、常に処理され、jvmMain
のリソースとともにコピーされます。jvmMain
とcommonMain
の言語設定は一貫している必要があります。
言語設定の一貫性は、次の方法で確認されます。
jvmMain
は、commonMain
以上のlanguageVersion
を設定する必要があります。jvmMain
は、commonMain
が有効にするすべての不安定な言語機能を有効にする必要があります(バグ修正機能にはそのような要件はありません)。jvmMain
は、commonMain
が使用するすべての実験的アノテーションを使用する必要があります。apiVersion
、バグ修正言語機能、およびprogressiveMode
は、任意に設定できます。
GradleでIsolated Projects機能を設定する
GradleはIsolated Projects機能を提供し、 個々のプロジェクトを互いに「分離」することで、ビルドのパフォーマンスを向上させます。この機能は、ビルドスクリプト とプラグインをプロジェクト間で分離し、安全に並行して実行できるようにします。
この機能を有効にするには、Gradleの手順に従ってシステムプロパティを設定します。
Isolated Projects機能の詳細については、Gradleのドキュメントを参照してください。