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

React Native 0.73.2+Java17环境下MoEngage项目编译失败求助

解决React Native集成MoEngage时JVM版本不兼容编译错误

问题背景

React Native 0.73.2项目集成MoEngage(RN版8.6.0,Android SDK版12.10.02),使用Java 17编译时出现错误:

'compileDebugJavaWithJavac' task (current target is 17) and 'compileDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version. Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain * Try:

已修改app模块build.gradle的compileOptions和kotlinOptions为Java 1.8,但问题仍存在。

解决方案

1. 全局统一所有模块的JVM版本

仅修改app模块的配置不足以覆盖所有依赖模块(比如MoEngage SDK或RN内部子模块),需在项目根目录的build.gradle中添加全局配置:

allprojects {
    tasks.withType(JavaCompile) {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

2. 强制子模块同步JVM版本

如果全局配置仍不生效,在app模块的build.gradle中添加代码,强制所有Android子模块使用统一的Java和Kotlin版本:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_1_8
                    targetCompatibility JavaVersion.VERSION_1_8
                }
                kotlinOptions {
                    jvmTarget = JavaVersion.VERSION_1_8.toString()
                }
            }
        }
    }
}

3. 使用JVM Toolchain配置(推荐)

React Native 0.73+支持JVM Toolchain,可替代传统的compileOptions配置,确保版本一致性。在app模块的build.gradle中替换原有配置:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(8)
    }
}

kotlin {
    jvmToolchain(8)
}

同时在根目录的build.gradle中添加全局Toolchain配置:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(8)
    }
}

4. 清理缓存并重新编译

执行以下命令清理Gradle缓存,然后重新构建项目:

./gradlew clean
./gradlew assembleDebug

内容的提问来源于stack exchange,提问作者Bagus Aji Santoso

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 12:03:16