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

升级Gradle 5.4与JDK 11时编译报错:javax/xml/bind/JAXBException

解决Gradle 5.4 + JDK 11下JAXBException/NoClassDefFoundError问题

我之前升级项目到JDK11的时候也踩过一模一样的坑!JDK9+确实把JAXB这类Java EE模块从默认的JRE类库中移除了,你虽然加了依赖但没生效,大概率是依赖配置或者JDK模块化的问题,试试下面这些步骤:

1. 替换过时的依赖配置

Gradle 5.x开始compile关键字已经被废弃了,改用implementation来声明运行时需要的依赖,确保依赖能正确传递到编译和运行类路径。把你app/build.gradle里的依赖改成:

implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
implementation group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0'
implementation group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.0'
implementation group: 'javax.activation', name: 'activation', version: '1.1.1'
implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'

2. 给编译任务添加JDK模块参数

因为JDK9+采用模块化设计,即使引入了JAXB依赖,有时候也需要显式告诉编译器要加载java.xml.bind模块。在app/build.gradle里添加:

tasks.withType(JavaCompile) {
    options.compilerArgs += [
        '--add-modules', 'java.xml.bind'
    ]
}

3. 确保Android Gradle Plugin(AGP)版本兼容JDK11

从你的任务名compileDesarrolloDebugJavaWithJavac来看这是Android项目,Gradle 5.4对应的AGP版本至少要3.5.x以上才能支持JDK11。检查项目根目录的build.gradle,把AGP版本更新到兼容版本:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.4' // 或更高兼容版本
    }
}

4. 配置JDK版本和编译选项

在gradle.properties中指定JDK11路径,同时在app/build.gradle里设置编译兼容性:

gradle.properties

org.gradle.java.home=/path/to/your/jdk11-installation

app/build.gradle

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
}

5. 排查依赖冲突

如果还是不行,运行./gradlew app:dependencies查看依赖树,确认是否有其他依赖引入了旧版本的JAXB相关类,导致类加载冲突。如果有,用exclude排除冲突的模块:

implementation('some-conflicting-dependency') {
    exclude group: 'javax.xml.bind'
}

做完这些后,先执行./gradlew app:clean清理旧编译产物,再运行./gradlew app:compileDesarrolloDebugJavaWithJavac测试编译是否通过。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 12:22:50