React Native Getting Started

Posted on  by admin
-->

The App Center SDK uses a modular architecture so you can use any or all of the services.

Let's get started with setting up App Center React Native SDK in your app to use App Center Analytics and App Center Crashes.

1. Prerequisites

Before you begin, make sure that the following prerequisites are met:

  • Welcome to the very start of your React Native journey! If you're looking for environment setup instructions, they've moved to their own section.
  • Continue reading for an introduction to the documentation, Native Components, React, and more!
  • Many different kinds of people use React Native: from advanced iOS developers to React beginners, to people getting started programming for the first time in their career.
  • These docs were written for all learners, no matter their experience level or background. You can start here and read through these docs linearly like a book; or you can read the specific sections you need.

2. Create your app in the App Center Portal to obtain the App Secret

If you've already created your app in the App Center portal, you can skip this step.

  1. Already familiar with React?
  2. You can skip that section—or read it for a light refresher. To work with React Native, you will need to have an understanding of JavaScript fundamentals.
  3. If you’re new to JavaScript or need a refresher, you can dive in or brush up at Mozilla Developer Network.
  4. While we do our best to assume no prior knowledge of React, Android, or iOS development, these are valuable topics of study for the aspiring React Native developer.
  5. Where sensible, we have linked to resources and articles that go more in depth.

Once you've created an app, you can obtain its App Secret on the Settings page on the App Center Portal. At the top right hand corner of the Settings page, click on the triple vertical dots and select Copy app secret to get your App Secret.

3. Add the App Center SDK modules

The default integration of the SDK uses CocoaPods for iOS. If you're not using CocoaPods in your app, you need to integrate the React Native SDK manually for your iOS app.

Open a Terminal and navigate to the root of your React Native project, then enter the following line to add App Center Analytics and Crashes to the app:

In case you prefer yarn over npm, use the following command to install App Center:

The App Center SDK uses a modular approach, where you just add the modules for App Center services that you want to use. appcenter-analytics and appcenter-crashes make sense to add to almost every app, as they provide value with no additional setup required. appcenter provides general purpose App Center APIs, useful for multiple services.

3.1 Integrate the SDK automatically for React Native 0.60

3.1.1 Integrate React Native iOS

  1. This introduction lets you get started immediately in your browser with interactive examples like this one:.

  2. The above is a Snack Player. It’s a handy tool created by Expo to embed and run React Native projects and share how they render in platforms like Android and iOS. The code is live and editable, so you can play directly with it in your browser. Go ahead and try changing the "Try editing me!"

  3. text above to "Hello, world!" Optionally, if you want to setup a local development environment, you can follow our guide to setting up your environment on your local machine and paste the code examples into your App.js file there.

    • (If you are a web developer, you may already have a local environment set up for mobile browser testing!). With React, you can make components using either classes or functions.
    • Originally, class components were the only components that could have state.

3.1.2 Integrate React Native Android

  1. But since the introduction of React's Hooks API, you can add state and more to function components. Hooks were introduced in React Native 0.59., and because Hooks are the future-facing way to write your React components, we wrote this introduction using function component examples.

Note: If the folder named assets doesn't exist, it should be created under "project_root/android/app/src/main/assets"

  1. Where useful, we also cover class components under a toggle like so:.

3.2 Integrate the SDK automatically for React Native lower than 0.60

Function Component.

Class Component. You can find more examples of class components in previous versions of this documentation. People from many different development backgrounds are learning React Native. You may have experience with a range of technologies, from web to Android to iOS and more.

  1. We try to write for developers from all backgrounds. Sometimes we provide explanations specific to one platform or another like so:.

    Android developers may be familiar with this concept. Web developers may be familiar with this concept.

    Menu paths are written in bold and use carets to navigate submenus.

    Example: Android Studio > Preferences. Now that you know how this guide works, it's time to get to know the foundation of React Native: Native Components.

    This page will help you install and build your first React Native app.

    If you are new to mobile development, the easiest way to get started is with Expo CLI. Expo is a set of tools built around React Native and, while it has many features, the most relevant feature for us right now is that it can get you writing a React Native app within minutes.

    You will only need a recent version of Node.js and a phone or emulator.

    If you'd like to try out React Native directly in your web browser before installing any tools, you can try out Snack. If you are already familiar with mobile development, you may want to use React Native CLI.

  2. It requires Xcode or Android Studio to get started. If you already have one of these tools installed, you should be able to get up and running within a few minutes. If they are not installed, you should expect to spend about an hour installing and configuring them.

  3. Expo CLI Quickstart. React Native CLI Quickstart. Assuming that you have Node 12 LTS or greater installed, you can use npm to install the Expo CLI command line utility:. Then run the following commands to create a new React Native project called "AwesomeProject":. This will start a development server for you. Install the Expo client app on your iOS or Android phone and connect to the same wireless network as your computer.

3.3 [iOS only] Integrate the SDK manually for React Native without react-native link or CocoaPods

Do this integration, if you don't want to use CocoaPods.We strongly recommend integrating the SDK via CocoaPods as described above. Nonetheless, it's also possible to integrate the iOS native SDK manually.

On Android, use the Expo app to scan the QR code from your terminal to open your project.

On iOS, use the built-in QR code scanner of the Camera app. Now that you have successfully run the app, let's modify it. Open App.js in your text editor of choice and edit some lines.

The application should reload automatically once you save your changes. Congratulations!

  1. You've successfully run and modified your first React Native app. Expo also has docs you can reference if you have questions specific to the tool.

  2. You can also ask for help at Expo forums. These tools help you get started quickly, but before committing to building your app with Expo CLI, read about the limitations.

  3. If you have a problem with Expo, before creating a new issue, please see if there's an existing issue about it:.

  4. in the Expo CLI issues (for issues related to Expo CLI), or. in the Expo issues (for issues about the Expo client or SDK). If you're curious to learn more about React Native, check out the Introduction to React Native.

  5. Expo CLI allows you to run your React Native app on a physical device without setting up a development environment.

    • If you want to run your app on the iOS Simulator or an Android Virtual Device, please refer to the instructions for "React Native CLI Quickstart" to learn how to install Xcode or set up your Android development environment.
    • Once you've set these up, you can launch your app on an Android Virtual Device by running npm run android, or on the iOS Simulator by running npm run ios (macOS only).
  6. Because you don't build any native code when using Expo to create a project, it's not possible to include custom native modules beyond the React Native APIs and components that are available in the Expo client app.

  7. If you know that you'll eventually need to include your own native code, Expo is still a good way to get started.

    • In that case you'll need to "eject" eventually to create your own native builds.
    • If you do eject, the "React Native CLI Quickstart" instructions will be required to continue working on your project. Expo CLI configures your project to use the most recent React Native version that is supported by the Expo client app. The Expo client app usually gains support for a given React Native version about a week after the React Native version is released as stable.
    • You can check this document to find out what versions are supported.
  8. If you're integrating React Native into an existing project, you'll want to skip Expo CLI and go directly to setting up the native build environment.

    • Select "React Native CLI Quickstart" above for instructions on configuring a native build environment for React Native.

    • Follow these instructions if you need to build native code in your project. For example, if you are integrating React Native into an existing application, or if you "ejected" from Expo, you'll need this section.

      • /node_modules/appcenter/ios
      • /node_modules/appcenter-analytics/ios
      • /node_modules/appcenter-crashes/ios
    • The instructions are a bit different depending on your development operating system, and whether you want to start developing for iOS or Android.

  9. If you want to develop for both Android and iOS, that's fine - you can pick one to start with, since the setup is a bit different. You will need Node, Watchman, the React Native command line interface, a JDK, and Android Studio.

    • libAppCenterReactNative.a
    • libAppCenterReactNativeAnalytics.a
    • libAppCenterReactNativeCrashes.a
  10. While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.

  • $(SRCROOT)/../node_modules/appcenter/ios/AppCenterReactNative
  • $(SRCROOT)/../node_modules/appcenter-analytics/ios/AppCenterReactNativeAnalytics
  • $(SRCROOT)/../node_modules/appcenter-crashes/ios/AppCenterReactNativeCrashes
  1. We recommend installing Node and Watchman using Homebrew.

    • Run the following commands in a Terminal after installing Homebrew:. If you have already installed Node on your system, make sure it is Node 10 or newer.
    • Watchman is a tool by Facebook for watching changes in the filesystem.
  2. It is highly recommended you install it for better performance. We recommend installing JDK using Homebrew. Run the following commands in a Terminal after installing Homebrew:. If you have already installed JDK on your system, make sure it is JDK 8 or newer.

Setting up your development environment can be somewhat tedious if you're new to Android development.

If you're already familiar with Android development, there are a few things you may need to configure.

  1. In either case, please make sure to carefully follow the next few steps.

    • Download and install Android Studio. While on Android Studio installation wizard, make sure the boxes next to all of the following items are checked:.
  2. If you are not already using Hyper-V: Performance (Intel ® HAXM) (See here for AMD or Hyper-V).

    • Then, click "Next" to install all of these components.
    • If the checkboxes are grayed out, you will have a chance to install these components later on. Once setup has finalized and you're presented with the Welcome screen, proceed to the next step. Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 10 (Q) SDK in particular.

3.4 [Android only] Integrate the SDK manually for React Native lower than 0.60 without react-native link

Integration steps without the react-native link command.

  1. Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 10 (Q) entry, then make sure the following items are checked:.

Intel x86 Atom_64 System Image or Google APIs Intel x86 Atom System Image.

Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the "Android SDK Build-Tools" entry, then make sure that 29.0.2 is selected.

  1. Finally, click "Apply" to download and install the Android SDK and related build tools. The React Native tools require some environment variables to be set up in order to build apps with native code.

  2. Add the following lines to your $HOME/.bash_profile or $HOME/.bashrc (if you are using zsh then ~/.zprofile or ~/.zshrc) config file:.

    • .bash_profile is specific to bash.
    • If you're using another shell, you will need to edit the appropriate shell-specific config file.
  3. Type source $HOME/.bash_profile for bash or source $HOME/.zprofile to load the config into your current shell. Verify that ANDROID_HOME has been set by running echo $ANDROID_HOME and the appropriate directories have been added to your path by running echo $PATH.

  4. Please make sure you use the correct Android SDK path. You can find the actual location of the SDK in the Android Studio "Preferences" dialog, under Appearance & Behavior → System Settings → Android SDK.

React Native has a built-in command line interface.

Rather than install and manage a specific version of the CLI globally, we recommend you access the current version at runtime using npx, which ships with Node.js.

3.5 If you use auto-backup to avoid getting incorrect information about device, follow the next steps:

With npx react-native , the current stable version of the CLI will be downloaded and executed at the time the command is run.

If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues.

React Native has a built-in command line interface, which you can use to generate a new project.

You can access it without installing anything globally using npx, which ships with Node.js.

a. Create appcenter_backup_rule.xml file in the android/app/src/main/res/xml folder.

b. Open the project's AndroidManifest.xml file. Add the android:fullBackupContent attribute to the element. It should point to the appcenter_backup_rule.xml resource file.

c. Add the following backup rules to the appcenter_backup_rule.xml file:

4. Start the SDK

Now you can build and launch your application either from command line or Xcode/Android Studio.

4.1 Build and run your application from command line

You may build and launch your iOS application by the following command:

Let's create a new React Native project called "AwesomeProject":.

This is not necessary if you are integrating React Native into an existing application, if you "ejected" from Expo, or if you're adding Android support to an existing React Native project (see Platform Specific Code).

You may build and launch your Android application by the following command:

You can also use a third-party CLI to init your React Native app, such as Ignite CLI.

If you want to start a new project with a specific React Native version, you can use the --version argument:. You can also start a project with a custom React Native template, like TypeScript, with --template argument:.

4.2 Build and run your application from Xcode or Android Studio

For iOS, open your project's ios/{appname}.xcworkspace or ios/{appname}.xcodeproj file in Xcode and build from there.

You will need an Android device to run your React Native Android app.

This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer. Either way, you will need to prepare the device to run Android apps for development. If you have a physical Android device, you can use it for development in place of an AVD by plugging it in to your computer using a USB cable and following the instructions here.

For Android, import your android project in Android Studio and build from there.

You're all set to visualize Analytics and Crashes data on the portal that the SDK collects automatically. There's no additional setup required. Look at Analytics and Crashes section for APIs guides and walkthroughs to learn what App Center can do.