我有一個 gradle kotlin 專案,我正在從 Rust 專案生成一個 kotlin 檔案,所以它最終在一個完全不同的地方,沒有 gradle 專案結構等。
如何將此檔案匯入到我的 gradle 專案中?
它有自己的包,但它是一個完全獨立的檔案。這是我的gradle檔案:
rootProject.name = "my_project"
include("app")
這是一個桌面專案,而不是安卓。
我的 build.gradle.kts:
plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id("org.jetbrains.kotlin.jvm") version "1.5.31"
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Align versions of all Kotlin components
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// This dependency is used by the application.
implementation("com.google.guava:guava:30.1.1-jre")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
// Define the main class for the application.
mainClass.set("my_project.ffi.AppKt")
}
uj5u.com熱心網友回復:
將以下代碼添加到您build.gradle.kts應該可以解決的問題(使用 Gradle 7.3.2 測驗):
// TODO: replace this dummy task with the task from your Rust project which
// generates the Kotlin source directory. Make sure that the generated
// directory (with the Kotlin file(s)) is the task output directory.
val rustTask by tasks.registering(Copy::class) {
// To test this, I had simply put a Kotlin file into this "somewhere"
// directory.
from("somewhere")
into(temporaryDir)
}
sourceSets {
main {
java {
srcDir(rustTask)
}
}
}
tasks {
compileKotlin {
dependsOn(rustTask)
}
}
因此,我們只是將生成的源作為附加源目錄添加到任務SourceSet使用的默認目錄中compileKotlin。此外,我們確保在compileKotlin運行之前生成源。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393372.html
