Site icon Mobile App Development Services

Automate React Native App Builds With Fastlane And Appcenter

Automating your application build process brings you speed, reliability and one less thing to worry about. It saves time, resources and effort getting an app from development to distribution.

What is a Fastlane?

Why Fastlane?

What is Appcenter?

Microsoft Visual Studio App Center is an integrated mobile development lifecycle solution for iOS, Android and Windows apps. It brings together multiple services commonly used by mobile developers, including build, test, distribute, monitoring, diagnostics into one single integrated cloud solution.

You May Also Like: Make Your Code Reusable Between React And React Native

Getting started with Fastlane & Appcenter

Let’s begin with a simple Fastlane guide for building and uploading new react-native apps to Visual Studio App Center. There is no official guide available yet we have managed to successfully run it.

IOS Configuration

Signing

For new projects, we need to create a bundle ID and provisioning profile. To create a new bundle ID, run:

fastlane produce create -i

To setup a provisioning profile, create a GIT repo and then run:

fastlane match init

A Matchfile will be created. Change the profile type (e.g. appstore, adhoc) and add your Apple developer account under username.

Now, add the following to the Fastfile:

platform :ios do    
  desc 'Fetch certificates and provisioning profiles'
  lane :certificates do
    match(app_identifier: 'com.app.bundle')
  end
end

Change com.app.bundle to the bundle ID you created earlier. In the future, pass readonly: true to the match action. This would ensure the command wouldn’t create new provisioning profiles.

Building

Add the following lane to iOS:

lane :bump_build_version do
  increment_build_number(xcodeproj: './ios/YourAppName.xcodeproj')
  commit_version_bump(message: 'Bump iOS build', xcodeproj: './ios/YourAppName.xcodeproj')
  git_pull
  push_to_git_remote
end

lane :build do
  cocoapods(podfile: "./ios/Podfile")
  certificates
  bump_build_version
  gym(
    scheme: 'YourAppName', 
    project: './ios/YourAppName.xcodeproj',
    clean: true, 
    workspace: './ios/YourAppName.xcworkspace, 
    export_method: 'development', 
    output_directory: './builds'
  )
end

Uploading

Add the following lane to iOS:

lane :beta do
  bump_build_version
  build
  appcenter_upload(
    api_token: ENV["APPCENTER_API_TOKEN"],
    owner_name: ENV["APPCENTER_OWNER_NAME"],
    owner_type: ENV["APPCENTER_OWNER_TYPE"],
    app_name: ENV["APPCENTER_APP_NAME_IOS"],
    ipa: ENV["IPA_PATH"]
  )
end

Run fastlane ios beta. Your IOS application is now on App Center! 

Android Configuration

Building

platform :android do   
  desc 'Bump version and Build the Android application.'
  lane :bump_build_version do
    path = '../android/app/build.gradle'
    re = /versionCode\s+(\d+)/ 

    s = File.read(path)
    versionCode = s[re, 1].to_i     
    s[re, 1] = (versionCode + 1).to_s

    f = File.new(path, 'w')     
    f.write(s)     
    f.close  

    repo_clean = `git status --porcelain`.empty?     
    if !repo_clean           
      git_commit(path: 'android/app/build.gradle', message: 'Bump android build')     
    end     
    git_pull           
    push_to_git_remote 
  end

  lane :build do     
     gradle(task: 'clean', project_dir: 'android/')     
     gradle(
        task: 'assemble', 
        build_type: 'release', 
        project_dir: 'android/',
        flavor: ENV["ANDROID_FLAVOUR"]
     )   
  end 
end

Generated .apk will be at 
android/app/build/outputs/apk/release/app-release.apk

Uploading

Add the following to the Android lane:

desc 'Build and upload to App Center.'
lane :beta do
  bump_build_version
  build
  appcenter_upload(
    api_token: ENV["APPCENTER_API_TOKEN"],
    owner_name: ENV["APPCENTER_OWNER_NAME"],
    owner_type: ENV["APPCENTER_OWNER_TYPE"],
    app_name: ENV["APPCENTER_APP_NAME_ANDROID"],
    apk: ENV["APK_PATH"]
  )
end

Run fastlane android beta. Your android application is now on App Center!