How to pass Gradle --offline flag from npm run dev gradle-offline in React Native run-android
10:11 05 May 2026

I have a custom dev.js script that runs my React Native Android app using react-native run-android.

I want to be able to run:

npm run dev gradle-offline

…and have it automatically pass --offline to Gradle (via React Native CLI).


Current script

const { spawn } = require('child_process');
const os = require('os');

const isGradleOffline = process.argv.includes('gradle-offline');

if (os.type() === 'Linux' || os.type() === 'Darwin') {
  spawn(
    `ENVFILE=/envs/.env.dev react-native run-android --active-arch-only --appIdSuffix debug ${isGradleOffline ? '-- --offline' : ''}`,
    [],
    { shell: true, stdio: 'inherit' }
  );
} else if (os.type() === 'Windows_NT') {
  spawn(
    `SET ENVFILE=/envs/.env.dev && react-native run-android --active-arch-only --appIdSuffix debug ${isGradleOffline ? '-- --offline' : ''}`,
    [],
    { shell: true, stdio: 'inherit' }
  );
} else {
  throw new Error(`Unsupported OS found: ${os.type()}`);
}

Problem

When I run:

npm run dev gradle-offline

I’m not sure if:

  • process.argv is the correct way to detect the flag

  • -- --offline is being forwarded correctly to Gradle

  • This is the correct way to structure arguments for spawn


What I want

A reliable way to:

  1. Detect gradle-offline from:

    npm run dev gradle-offline
    
  2. Pass --offline correctly to Gradle via:

    react-native run-android -- --offline
    
  3. Make it work cross-platform (Windows + macOS/Linux)


Expected behavior

  • npm run dev → normal build

  • npm run dev gradle-offline → Gradle runs with --offline


Question

What is the correct way to structure this script so that:

  • arguments from npm are reliably read

  • Gradle flags are properly forwarded

  • and this works with cached files consistently when the offline mode is on

react-native gradle npm react-native-cli