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

Gradle升级后Kotlin版本不兼容致React Native Android构建失败

问题:React Native Android构建APK时Kotlin版本不兼容错误

升级Gradle版本从7.5到8.5后,构建debug/release APK时出现以下错误:

.gradle/caches/8.9/generated-gradle-jars/gradle-api-8.9.jar!/META-INF/bean-serialization-services.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

当前配置文件内容

项目根目录build.gradle

ext { 
    // kotlinVersion = "2.1.0"
    kotlin_version = '2.1.0'
    buildToolsVersion = "34.0.0"
    minSdkVersion = 21
    compileSdkVersion = 34
    targetSdkVersion = 34
    ndkVersion = "23.1.7779620"
}
repositories {
    google()
    mavenCentral()
    jcenter()
    maven {  url "https://maven.google.com"  }
}
dependencies {
    classpath('com.android.tools.build:gradle:8.7.3')
    classpath("com.facebook.react:react-native-gradle-plugin")
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

plugins {
    id 'org.jetbrains.kotlin.jvm'
}
repositories {
    mavenCentral()
}
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
}

subprojects {project ->
    if (project.name.contains('react-native-fetch-blob')) {
        buildscript {
            repositories {
                google()
                maven { url "https://dl.bintray.com/android/android-tools/"  }
                mavenLocal()
                jcenter()                
            }    
        }
    }
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

app/build.gradle

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    packagingOptions {
        pickFirst 'lib/x86/libc++_shared.so'
        pickFirst 'lib/x86_64/libc++_shared.so'
        pickFirst 'lib/armeabi-v7a/libc++_shared.so'
        pickFirst 'lib/arm64-v8a/libc++_shared.so'
    }
    // dexOptions {
    //     incremental true
    //     javaMaxHeapSize "4g"
    //     }


    namespace "com.tcp_socket_app_test"

    defaultConfig {
        applicationId "com.tcp_socket_app_test"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include (*reactNativeArchitectures())
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }

        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.0.0")
    
    implementation 'androidx.work:work-runtime-ktx:2.7.1'

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
}

已尝试删除.gradle缓存并重建项目,问题未解决。


解决方案

1. 对齐Gradle、AGP和Kotlin版本兼容性

Gradle 8.9最低要求Kotlin 1.9.0,当前配置中kotlin.jvm插件未指定版本,且第三方依赖(如androidx.work:work-runtime-ktx:2.7.1)依赖低版本Kotlin,导致冲突。

修改根目录build.gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '2.1.0' // 和kotlin-gradle-plugin版本统一
}

修改app/build.gradle中的依赖:

// 升级work依赖到兼容Kotlin 2.x的版本
implementation 'androidx.work:work-runtime-ktx:2.9.0'

2. 强制所有子项目使用统一Kotlin版本

在根目录build.gradle添加配置,避免子项目版本不一致:

allprojects {
    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib:2.1.0"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.1.0"
        }
    }
}

3. 彻底清理缓存并重建

执行以下命令:

./gradlew clean
rm -rf ~/.gradle/caches
rm -rf node_modules && npm install
./gradlew assembleDebug

4. 修复第三方库兼容性

react-native-fetch-blob是老旧库,需适配高版本工具链,修改子项目配置:

subprojects {project ->
    if (project.name.contains('react-native-fetch-blob')) {
        buildscript {
            repositories {
                google()
                mavenCentral() // 替换废弃的jcenter和bintray
                mavenLocal()
            }
            dependencies {
                classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.0"
            }
        }
        project.apply plugin: 'kotlin-android'
        project.android {
            compileSdkVersion rootProject.ext.compileSdkVersion
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:24:55