You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

React Native构建产物是否包含Source Map?APK/IPA打包时如何禁用内置Source Map?

React Native: Source Map Inclusion & Disabling in APK/IPA Builds

Hey there! Let's tackle your questions about how React Native handles source maps during app builds:

1. Does React Native generate or include Source Maps during the build process?

Great question! The short answer is yes, but behavior varies by build type:

  • Debug builds: Source maps are automatically generated and used under the hood to power debugging tools like Flipper or Chrome DevTools. This lets you debug unminified JavaScript code while running the app in debug mode.
  • Release builds: React Native will generate a standalone source map file (e.g., index.android.bundle.map for Android, index.ios.bundle.map for iOS) by default. However, this file is not bundled into the final APK/IPA—it’s saved in your project’s build output folder (like android/app/build/generated/assets/react/release/ for Android) for optional use in crash reporting or post-release debugging workflows.

2. Are Source Maps bundled into APK/IPA files with the JS Bundle? How to disable this if needed?

First, a key clarification: by default, source maps are NOT included in release APK/IPA packages—only the minified, production-ready JS bundle gets packaged into the app. That said, if you want to either prevent source maps from being generated entirely, or add an extra layer of protection to ensure they never end up in your app, here’s what you can do:

Option 1: Disable source map generation via Metro Config

Edit your project’s metro.config.js to turn off source map generation for all builds, or specifically for release builds:

module.exports = {
  transformer: {
    // Disable source map generation entirely
    sourceMap: false,
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
  // Rest of your metro config...
};

If you only want to disable source maps for release builds, tie it to the NODE_ENV environment variable:

const isProduction = process.env.NODE_ENV === 'production';

module.exports = {
  transformer: {
    sourceMap: !isProduction, // Enable only for non-production builds
    // ... other transformer configs
  },
  // Rest of your metro config...
};

Option 2: Skip source maps via manual bundle commands

If you’re running the react-native bundle command directly (for custom build workflows), add the --no-sourcemap flag to skip source map generation entirely:

# Android release bundle without source maps
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res --no-sourcemap

# iOS release bundle without source maps
react-native bundle --platform ios --dev false --entry-file index.js --bundle-output ios/main.jsbundle --assets-dest ios --no-sourcemap

Option 3: Exclude source maps via Android Gradle config

For Android, add a safety net in your android/app/build.gradle to ensure no .map files get packaged into the APK, even if they accidentally end up in your assets folder:

android {
  // ... existing configs
  packagingOptions {
    exclude "**/*.map"
  }
}

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

火山引擎 最新活动