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

将compileSdkVersion改为31后React Native应用崩溃求助

问题排查与解决方案

从你提供的配置和React Native版本来看,升级compileSdkVersion到31后崩溃的核心原因是版本不兼容,以下是针对性的修复步骤:

1. 统一Android版本配置

当前compileSdkVersion=31但targetSdkVersion=30、buildToolsVersion=30.0.2、supportLibVersion=30.0.0,版本不匹配会触发兼容性问题,修改配置如下:

ext {
    buildToolsVersion = "31.0.0"
    minSdkVersion = 21
    compileSdkVersion = 31
    targetSdkVersion = 31
    ndkVersion = "20.1.5948944"
    fitnessVersion = "20.0.0"
    supportLibVersion = "31.0.0"
    kotlin_version = '1.6.10'
}

2. 适配Android 12(API 31)的启动Activity配置

Android 12要求带<intent-filter>的Activity必须指定android:exported属性,React Native 0.64.2默认未配置,需要修改AndroidManifest.xml:
找到主Activity(通常是MainActivity),添加android:exported="true":

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
    android:launchMode="singleTask"
    android:windowSoftInputMode="adjustResize"
    android:exported="true"> <!-- 新增该行 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

如果项目中还有其他带有<intent-filter>的Activity、Service或BroadcastReceiver,也需要添加该属性。

3. 修复React Native 0.64.x对API 31的依赖兼容问题

RN 0.64.2的部分内置依赖未适配API 31,需要在android/build.gradle中强制升级相关依赖:
在allprojects块的repositories下方添加:

configurations.all {
    resolutionStrategy {
        force 'androidx.core:core-ktx:1.7.0'
        force 'androidx.appcompat:appcompat:1.4.1'
    }
}

4. 清理缓存并重新构建

执行以下命令彻底清理缓存,避免旧缓存干扰构建:

# 清理React Native缓存
npx react-native clean
# 清理Android构建缓存
cd android && ./gradlew clean
# 重新编译运行项目
cd .. && npx react-native run-android

如果以上步骤后仍有崩溃,检查第三方依赖库是否存在未适配API 31的情况,逐个排查并升级到最新适配版本即可。

内容的提问来源于stack exchange,提问作者Даниил Лазарев

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 15:51:42