react-native run-android with different flavors

Created on 16 Feb 2016  路  11Comments  路  Source: facebook/react-native

In my android project I have created different flavors one for each environment and I have something like this in my build.gradle:

    productFlavors {
        development {
            applicationId "com.myapp.development"
        }

        staging {
            applicationId "com.myapp.staging"
        }

        production {
            applicationId "com.myapp"
        }
    }

After adding productFlavors and run the command ./gradlew tasks I can see that in my project I have available these tasks:

Install tasks
-------------
installDevelopmentDebug 
installProductionDebug 
installStagingDebug 

And it is fine because I can then run this command (in android folder):

./gradlew installDevelopmentDebug

But if I try to run:

react-native run-android

It tries to start ./gradlew installDebug by default and it installs all the android libraries from the project in the device/emulator and it throws a massive error.

I have checked the runAndroid.js file and it looks like this when it takes the task to execute:

    const cmd = process.platform.startsWith('win')
      ? 'gradlew.bat'
      : './gradlew';

    const gradleArgs = ['installDebug'];
    if (args['install-debug']) {
      gradleArgs.push(args['install-debug']);
    }

    console.log(chalk.bold(
      'Building and installing the app on the device (cd android && ' + cmd +
      ' ' + gradleArgs.join(' ') + ')...'
    )); 

It is taking the installDebug task by default, you can add more argument to that task but you cannot replace it.

It would be ideal if you could do something like this:

react-native run-android --option-flavour=staging

And then this would run ./gradlew installStagingDebug instead of installDebug

Locked

Most helpful comment

the last link is helpful but that seems to be deprecated stuff, I could get it to work by running
react-native run-android --variant=developmentDebug

All 11 comments

Hey javierM84, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If you don't know how to do something or something is not working as you expect but not sure it's a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • If this is a feature request or a bug that you would like to be fixed, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • We welcome clear issues and PRs that are ready for in-depth discussion. Please provide screenshots where appropriate and always mention the version of React Native you're using. Thank you for your contributions!

Actually, it is not difficult to implement this functionality. If you want, I can do this, and submit PR after that.

That would be great, I think it is not complicated but I don't have enough experience with gradle.

Thanks

+1 on the PR. Just name it --option-flavor to keep it consistent with the U.S. English naming used in other places (productFlavors, http://developer.android.com/tools/building/configuring-gradle.html etc.).

@javierM84 @mkonicek it is here: https://github.com/facebook/react-native/pull/6010

the last link is helpful but that seems to be deprecated stuff, I could get it to work by running
react-native run-android --variant=developmentDebug

It is great that we can now use --variant and --flavor, but part of the problem remains after adding flavors to build.gradle. When you try to use react-native run-android without the new options:

It tries to start ./gradlew installDebug by default and it installs all the android libraries from the project in the device/emulator and it throws a massive error.

Can't find a better place to put this tidbit but in case anyone runs across the same issues I did...
react-native does NOT support product flavor dimensions. For Android, make sure all of your product flavors are a single list so that react.gradle can parse them into the tasks it's looking for.

try this:

react-native run-android  --flavor=StagingDebug 

@linuxgg I understand that react-native allows you to target flavors & variants. What I'm talking about is specific to gradle. To better understand how product dimensions work, read here: https://developer.android.com/studio/build/build-variants.html#flavor-dimensions.
So in your example, If I were to make 2 dimensions, one for Release/Debug and a separate dimension for Staging/Production, the react.gradle file would not be able to properly bundle the jS assets because of the multiple product dimensions. So instead of this:

flavorDimensions "staging", "production"
  productFlavors {
    debug {
    }

    release {
    }
  }

You have to use this:

  productFlavors {
    stagingdebug {
    }
    stagingrelease {
    }
    productiondebug {
    }
    productonrelease {
    }
  }
Was this page helpful?
0 / 5 - 0 ratings