How to inject productFlavors as options in @nrwl/react-native:build-android

381 Views Asked by At

I'm trying to implement environments in the react-native application.

For the android, I have the following configuration:

.
.
project.ext.envConfigFiles = [
  dev: ".env.dev",
  qa: ".env.qa",
]
.
.
productFlavors {
    qa {
        applicationId "com.myapp.qa"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        resValue "string", "build_config_package", "com.myapp"
    }
    dev {
        applicationId "com.myapp.dev"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        resValue "string", "build_config_package", "com.myapp"
    }
}

npx nx run-android my-app --variant=devDebug --appIdSuffix=dev

the above command is working as expected, it is taking env variables from .env.dev and creating dev apk.

The issue am I facing is with the build command. I think build-android doesn't have options like run-android. It's building both the dev and QA apps.

npx nx build-android my-app --apk

Does anyone have any idea on how to pass options to build-android?

1

There are 1 best solutions below

0
On

In latest(14.X.X) nx workspace, there is an option to pass custom assemble/bundle task using gradleTask.

e.g.

"build-android": {
      "executor": "@nrwl/react-native:build-android",
      "outputs": [
        "apps/mobile/android/app/build/outputs/bundle",
        "apps/mobile/android/app/build/outputs/apk"
      ],
      "options": { "gradleTask": "bundleDevDebug" } // can pass assembleQaRelease, bundleQaRelease, assembleProdRelease or bundleProdRelease etc.
    },

I might need to to append release or debug to following config

project.ext.envConfigFiles = [
  devdebug: ".env.dev",
  qarelease: ".env.qa",
  prodrelease: ".env.prod",
]