Setting Up Your Environment in iOS
I used this blog post to set up my iOS environment. I won’t duplicate this explanation here. Just come back after you’ve followed these steps!
Setting Up Your Environment in Android
By wiring up our Android NativeModule correctly we can leverage the work we have already done on the iOS side. We’ll be taking a similar strategy to the iOS work we just did. We’ll create build types, and when the app compiles, they will make make a
NOTE: You’ll need to have Android Studio set up, so please do so if you haven’t already.
First, we’ll need to edit our build.gradle file to be able to create different builds. Add
android and add whatever environments you need.
release are available by default.
initWith(buildTypes.debug) is there to duplicate the
release apps need to be signed by default, but we didn’t need that level of security in test or acceptance on my project.
Note that you cannot use the name
test for a buildType, which is why we went with
uiTest. It shouldn’t make a difference in the end.
Click the Sync Project with Gradle Files button (looks like a green Pokeball) at the top of Android Studio so it generates the proper build file.
Create a NativeModule
buildEnvironment. First, create an
RNConfig class and paste in the code below. You can put this class anywhere; I put mine in
Next, we’ll create a package file that wraps this class. In
java/com/example/app/packages, create an
RNConfigPackage class and add the code below. Again, you can put this anywhere inside
Finally, in MainApplication.java, add the package to the
That’s it! Now we can run the app with different schemes (iOS) or build variants (Android) and map different api url’s, etc, to the different environments.
Run different schemes (iOS)
You can select different schemes in the top left corner of XCode.
If you want to create a build for a specific scheme from the command line, cd into the
ios directory and use this command:
xcodebuild -sdk iphonesimulator9.3 -project .xcodeproj -scheme '' -destination "platform=iOS Simulator,name=iPhone 6,OS=9.3" -derivedDataPath build
Run different build variants (Android)
Select the build variants tab in the bottom left corner of Android Studio on the left-hand side. Then select whatever build variant you’d like. You have had to gradle sync after editing your build.gradle configuration for your build variants to appear in this list.
If you need a command line build, cd into the
android directory and use this command:
./gradlew clean assembleUiTest (or assemble)
If you’re having trouble finding the command, you can use
./gradlew tasks to list out all possible build tasks.
Build environments are a powerful configuration tool that allow us to specify the services we need in different contexts without making. Utilizing NativeModules, we can have access to our build environment in React Native as well.
In my next blog post, I’ll show you how to set up UI Tests for your React Native app leveraging the environments that we created today.