You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非Android Kotlin项目:Gradle多平台配置及Kotlin平台判断

一、Gradle实现基于目标平台的动态配置

你可以利用Gradle内置的OperatingSystem工具类自动识别当前构建系统,动态设置LWJGL natives后缀和JVM启动参数,无需手动切换配置。以下是完整的Groovy版build.gradle配置:

import org.gradle.nativeplatform.platform.internal.OperatingSystem

// 获取当前操作系统信息
def currentOs = OperatingSystem.current()

// 动态匹配LWJGL natives后缀
project.ext.lwjglNatives = currentOs.macOsX ? "natives-macos" : 
                           currentOs.windows ? "natives-windows" : 
                           "natives-linux" // 可选:添加Linux兼容分支

// 仅在macOS下注入JVM启动参数
if (currentOs.macOsX) {
    applicationDefaultJvmArgs = ["-XstartOnFirstThread"]
}

dependencies {
    implementation platform('org.jetbrains.kotlin:kotlin-bom')
    implementation platform("org.lwjgl:lwjgl-bom:3.2.3")
    implementation "org.lwjgl:lwjgl"
    implementation "org.lwjgl:lwjgl-openal"

    runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-openal::$lwjglNatives"
}

说明:

  • 代码会自动识别当前构建环境,匹配对应平台的natives包
  • 仅在macOS环境下添加-XstartOnFirstThread参数,Windows/Linux环境下不会生成该配置
  • 额外添加了Linux兼容分支,不需要的话可以直接删除

如果使用Kotlin DSL(build.gradle.kts),对应的配置如下:

import org.gradle.nativeplatform.platform.internal.OperatingSystem

val currentOs = OperatingSystem.current()

project.ext.set("lwjglNatives", when {
    currentOs.isMacOsX -> "natives-macos"
    currentOs.isWindows -> "natives-windows"
    else -> "natives-linux"
})

if (currentOs.isMacOsX) {
    applicationDefaultJvmArgs = listOf("-XstartOnFirstThread")
}

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation(platform("org.lwjgl:lwjgl-bom:3.2.3"))
    implementation("org.lwjgl:lwjgl")
    implementation("org.lwjgl:lwjgl-openal")

    runtimeOnly("org.lwjgl:lwjgl:${project.ext.get("lwjglNatives")}")
    runtimeOnly("org.lwjgl:lwjgl-openal:${project.ext.get("lwjglNatives")}")
}
二、Kotlin代码中判断运行/构建平台

有两种实用方式判断当前平台:

方式1:使用JDK原生API

通过读取系统属性os.name判断,无需额外依赖:

fun detectPlatform(): String {
    val osName = System.getProperty("os.name").lowercase()
    return when {
        osName.contains("mac") -> "macos"
        osName.contains("win") -> "windows"
        osName.contains("nix") || osName.contains("nux") || osName.contains("aix") -> "linux"
        else -> "unknown"
    }
}

// 使用示例
fun main() {
    when (detectPlatform()) {
        "macos" -> println("当前运行在macOS")
        "windows" -> println("当前运行在Windows")
        "linux" -> println("当前运行在Linux")
        else -> println("未知平台")
    }
}

方式2:使用LWJGL内置Platform类

因为已经依赖LWJGL,可以直接用它封装好的org.lwjgl.system.Platform类,判断更准确:

import org.lwjgl.system.Platform

fun main() {
    when (Platform.get()) {
        Platform.MACOSX -> println("当前运行在macOS")
        Platform.WINDOWS -> println("当前运行在Windows")
        Platform.LINUX -> println("当前运行在Linux")
        else -> println("未知平台")
    }
}

这个方法不需要自己处理字符串匹配,LWJGL已完成平台识别逻辑,推荐使用。

内容的提问来源于stack exchange,提问作者digory doo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 14:15:41