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

React Native无法识别Android SDK,Android打包遇Dex溢出错误求助

Fixing React Native Android Build Failures: Dex Limit & SDK Recognition Issues

Hey there, let's break down your problems step by step—you've got two key issues here: the Dex method count exceeding the 65536 limit, and React Native not recognizing your Android SDK. Let's tackle them one by one.

1. Resolve the "Cannot fit requested classes in a single dex file" Error

This is the classic 64K method limit issue, which pops up when your app (plus all its dependencies) has more methods than Android's default single Dex file can handle. Here's how to enable Multidex to fix it:

  • Open your project's android/app/build.gradle file
  • In the defaultConfig block, add multiDexEnabled true:
    android {
        defaultConfig {
            // ... other existing config
            multiDexEnabled true
        }
    }
    
  • In the dependencies block, add the Multidex library dependency:
    dependencies {
        // ... other dependencies
        implementation 'androidx.multidex:multidex:2.0.1'
    }
    
  • If you have a custom Application class (extending Application), update it to extend MultiDexApplication instead:
    public class MyApplication extends MultiDexApplication {
        // ... your existing code
    }
    
    • If you can't extend MultiDexApplication (e.g., you're using another base class), override the attachBaseContext method instead:
      @Override
      protected void attachBaseContext(Context base) {
          super.attachBaseContext(base);
          MultiDex.install(this);
      }
      
  • Save the file, then clean your build with cd android && ./gradlew clean before rebuilding.

2. Fix React Native CLI Doctor Not Recognizing Android SDK

Even if echo ANDROID_HOME works, sometimes environment variables aren't properly loaded across all terminal sessions, or React Native needs additional path configurations. Try these steps:

  • Verify environment variables are set correctly:
    Depending on your shell (Bash, Zsh, etc.), open your shell config file (~/.bashrc, ~/.bash_profile, or ~/.zshrc on Mac; System Properties > Environment Variables on Windows) and ensure these lines are present:

    # Mac example
    export ANDROID_HOME=$HOME/Library/Android/sdk
    export PATH=$PATH:$ANDROID_HOME/emulator
    export PATH=$PATH:$ANDROID_HOME/tools
    export PATH=$PATH:$ANDROID_HOME/tools/bin
    export PATH=$PATH:$ANDROID_HOME/platform-tools
    

    After adding these, run source ~/.zshrc (or your corresponding config file) to apply changes immediately, then restart your terminal.

  • Add SDK path to local.properties:
    In your project's android folder, create or edit the local.properties file and add your SDK path explicitly:

    # Mac example
    sdk.dir=/Users/YourUsername/Library/Android/sdk
    # Windows example (use forward slashes or escaped backslashes)
    sdk.dir=C:/Users/YourUsername/AppData/Local/Android/Sdk
    

    This ensures Gradle and React Native can find the SDK even if environment variables are misbehaving.

  • Double-check Android Studio SDK Setup:
    Open Android Studio's SDK Manager and confirm:

    • The SDK version matches the compileSdkVersion in your android/build.gradle file
    • You've installed the required Build Tools version (check buildToolsVersion in android/app/build.gradle)
    • The Android Emulator and Platform Tools are installed and up to date

Final Steps

After applying both fixes:

  1. Run react-native doctor again to confirm the SDK is now recognized
  2. Clean your Android build with cd android && ./gradlew clean
  3. Rebuild your app with react-native run-android

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:42:37