cli icon indicating copy to clipboard operation
cli copied to clipboard

Allow gradle options to be passed to build

Open fabiancook opened this issue 5 years ago • 4 comments

Describe the Feature

It would be great if we could include options for the gradle build command, or overwrite them completely

Here is the related source https://github.com/react-native-community/cli/blob/master/packages/platform-android/src/commands/runAndroid/index.ts#L204

This would allow me to disable tests for the build for example

Possible Implementations

function buildApk(gradlew: string, sourceDir: string, replacementGradleBuildArgs: string = undefined) {
  try {
    // using '-x lint' in order to ignore linting errors while building the apk
    const gradleBuildArgs = replacementGradleBuildArgs || ['-x', 'lint'].join(' ');
    const gradleArgs = ['build', gradleBuildArgs];
    logger.info('Building the app...');
    logger.debug(`Running command "${gradlew} ${gradleArgs.join(' ')}"`);
    execa.sync(gradlew, gradleArgs, {stdio: 'inherit', cwd: sourceDir});
  } catch (error) {
    throw new CLIError('Failed to build the app.', error);
  }
}
buildApk(gradlew, androidProject.sourceDir, args.gradleBuildArgs);

Usage:

yarn android --gradleBuildArgs="-x lint -x test"

I would expect to see in the logs:

Running command gradle build -x lint - x test

fabiancook avatar May 02 '20 23:05 fabiancook

I am happy to do a PR if this issue is flagged as good to go

fabiancook avatar May 14 '20 21:05 fabiancook

cc @Krizzu @troZee @dulmandakh

thymikee avatar May 14 '20 21:05 thymikee

I believe this is essential for multi-stage builds, since Gradle allows easy build time overrides. Due to missing support of this feature, I was forced to come up with hack I did not like.

If there is interest from maintainer, I am willing to spend time on this.

cc @thymikee @Krizzu @troZee @dulmandakh

Andreyco avatar Feb 03 '21 23:02 Andreyco

As a workaround, I used env variables. I know it is not perfect but enough for my use case. Hope it will help someone. Let me know if there is a better way, please :)

# this would be my own run-android.sh where I can pass VAR1 as parameter
export VAR1=foo
react-native run-android

In build.gradle then:

def name = "$System.env.VAR1"

xotahal avatar Sep 29 '21 05:09 xotahal

There hasn't been any activity on this issue in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

github-actions[bot] avatar Nov 27 '22 03:11 github-actions[bot]

There's ongoing effort to add custom gradle options with #1729

adamTrz avatar Nov 28 '22 17:11 adamTrz